Spring Cloud Config
介绍
Spring Cloud Config是一个分布式的配置管理方案,分为Server端和客户端。Server端在项目中一般是一个独立的moudule,主要是与配置仓库交互,从仓库中获取配置文件;而客户端一般是与其它服务配合使用,从Server端中获取配置文件。当我们修改了仓库中的配置文件的时候,Server端中的配置文件会自动更新,而客户端中的配置文件默认情况下需要进行手动刷新。
基本流程
 本地Git:当config-client请求配置信息时,首先会从远程Git上获取最新的配置到本地Git,然后从本地Git中读取并返回,如果远程Git不能使用,则直接读取本地Git的内容。 应用A、应用B:这些应用中包含config-client,当应用启动时会去config-server中请求加载配置文件。
我们将配置文件放在远程git上,config server启动时会将远程的配置文件保存在本地,然后config client会从config server上读取配置文件;config server与远程git是双向的,config server会把从远程git读取的配置文件保存在本地,即使远程git出了问题也可以正常使用。
使用
首先创建一个module,需要导入Eureka和Config的依赖。由于这里演示的是微服务,所以会把Spring Cloud Config和Eureka一起使用,将Spring Cloud Config的Server端和客户端注册到服务中心中。但是Spring Cloud Config也是可以单独使用的。
config配置文件的命名规则

导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
配置git
由于Spring Cloud Config的Server端是与仓库进行交互的,所以需要在配置文件中配置仓库地址等信息。下面的代码没有给出Eureka的配置,只给出了Spring Cloud Config的配置。
uri:仓库的地址,本人使用的是码云; username:登录用户名; password:登录密码; default-label:读取的分支; search-paths:
server:
port: 8096
spring:
application:
name: config
cloud:
config:
server:
git:
password: xxxx
uri: https://gitee.com/xxxx/spring-cloud-config_repo.git
username: xxxx
default-label: master
search-paths: spring-cloud-config_repo
management:
endpoints:
jmx:
exposure:
include: "*"
endpoint:
health:
show-details: always
配置Config
在启动类上使用@EnableConfigServer注解。
结果
在浏览器上输入http://localhost:8096/分支/配置文件名称,即可看到git仓库中配置文件的内容。 
config client使用
这里面一般会把config client中的application.yml文件改为bootstrap.yml文件,bootstrap文件是系统级别的,优先级比application.yml文件高,应用启动时会检查这个配置文件,在这个配置文件中指定config server的地址,会自动拉取所有配置并且启用。主要是要把与config server相关的配置信息放入其中。
导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
配置
spring:
cloud:
config:
name: lagou-service-resume
profile: dev
label: master
uri: http://localhost:8096
到这里,基本就完成了分布式配置中心的基本使用了,但是会有一个问题,当我们修改远程git仓库,config server可以获取到最新的配置文件的值,但是config client读取的是缓存,无法实时获取最新的值,Sprig Cloud为我们提供了解决方案,那就是使用Post去触发refresh,获取最新数据。
手动刷新config client中配置文件的值
1、config client添加依赖springboot-starter-actuator; 2、client客户端bootstrap.yml中添加配置(暴露通信端点);
management:
endpoints:
jmx:
exposure:
include: refresh
3、client客户端使?到配置信息的类上添加@RefreshScope; 4、向client客户端发起POST请求,http://localhost:8080/actuator/refresh,刷新配置信息;
Spring Cloud config + Spring Cloud Bus自动刷新
原理如下: 
config端配置
rabbitmq配置
rabbitmq:
addresses:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
jmx:
exposure:
include: "*"
重启各个服务,更改配置之后,向配置中?服务端发送post请求http://localhost:9003/actuator/bus-refresh,各个客户端配置即可自动刷新,在?播模式下实现了?次请求,处处更新;
如果需要定向更新,在发起刷新请求的时候,使用 http://localhost:9006/actuator/bus-refresh/lagouservice/resume:8081, 即url后面加上要定向刷新的实例的服务名:端口号即可;
参考
- 视频课程
- 《Spring Boot + Spring Cloud微服务开发实战》
|