SpringCloud【Greenwich版本】第五章配置中心(
序言
上一章节我们已经学习了路由网关和链路跟踪,其实SpringCloud最基本也是最核心的也基本学习完毕。本章主要想和大家分享下SpringCloud的Config和Bus,一块是配置集中管理,另一块消费通知,分发,能很好管控微服务的配置以及提高各个微服务之间的消息收发的效率。
Spring Clound Config 简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在这个业务场景下,Spring Cloud为我们提供了分布式配置中心组件Spring Cloud Config。
特点如下:
- 支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。
- 在spring cloud config 组件中,分两个角色(Config Server 和 Config Client)
tip: 本章演示用的远程git库的方式进行统一管理配置
准备工作
拷贝上一章节中的Eureka Server 和 feign-client-1,并启动Eureka Server
创建Config Server
创建一个project,取名为config-server,选择Cloud Config,再勾选上Config Server,点击完成即可
![](https://img.haomeiwen.com/i5193475/0cf05504678d9cbc.png)
添加ConfigServer注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
添加配置
- 首先在git库第五章节文件夹下面创建config文件夹,并创建application-dev.properties文件,添加参数内容
test.word=config client hello world
- 接着项目的application.properties里面添加远端git库的访问配置
eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
spring.application.name=configserver
server.port=8101
#git repo的url地址
spring.cloud.config.server.git.uri=https://github.com/lenvonsam/spring-cloud-training
spring.cloud.config.server.git.search-paths=/chapter-fifth/config
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
![](https://img.haomeiwen.com/i5193475/7c60f74ac0dfe43f.png)
【配置说明】
- git.uri=git库项目访问路径
- git.search=配置文件对应的目录
- git.label=配置文件所在项目分支
- git.username和git.password是针对私有的git库需要输入用户名和密码,但我们演示的是公开项目库,不需要输入用户名和密码。
启动服务
浏览器输入http://localhost:8101/application-dev.properties
![](https://img.haomeiwen.com/i5193475/38de818b75e15c86.png)
能达到上图效果,恭喜您Config Server已成功搭建成功_
创建Config Client
打开之前准备好的feign-client-1项目,添加spring client config的pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在resources资源目录下创建bootstrap.properties文件(一定要创建此文件,在application里面写配置无效),并添加访问配置
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8101/
spring.cloud.config.discovery.service-id=configserver
spring.cloud.config.fail-fast=true
spring.cloud.config.discovery.enabled=true
【配置说明】
spring cloud config 默认的请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
根据上述映射规则,本章项目演示对于的值如下
- spring.cloud.config.profile=dev
- spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8101/ (指明配置服务中心的网址)
spring.cloud.config.discovery.service-id=configserver (指明在eureka server中注册的服务中心应用名称-configserver)
spring.cloud.config.discovery.enabled (指明能否从配置中心读取文件)
在HelloController中添加访问代码块
@Value("${test.word}")
private String testWord;
@GetMapping("testWord")
public String testWord() {
return "remote git repo test.word value:>>>" + testWord;
}
启动项目
浏览器中访问 http://localhost:8790/client/testWord
![](https://img.haomeiwen.com/i5193475/b4fb95dbf1d837b5.png)
如果能达到上图效果,恭喜您Config Client 已经成功整合到原有项目中 _
Spring Cloud Bus 简介
在我们的实战业务开发中,肯定会碰到项目参数、端口统一管理、统一配置的问题,这些spring clound config已经帮您解决,但还有个问题就是改了配置后,传统的方法只能重新启动原有的服务器的,如果大项目重新肯定要耗费较多重启时间,这样大大增加了代码的复用性。
Spring Cloud Bus 是将分布式的节点用轻量的消息代理连接起来的模块。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。
本章节要使用的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。
准备工作
- 下载rabbitmq,并能正常运行(参考文献)
- 启动rabbitmq server
mac$ cd rabbitmq-server
mac$ cd sbin
mac$ ./rabbitmq-server
【服务正常启动的效果图片】
![](https://img.haomeiwen.com/i5193475/28b550185d0ae851.png)
改造Feign-client-1项目
添加bus的pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
添加文件配置
在application.properties或者bootstrap.properties里面添加bus的参数配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
【配置说明】
- spring.rabbitmq.* (rabbitmq基本配置,里面有默认的guest用户)
- spring.cloud.bus.enabled (是否启用bus消息通知)
- spring.cloud.bus.trace.enabled (是否启用bus消息追踪)
- management.endpoints.web.exposure.include=bus-refresh (暴露http网络访问路径以bus-refresh结尾)
添加注解
@RefreshScope
public class HelloController {}
启动项目,测试效果
- 登录远端git库,在原有的值后面添加first time,然后保存
test.word=config client hello world first time
- 打开shell命令窗,输入
curl -v -X POST http://localhost:8790/actuator/bus-refresh
【运行截图】
![](https://img.haomeiwen.com/i5193475/b3f70b0773883ccc.png)
【工程项目截图】
![](https://img.haomeiwen.com/i5193475/ad0585fe93237346.png)
- 再次刷新之前访问的浏览窗
![](https://img.haomeiwen.com/i5193475/36b36daec6e2eb30.png)
出现上图,说明消息总线已经通知config client 成功,到此本章内容已经全部学完 _
项目示例地址
https://github.com/lenvonsam/spring-cloud-training/tree/master/chapter-fifth