Config服务端配置
git
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
用你自己的账号在Github上新建一个名为sprincloud-config
的新Repository

仓库中添加config-dev.yml
,config-prod.yml
,config-test.yml
,三个文件内容只有名字不同。创建dev分支,将文件中的master全都修改为dev
文件内容
1 2
| config: info: "master branch,springcloud-config/config-dev.yml version=1"
|


clone到本地使用git clone
命令

Model
新建Module模块cloud-config-center-3344
它既为Cloud的配置中心模块cloudConfig Center

pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
yml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server: port: 3344
spring: application: name: cloud-config-center cloud: config: server: git: uri: https://github.com/kylincw/sprincloud-config.git search-paths: - springcloud-config label: master
eureka: client: service-url: defaultZone: http://localhost:7001/eureka
|
主启动类
编写ConfigCenterMain3344
通过@EnableConfigServer
开启ConfigServer服务

1 2 3 4 5 6 7 8
| @SpringBootApplication
@EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344 .class,args); } }
|
测试
启动7001 3344,访问
Config客户端配置
新建cloud-config-client-3355

pom
config客户端导入spring-cloud-starter-config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
yml
Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的“Application Context”的父上下文。初始化的时候,“Bootstrap Context”负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的“Environment”。
“Bootstrap”属性有高优先级,默认情况下,他们不会被本地配置覆盖。“Bootstrap context”和“Application Context”有着不同的约定,所以新增一个“bootstrap.yml”文件,保证“Bootstrap Context”和“Application Contetxt”的配置分离
application.yml是用户级的资源配置项,bootstrap.yml是系统级的,优先级更加高。
要将Client模块的application.yml文件改为bootstrap.yml这是很关键的。因为bootstrap.yml是比application.yml先加载的。
创建bootstrap.yml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server: port: 3355
spring: application: name: config-client cloud: config: label: master name: config profile: dev uri: http://localhost:3344 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka
|
主启动类
创建ConfigClientMain3355

1 2 3 4 5 6 7
| @SpringBootApplication @EnableEurekaClient public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run( ConfigClientMain3355.class,args); } }
|
controller

1 2 3 4 5 6 7 8 9 10 11
| @RestController public class ConfigClientController {
@Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
|
测试
启动7001 3344 3355

3344自测访问http://localhost:3344/master/config-dev.yml

3355测试访问http://localhost:3355/configInfo

成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息!
但是问题随之而来,分布式配置的动态刷新,Linux运维修改GitHub上的master分支的config-dev.yml
配置文件内容做调整,version变为2

刷新3344,发现ConfigServer配置中心立刻响应http://localhost:3344/master/config-dev.yml

但是访问3355却依旧没有发生改变http://localhost:3355/configInfo

只有重启重新加载才更新

难道每次运维修改配置文件,客户端都需要重启??
Config客户端动态刷新
pom
引入actuator监控
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
yml
暴露监控端口

1 2 3 4 5 6
| management: endpoints: web: exposure: include: "*"
|
controller
加上@RefreshScope
注解

1 2 3 4 5 6 7 8 9 10 11 12
| @RefreshScope @RestController public class ConfigClientController {
@Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
|
测试
重新启动3355,启动成功后将master分支的config-dev
version修改为3
访问3344,更新成功

访问3355,依旧为2

配置没有生效
此时需要运维人员发送Post请求刷新3355curl -X POST "http://localhost:3355/actuator/refresh"
必须是Post请求

刷新3355页面,version更新为3

但是目前还是存在问题,如果有多个微服务都需要更新,则要发多次post请求,手动刷新。可否广播,一次通知,处处生效?如果有些服务不需要更新,能否精确的控制刷新?这都是目前不能做到的。