欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Spring Cloud Bus 實現(xiàn)配置實時更新詳解

 更新時間:2021年09月15日 09:37:51   作者:lbl2018  
這篇文章主要介紹了SpringCloud Bus如何實現(xiàn)配置刷新,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

背景

使用Spring Cloud Config Server,啟動Service時會從配置中心取配置文件,并注入到應(yīng)用中,如果在Service運行過程中想更新配置,需要使用Spring Cloud Bus配合實現(xiàn)實時更新。

實現(xiàn)原理

需要借助RabbitMQ等消息中間件來實現(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聲明配置刷新時需要重新注入

@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)文章

最新評論