Java Spring Cloud Bus 实现配置实时更新详解
背景
使用Spring Cloud Config Server,
启动Service时会从配置中心取配置文件,并注入到应用中,如果在Service运行过程中想更新配置,需要使用Spring Cloud Bus配合实现实时更新。
实现原理
需要借助RabbitMQ等消息中间件来实现服务间的通讯
ConfigServer改造
目标:使ConfigServer暴露bus-refresh接口,通过bus通知服务更新配置
1. pom.xml增加以下依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. 配置文件中配置暴露接口
management.endpoints.web.exposure.include=bus-refresh
Service改造
1. pom.xml增加以下依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. 通过@RefreshScope声明配置刷新时需要重新注入
@RefreshScope @Controller public class LblController {
测试
1. 修改git仓库上的配置文件
Service配置没有更新
2. 调用http://localhost:8081/actuator/bus-refresh(POST)
Service配置更新
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
相关文章
基于selenium-java封装chrome、firefox、phantomjs实现爬虫
这篇文章主要介绍了基于selenium-java封装chrome、firefox、phantomjs实现爬虫,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2020-10-10Springboot AOP对指定敏感字段数据加密存储的实现
本篇文章主要介绍了利用Springboot+AOP对指定的敏感数据进行加密存储以及对数据中加密的数据的解密的方法,代码详细,具有一定的价值,感兴趣的小伙伴可以了解一下2021-11-11spring @Transactional注解中常用参数详解
这篇文章主要介绍了spring @Transactional注解中常用参数详解,事物注解方式: @Transactional,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下2024-02-02
最新评论