为了账号安全,请及时绑定邮箱和手机立即绑定

软件架构-分布式集中配置中心Spring Cloud Config详解(下)

标签:
Java

上次咱们主要说下,如果制作server,client端如何获取,而且还说了加密和解密。这次咱们说说动态刷新配置,消息队列如何配置分布式系统统一配置和高可用。

配置信息手动刷新(一)

很多场景下,需要在运行期间动态调整配置。如果配置发生了修改,微服务要如何实现配置的刷新呢?重点都是开关值这块,一开始认为开关可以走的,后来发现流量太大了,必须把开关关闭。动态的改内存中的值。

  • ① 源码

10-ms-config-client-refresh

  • ② 添加依赖

其中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>

  • ③ 在Controller上添加注解@RefreshScope,添加这个注解的类会在配置更改时得到特殊的处理

  • ④ 演示功能

启动10-ms-config-server项目

启动10-ms-config-client-refresh

1.启动项目(启动两个,一个端口8081,一个端口8082),
访问地址:http://localhost:8081/profile,
得到结果: dev-1.0,
访问地址:http://localhost:8082/profile,
得到结果: dev-1.0

2.修改git仓库里的配置文件ms-config-dev.properties的内容为:
profile=dev­-1.0-­change

3.再次访问地址:http://localhost:8081/profile,得到结果还是 dev-1.0,说明配置尚未刷新

4.发送post请求:http://localhost:8081/refresh,返回结果:“profile”,表
示profile这个配置属性已被刷新

5.再次访问 http://localhost:8081/profile,得到结果: dev-1.0-change,说明属性已刷新

6.再次访问 http://localhost:8082/profile,得到结果: dev-1.0,说明8082的服务
并没有刷新,还需再次手动刷新才能更新配置

配置信息自动刷新(二)

使用/refresh端点手动刷新配置,但如果所有微服务节点的配置都需要手动去刷新,工作量可想而知。不仅如此,随着系统的不断扩张,会越来越难以维护。因此,实现配置的自动刷新是很有必要的,Spring Cloud Bus就可以实现配置的自动刷新。Spring Cloud Bus使用轻量级的消息代理(例如 RabbitMQ、 Kafka等)连接分布式系统的节点,这样就可以广播传播状态的更改(例如配置的更新)或者其他的管理指令。可将Spring Cloud Bus想象成一个分布式Spring Boot Actuator。

  • ① 源码

10-ms-config-server-refresh-cloud-bus
10-ms-config-client-refresh-cloud-bus

  • ② 服务端 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>

  • ③ 服务端 10-ms-config-server-refresh-cloud-bus 配置文件增加rabbitmq

  • ④ 客户端 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>

  • ⑤ 客户端 10-ms-config-client-refresh-cloud-bus 配置文件增加rabbitmq

  • ⑥ rabbitmq docker的方式安装
docker run -d -p 5672:5672 -p15672:15672 --name myrabbitmq rabbitmq
docker ps

运行项目(运行一个config server和两个config client),修改git仓库里的配置文件,然后用post方式请求地址:http://localhost:8080/bus/refresh,如果返回成功,则config的所有客户端的配置都会动态刷新。【里面关于rabbitmq的地址配置根据实际情况配置吧】

一个config server

两个config client

修改git仓库里的配置文件

客户端动态刷新后

通过mq的方式动态的管理分布式的所有系统刷新配置文件,是不是很爽。

config的安全认证(三)

  • ① 源码

10-ms-config-server-authenticating
10-ms-config-client-authenticating

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lAV5GGnw-1635300314501)(https://upload-images.jianshu.io/upload_images/11223715-de21ec6818f2e3e1.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]

  • ② 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>

  • ③ 10-ms-config-server-authenticating application.yml配置

  • ④ 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>

  • ⑤ 10-ms-config-client-authenticating application.yml配置

首先服务端启动安全机制,用户名,密码。客户端连接的时候通过用户名和密码。

config与eureka配合使用(四)

10-ms-config-server-eureka,10-ms-config-client-eureka,08-ms-eureka-server
config配置中心的高可用

  1. config server未注册到eureka上的情况,通过负载均衡器来实现
  2. config server注册到eureka上的情况,client端也注册到eureka上,则已经实现高可用

PS:基本上springcloudconfig的配置已经讲完了,这次的比上次更加实用,高可用,高性能。已经凌晨2点了有点太累了,不总结了,希望各位老铁可以get到。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
1.7万
获赞与收藏
1318

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消