SpringCloud之Config 分布式的配置中心

一、简介

在spring cloud config 组件中,分两个角色,一是config server,二是config client。创建一个spring-boot项目,取名为config

二、构建Config Server
 

创建一个config-server模块,

其build.grale如下:

plugins {
    id 'java'
}
version 'unspecified'
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'org.springframework.cloud:spring-cloud-config-server'
    compile 'org.springframework.boot:spring-boot-starter-test'
}

该模块目录结构如下
在程序的入口ConfigServerApplication类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}


需要在程序的配置文件bootstrap.yml文件配置以下:  程序运行会默认先加载名字为bootstrap的配置文件,

写完最好格式化一下,有一个空格加多了,运行时都会报各种各样的错误!!

server:
  port: 7001
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/XXXX/git-config-repository.git
          searchPaths: config-repo
          label: master

 

bootstrap.properties配置文件如下

spring.application.name=config-server
server.port=7001

spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用

远程仓库https://github.com/XXXX/git-config-repository/ 中有个文件config-client-dev.yml文件中有一个属性:

foo = foo version 3

启动程序:访问http://localhost:7001/foo/dev

{"name":"foo","profiles":["dev"],"label":"master",
"version":"792ffc77c03f4b138d28e89b576900ac5e01a44b","state":null,"propertySources":[]}


证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties


三、构建一个config client
重新创建一个模块,取名为config-client,其gradle文件:

 

plugins {
    id 'java'
}

version 'unspecified'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12' 
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.cloud:spring-cloud-starter-config'
    compileOnly 'org.springframework.boot:spring-boot-autoconfigure' 
 
}

需要在程序的配置文件bootstrap.yml文件配置以下

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:7001/
server:
  port: 7002

其配置文件bootstrap.properties:

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:7001/
server.port=7002

 
spring.cloud.config.label 指明远程仓库的分支

spring.cloud.config.profile

dev开发环境配置文件
test测试环境
pro正式环境


spring.cloud.config.uri= http://localhost:7001/ 指明配置服务中心的网址。

程序的入口类,写一个API接口“/hi”,返回从配置中心读取的foo变量的值,代码如下:

 注意应用名的映射!!!

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}


打开网址访问:http://localhost:7001/hi,网页显示:

foo version 3

这就说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的,如图: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值