Java Spring Cloud Bus 实现配置实时更新详解
更新时间:2021年09月15日 09:37:51 作者:lbl2018
这篇文章主要介绍了SpringCloud 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配置更新
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
相关文章
springboot + devtools(热部署)实例教程
devtools是boot的一个热部署工具,当我们修改了classpath下的文件(包括类文件、属性文件、页面等)时,会重新启动应用。本文通过实例给大家介绍springboot+devtools热部署,感兴趣的朋友一起看看吧2017-04-04
Java微服务Filter过滤器集成Sentinel实现网关限流过程详解
这篇文章主要介绍了Java微服务Filter过滤器集成Sentinel实现网关限流过程,首先Sentinel规则的存储默认是存储在内存的,应用重启之后规则会丢失。因此我们通过配置中心Nacos保存规则,然后通过定时拉取Nacos数据来获取规则配置,可以做到动态实时的刷新规则2023-02-02


最新评论