09.SpringCloud Config (分布式配置中心)
1.概述
分布式系统面临的配置问题是什么
能干嘛
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露 post、curl访问刷新均可....
与Github整合配置由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
官网https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
2.Config服务端配置与测试
Config服务端的创建
用你自己的账号在Github上新建一个名为sprincloud-config的新Repository添加三个配置文件进这个仓库config-dev.ymlconfig:
info: dev1config-prod.ymlconfig:
info: prod1config-test.ymlconfig:
info: test1新建Module模块cloud-config-center-3344它为Cloud的配置中心模块CloudConfig Centerpom
<?xml version="1.0" encoding="UTF-8"?>
springcloud2020
com.chl.springcloud
1.0-SNAPSHOT
4.0.0
cloud-config-center-3344
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.chl.springcloud
cloud-api-commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
ymlserver:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config: #分支/config-环境名.yml
server: #http://config-3344.com:3344/master/config-dev.yml
git: #githud仓库的位置 用码云不行,识别不了,只能githud
uri: https://github.com/badbad001/springcloud-config.git
search-paths:
- springcloud-config #仓库名称
label: master #仓库的分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka启动类@SpringBootApplication
@EnableConfigServer //开启服务配置中心
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344 .class,args);
}
}windows下修改hosts文件,增加映射127.0.0.1 config-3344.com测试通过Config微服务是否可以从Github上获取配置内容启动微服务3344http://config-3344.com:3344/master/config-dev.yml 配置读取规则
/{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
- 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
重要配置细节总结
成功实现了用SpringCloud Config 通过GitHub获取配置信息
3.Config客户端配置与测试
新建cloud-config-client-3355pom<?xml version="1.0" encoding="UTF-8"?>
springcloud2020
com.chl.springcloud
1.0-SNAPSHOT
4.0.0
cloud-config-client-3355
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.chl.springcloud
cloud-api-commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
bootstap.yml
配置如下
#这个文件名 bootrap.yml 系统级别的 优先于 application.yml(用户界别)
server:
port: 3355
spring:
application:
name: config-client
cloud: #config客户端的配置
config: #组装成就是 http://localhost:3344/master/config-dev.yml 配置的是从哪里获取配置信息
uri: http://localhost:3344 #配置中心地址
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version主启动
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3355.class,args);
}
}业务类@RestController
public class ConfigClientController {
//获取统一配置的数据信息
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}测试启动Config配置中心3344微服务并自测- http://config-3344.com:3344/master/config-dev.yml
- http://config-3344.com:3344/master/config-test.yml
- http://localhost:3355/configInfo
成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息
问题随时而来,分布式配置的动态刷新问题
- Linux运维修改GitHub上的配置文件内容做调整
- 刷新3344,发现ConfigServer配置中心立刻响应
- 刷新3355,发现ConfigServer客户端没有任何响应
- 3355没有变化除非自己重启或者重新加载
- 难道每次运维修改配置文件,客户端都需要重启??噩梦
4.Config客户端之动态刷新
避免每次更新配置都要重启客户端微服务3355动态刷新步骤
修改3355模块pom引入actuator监控
org.springframework.boot
spring-boot-starter-actuator
yml#这个文件名 bootrap.yml 系统级别的 优先于 application.yml(用户界别)
server:
port: 3355
spring:
application:
name: config-client
cloud: #config客户端的配置
config: #组装成就是 http://localhost:3344/master/config-dev.yml 配置的是从哪里获取配置信息
uri: http://localhost:3344 #配置中心地址
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#暴露监控端口,用于刷新分布式的配置
management:
endpoints:
web:
exposure:
include: "*"@RefreshScope业务类Controller修改@RestController
@RefreshScope //用于刷新分布式的配置
//可能需要服务中心先请求一次,而且还要运维人员发起一个请求去刷新相应的客户端
//例如运维人员改了,还要刷新客户端3355 curl -X POST "http://localhost:3355/actuator/refresh
public class ConfigClientController {
//获取统一配置的数据信息
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}此时修改github---> 3344 ---> 3355http://localhost:3355/configInfo 3355改变了没有???没有改变,(┬_┬)为什么需要运维人员发送Post请求刷新3355 必须是Post请求
curl -X POST "http://localhost:3355/actuator/refresh" 再次访问http://localhost:3355/configInfo成功实现了客户端3355刷新到最新配置内容 避免了服务的重启