概述
官网地址:
https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/
分布式系统面临的配置问题
Spring Cloud Config 是什么
Spring Cloud Config为微服务中的微服务提供集中化的外部配置支持 ,配置服务器为各个不同微服务应用的 所有环境提供了一个中心化的外部配置
服务端是一个单独的微服务
客户端是一个一个的微服务
Spring Cloud Config 能干啥
与GitHub 整合配置
由于SpringCloud Config默认使用Git来存储配置文件(也有 其他方式,比如支持SVN和本地文件),但最 推荐的还是Git,而且使用的是http访问形式
GitHub创建配置中心仓库
- 用自己的GitHub账号在GitHub上新建一个名为springcloud-config的Repostory
配置中心服务搭建
新建springboot模块:
添加pom依赖
<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>
配置文件yml
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://github.com/fangyajun/springcloud-config.git #GitHub上面的git仓库名字
####搜索目录
search-paths:
- springcloud-config
####读取分支
label: master
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
主启动类
@EnableConfigServer
@SpringBootApplication
public class SpringcloudConfigCenterApplication {
启动测试:
测试地址:http://localhost:3344/master/config-dev.yml
结果:已经成功访问到了 github上的配置
配置读取规则
/ {application} / {profile} [/ {label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中application注入作为spring.config.name在SpringApplication(什么是通常application以规则的弹簧引导应用程序),profile是一个有效简表(或逗号分隔的属性列表),并且label是一个可选的git标签(默认为master)。
配置规则总结
Config 客户端配置与测试
-
新建一个springboot 客户端模块
-
添加pom依赖
<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>
-
添加配置
bootstrap.yml
文件
bootstrap.yml是什么?
server: port: 3355 spring: application: name: config-client cloud: #Config客户端配置 config: label: master #分支名称 name: config #配置文件名称 profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml uri: http://localhost:3344 #配置中心地址k #服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka
-
主启动类
@EnableEurekaClient @SpringBootApplication public class SpringcloudConfigClientApplication {
-
业务类:
注意@Value("${config.info}")
读取的是Github 上的配置文件配置的信息@RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
-
启动测试:
从访问结果看 ,配置客户端经过配置中心 已经成功读取到 github 上配置信息
成功实现了客户端3355访问Spring Config3344配置中心通过Github获取配置信息
分布式配置动态刷新问题
配置动态刷新问题是什么
- 当我们修改GitHub上的配置文件的内容时候
- 我们的config配置服务器中心
有立即响应
- 我们的config 客户端也
没有立即响应
- 客户端没有立刻响应,除非自己重启或者重新加载
动态刷新问题解决
- 在config 客户端添加监控依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
-
修改yml配置,暴露监控端点
# 暴露监控端点 management: endpoints: web: exposure: include: "*"
-
在业务类Controllert添加注解
@RefreshScope
@RefreshScope @RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
其实经过以上的配置修改,其实我们的config 客户端
还是不能
实现动态刷新 -
我们还需要
发送一个post请求到 config 客户端
刷新 config 客户端、
必须是post请求:http://localhost:3355/actuator/refresh
(客户端uri/actuator/refresh)
“http://localhost:3355/actuator/refresh”
经过以上的步骤才能实现config 客户端的配置动态刷新(避免了config客户端重启)
动态刷新与spring cloud Bus
-
其实上述实现自动刷新需要我们手动发送一个post请求到config客户端,
-
那么我们有没有办法实现github配置修改自动通知到config客户端呢,一次通知处处生效?
-
需要实现这样的功能我们需要
结合 spring cloud Bus 消息总线来实现