Java Spring Cloud Bus 實(shí)現(xiàn)配置實(shí)時(shí)更新詳解
背景
使用Spring Cloud Config Server,
啟動(dòng)Service時(shí)會(huì)從配置中心取配置文件,并注入到應(yīng)用中,如果在Service運(yùn)行過程中想更新配置,需要使用Spring Cloud Bus配合實(shí)現(xiàn)實(shí)時(shí)更新。
實(shí)現(xiàn)原理
需要借助RabbitMQ等消息中間件來實(shí)現(xiàn)服務(wù)間的通訊
ConfigServer改造
目標(biāo):使ConfigServer暴露bus-refresh接口,通過bus通知服務(wù)更新配置
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聲明配置刷新時(shí)需要重新注入
@RefreshScope @Controller public class LblController {
測試
1. 修改git倉庫上的配置文件
Service配置沒有更新
2. 調(diào)用http://localhost:8081/actuator/bus-refresh(POST)
Service配置更新
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解
這篇文章主要介紹了Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot實(shí)現(xiàn)阿里云快遞物流查詢的示例代碼
本文將基于springboot實(shí)現(xiàn)快遞物流查詢,物流信息的獲取通過阿里云第三方實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10如何在SpringBoot中使用Spring-AOP實(shí)現(xiàn)接口鑒權(quán)
這篇文章主要介紹了如何在SpringBoot中使用Spring-AOP實(shí)現(xiàn)接口鑒權(quán),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09Spring boot + LayIM + t-io 實(shí)現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實(shí)例代碼
這篇文章主要介紹了Spring boot + LayIM + t-io 實(shí)現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實(shí)例代碼,需要的朋友可以參考下2017-12-12Springboot實(shí)現(xiàn)人臉識(shí)別與WebSocket長連接的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot實(shí)現(xiàn)人臉識(shí)別與WebSocket長連接的實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11