SpringCloud config分布式配置中心
分布式系统面临的配置问题
![1597116784397]()
![1597116831303]()
![1597116840717]()
能干嘛?
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
将配置信息以REST接口的形式暴露,post、curl访问刷新均可….
与Github整合配置
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
Config服务端配置与测试
1.用你自己的账号在Github上新建一个名为sprincloud-config的新Repository
2.由上一步获得刚新建的git地址,写你自己的仓库地址
3.本地硬盘上新建git仓库并clone,
本地地址:D:\44\SpringCloud2020
git命令 git clone xxx
4.此时在本地D盘符下D:\44\SpringCloud2020\springcloud-config
![1597305038489]()
表示多个环境的配置文件,保存格式必须为UTF-8,如果需要修改,
此处模拟运维人员操作git和github
git add
git commit -m “init yml”
git push origin master
5.新建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 38 39 40 41 42
| <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>com.kayleh.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </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/Kayleh/springcloud-config.git search-paths: - springcloud-config username: Kayleh password: label: master eureka: client: service-url: defaultZone: http://localhost:7001/eureka
|
主启动类: @EnableConfigServer
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.kayleh.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344 .class,args); } }
|
windows下修改hosts文件,增加映射
1
| 127.0.0.1 config-3344.com
|
测试通过Config微服务是否可以从Github上获取配置内容
启动微服务3344
http://config-3344.com:3344/master/config-dev.yml
配置读取规则
![1597305257005]()
![1597305264686]()
/{label}/{application}-{profile}.yml(最推荐使用这种方式)
master分支
http://config-3344.com:3344/master/config-dev.yml
http://config-3344.com:3344/master/config-test.yml
http://config-3344.com:3344/master/config-prod.yml
dev分支
http://config-3344.com:3344/dev/config-dev.yml
http://config-3344.com:3344/dev/config-test.yml
http://config-3344.com:3344/dev/config-prod.yml
/{application}-{profile}.yml
- http://config-3344.com:3344/config-dev.yml
- http://config-3344.com:3344/config-test.yml
- http://config-3344.com:3344/config-prod.yml
- http://config-3344.com:3344/config-xxxx.yml(不存在的配置)
/{application}-{profile}[/{label}]
- http://config-3344.com:3344/config/dev/master
- http://config-3344.com:3344/config/test/master
- http://config-3344.com:3344/config/prod/master
重要配置细节总结
![1597305464178]()
成功实现了用SpringCloud Config 通过GitHub获取配置信息
Config客户端配置与测试
新建cloud-config-client-3355
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 38 39 40 41 42 43 44
| <dependencies>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.kayleh.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </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>
|
bootstap.yml
是什么?
![1597305556768]()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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
|
![1597305596684]()
修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version
主启动类:
1 2 3 4 5 6 7 8 9 10 11
| package com.kayleh.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run( ConfigClientMain3355.class,args); } }
|
业务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.kayleh.springcloud.Controller;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class ConfigClientController {
@Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
|
启动Config配置中心3344微服务并自测
1 2
| http://config-3344.com:3344/master/config-dev.yml http://config-3344.com:3344/master/config-test.yml
|
启动3355作为Client准备访问
1
| http://localhost:3355/configInfo
|
成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息
问题随时而来,分布式配置的动态刷新
Linux运维修改GitHub上的配置文件内容做调整
刷新3344,发现ConfigServer配置中心立刻响应
刷新3355,发现ConfigServer客户端没有任何响应
3355没有变化除非自己重启或者重新加载
难道每次运维修改配置文件,客户端都需要重启??噩梦
Config客户端之动态刷新
避免每次更新配置都要重启客户端微服务3355
修改3355模块
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 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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
management: endpoints: web: exposure: include: "*"
|
@RefreshScope业务类Controller修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.kayleh.springcloud.Controller;
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RefreshScope @RestController public class ConfigClientController {
@Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
|
此时修改去github
测试3344 和3355
http://config-3344.com:3344/master/config-dev.yml
http://localhost:3355/configInfo
3355没有改变,(只有重启才有效果)
需要运维人员发送Post请求刷新3355,必须是Post请求
1
| curl -X POST "http://localhost:3355/actuator/refresh"
|
再次http://localhost:3355/configInfo
成功实现了客户端3355刷新到最新配置内容,避免了服务的重启
想想还有什么问题?
假如有多个微服务客户端3355/3366/3377。。。。
每个微服务都要执行一次post请求,手动刷新?
可否广播,一次通知,处处生效?
我们想大范围的自动刷新,求方法