『互联网架构』软件架构-分布式集中配置中心Spring Clou
原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
原文链接地址:『互联网架构』软件架构-分布式集中配置中心Spring Cloud Config详解(下)(104)
上次咱们主要说下,如果制作server,client端如何获取,而且还说了加密和解密。这次咱们说说动态刷新配置,消息队列如何配置分布式系统统一配置和高可用。
源码:https://github.com/limingios/netFuture/tree/master/源码/『互联网架构』软件架构-分布式集中配置中心Spring Cloud Config详解(上)(103)/
![](https://img.haomeiwen.com/i11223715/45ef6455ab18f69b.png)
(一)配置信息手动刷新
很多场景下,需要在运行期间动态调整配置。如果配置发生了修改,微服务要如何实现配置的刷新呢?重点都是开关值这块,一开始认为开关可以走的,后来发现流量太大了,必须把开关关闭。动态的改内存中的值。
- 源码
10-ms-config-client-refresh
![](https://img.haomeiwen.com/i11223715/bee302c791c25149.png)
- 添加依赖
其中spring-boot-starter-actuator提供了/refresh端点,用于配置的刷新
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
![](https://img.haomeiwen.com/i11223715/63b171a01868fda6.png)
- 在Controller上添加注解@RefreshScope,添加这个注解的类会在配置更改时得到特殊的
处理
![](https://img.haomeiwen.com/i11223715/bbffb7cc6cfc347f.png)
- 演示功能
启动10-ms-config-server项目
![](https://img.haomeiwen.com/i11223715/e36727d9682b86c1.png)
启动10-ms-config-client-refresh
1.启动项目(启动两个,一个端口8081,一个端口8082),
访问地址:http://localhost:8081/profile,
得到结果: dev-1.0,
访问地址:http://localhost:8082/profile,
得到结果: dev-1.0
![](https://img.haomeiwen.com/i11223715/928f39e875fdfef4.png)
![](https://img.haomeiwen.com/i11223715/d170c9529f68b543.png)
![](https://img.haomeiwen.com/i11223715/f2a030dae28f86ed.png)
![](https://img.haomeiwen.com/i11223715/e70384364d3ee2c8.png)
2.修改git仓库里的配置文件ms-config-dev.properties的内容为:
profile=dev-1.0-change
![](https://img.haomeiwen.com/i11223715/06946bb693c2dd60.png)
3.再次访问地址:http://localhost:8081/profile,得到结果还是 dev-1.0,说明配置尚未刷新
![](https://img.haomeiwen.com/i11223715/1af425b0ee97c14d.png)
4.发送post请求:http://localhost:8081/refresh,返回结果:"profile",表
示profile这个配置属性已被刷新
![](https://img.haomeiwen.com/i11223715/3bc15319974d09f8.png)
5.再次访问 http://localhost:8081/profile,得到结果: dev-1.0-change,说明属性已刷新
![](https://img.haomeiwen.com/i11223715/d66780f24b9bcb78.png)
6.再次访问 http://localhost:8082/profile,得到结果: dev-1.0,说明8082的服务
并没有刷新,还需再次手动刷新才能更新配置
![](https://img.haomeiwen.com/i11223715/13c4b5cc47509f51.png)
(二)配置信息自动刷新
使用/refresh端点手动刷新配置,但如果所有微服务节点的配置都需要手动去刷新,工作量可想而知。不仅如此,随着系统的不断扩张,会越来越难以维护。因此,实现配置的自动刷新是很有必要的,Spring Cloud Bus就可以实现配置的自动刷新。Spring Cloud Bus使用轻量级的消息代理(例如 RabbitMQ、 Kafka等)连接分布式系统的节点,这样就可以广播传播状态的更改(例如配置的更新)或者其他的管理指令。可将Spring Cloud Bus想象成一个分布式Spring Boot Actuator。
![](https://img.haomeiwen.com/i11223715/1edca479629c5069.png)
- 源码
10-ms-config-server-refresh-cloud-bus和10-ms-config-client-refresh-cloud-bus
![](https://img.haomeiwen.com/i11223715/4f1d770f8403c5fd.png)
![](https://img.haomeiwen.com/i11223715/3cdf36f619362075.png)
- 服务端 10-ms-config-server-refresh-cloud-bus 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
![](https://img.haomeiwen.com/i11223715/0422928e03a8942b.png)
- 服务端 10-ms-config-server-refresh-cloud-bus 配置文件增加rabbitmq
![](https://img.haomeiwen.com/i11223715/ca172da6fc736579.png)
- 客户端 10-ms-config-client-refresh-cloud-bus 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
![](https://img.haomeiwen.com/i11223715/3c6fe7a0c8e818a0.png)
- 客户端 10-ms-config-client-refresh-cloud-bus 配置文件增加rabbitmq
![](https://img.haomeiwen.com/i11223715/ad55bb7e2c949fe3.png)
- rabbitmq docker的方式安装
docker run -d -p 5672:5672 -p15672:15672 --name myrabbitmq rabbitmq
docker ps
![](https://img.haomeiwen.com/i11223715/b2c950708141844d.png)
![](https://img.haomeiwen.com/i11223715/abe327aaf2453188.png)
运行项目(运行一个config server和两个config client),修改git仓库里的配置文件,然后用post方式请求地址:http://localhost:8080/bus/refresh,如果返回成功,则config的所有客户端的配置都会动态刷新。【里面关于rabbitmq的地址配置根据实际情况配置吧】
一个config server
![](https://img.haomeiwen.com/i11223715/3b97c246ba84318d.png)
两个config client
![](https://img.haomeiwen.com/i11223715/3d2fc88b3971b6fd.png)
![](https://img.haomeiwen.com/i11223715/8c4b469de6ab5910.png)
![](https://img.haomeiwen.com/i11223715/3b68655d681636d6.png)
![](https://img.haomeiwen.com/i11223715/fb18550a67e3088e.png)
修改git仓库里的配置文件
![](https://img.haomeiwen.com/i11223715/a2c5054b76732936.png)
post方式请求地址:http://localhost:8080/bus/refresh,如果返回成功
![](https://img.haomeiwen.com/i11223715/84d3abc3bb29cd7e.png)
客户端动态刷新后
![](https://img.haomeiwen.com/i11223715/5022740c6d63721d.png)
![](https://img.haomeiwen.com/i11223715/e908bc9c654824e2.png)
通过mq的方式动态的管理分布式的所有系统刷新配置文件,是不是很爽。
(三)config的安全认证
- 源码
10-ms-config-server-authenticating和10-ms-config-client-authenticating
![](https://img.haomeiwen.com/i11223715/de21ec6818f2e3e1.png)
![](https://img.haomeiwen.com/i11223715/85e57b743ba647ff.png)
- 10-ms-config-server-authenticating 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
![](https://img.haomeiwen.com/i11223715/d692e20dc63878f3.png)
- 10-ms-config-server-authenticating application.yml配置
![](https://img.haomeiwen.com/i11223715/ac69f398cfff2589.png)
- 10-ms-config-client-authenticating 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
![](https://img.haomeiwen.com/i11223715/4bf5563f94d6c5dd.png)
- 10-ms-config-client-authenticating application.yml配置
![](https://img.haomeiwen.com/i11223715/4fda754cf5dd2959.png)
首先服务端启动安全机制,用户名,密码。客户端连接的时候通过用户名和密码。
(四)config与eureka配合使用
10-ms-config-server-eureka,10-ms-config-client-eureka,08-ms-eureka-server
config配置中心的高可用
- config server未注册到eureka上的情况,通过负载均衡器来实现
- config server注册到eureka上的情况,client端也注册到eureka上,则已经实现高可用
![](https://img.haomeiwen.com/i11223715/6f8c888e68fdc2c6.png)
PS:基本上springcloudconfig的配置已经讲完了,这次的比上次更加实用,高可用,高性能。已经凌晨2点了有点太累了,不总结了,希望各位老铁可以get到。