Java Spring Cloud Bus 实现配置实时更新详解

网友投稿 296 2022-12-08

Java Spring Cloud Bus 实现配置实时更新详解

目录背景实现原理ConfigServer改造1. pom.xml增加以下依赖2. 配置文件中配置暴露接口Service改造1. pom.xml增加以下依赖2. 通过@RefreshScope声明配置刷新时需要重新注入测试总结

背景

使用Spring Cloud Config Server,启动Service时会从配置中心取配置文件,并注入到应用中,如果在Service运行过程中想更新配置,需要使用Spring Cloud Bus配合实现实时更新。

实现原理

需要借助RabbitMQ等消息中间件来实现服务间的通讯

ConfigServer改造

目标:使ConfigServer暴露bus-refresh接口,通过http://bus通知服务更新配置

1. pom.xml增加以下依赖

org.springframework.cloud

spring-cloud-starter-bus-amqp

org.springframework.boot

spring-boot-starter-actuator

2. 配置文件中配置暴露接口

management.endpoints.web.exposure.include=bus-refresh

Service改造

1. pom.xml增加以下依赖

org.springframework.cloud

spring-cloud-starter-bus-amqp

org.springframework.boot

spring-boot-starter-actuator

2. 通过@RefreshScope声明配置刷新时需要重新注入

@RefreshScope

@Controller

public class LblController {

测试

1. 修改git仓库上的配置文件

Service配置没有更新

2. 调用http://localhost:8081/actuator/bus-refresh(POST)

Service配置更新

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:利用Java实现红黑树
下一篇:详解MyBatis resultType与resultMap中的几种返回类型
相关文章

 发表评论

暂时没有评论,来抢沙发吧~