Bus 服務總線
Spring Cloud Bus 是輕量的訊息代理,將分布式節點連接,用于廣播組態檔的更新 或 監控管理
補充說明&優點
- Bus 是輕量級訊息代理
- Bus 主要實作 配置的自動更新 (無需配置)
- Bus 底層基于 RabbitMQ實作
- 廣播組態檔、監控管理
存在問題
上篇文章的 Config配置中心 啟動后,此時更改 遠端倉庫 中某一微服務的組態檔,是不會同步本地快取中的組態檔,因此在某一微服務運行后是不會及時更新組態檔,而是需要重啟微服務才能更新生效
問題示例
- 以下主要展示 更改配置不會同步的問題
- 上篇章代碼測驗
-
修改倉庫
user-service微服務的組態檔user-dev.yml添加
test.name屬性 (用于測驗)
依舊應用
user-service進行測驗 -
修改Controller
user-service(在介面進行測驗回傳資料)@RestController @RequestMapping ("user") public class UserController { @Autowired private UserService service; @Value("${test.name}") private String name; @GetMapping ("name") public String testConfig() { return name; } //··· }添加一個介面用于回傳配置中的屬性name的介面
-
運行測驗1(查看剛剛在倉庫中添加的屬性)
依次啟動Eureka、config-server、user-server三個服務
訪問 http://localhost:9091/user/name ,下圖回傳表示成功
-
修改倉庫
user-service微服務的組態檔user-dev.yml
主要修改test.name屬性的值# 其他就不展示了 test : name : Sanscan12 -
瀏覽器再次訪問 http://localhost:9091/user/name
此時 配置中的
test.name屬性值沒有更變 -
重啟
user-service微服務 ,再次訪問http://localhost:9091/user/nameGit倉庫中組態檔的修改并沒有及時更新到 微服務,只有重啟微服務才能生效
Bus應用
Bus 構架圖

說明:
- 應用前提需要安裝
RabbitMQ才能實作功能 - 配置中心 需要自行暴露觸發總線地址 /actuator/bus-refresh
- 請求 /actuator/bus-refresh 是固定的
- 當配置中心微服務接收到 /actuator/bus-refresh 請求時,向
RabbitMQ發送配置改變訊息,這時也會更新本地快取 - 微服務 會監聽
RabbitMQ,如果堅監聽到有倉庫配置改變的訊息,會重新讀取本地快取中的組態檔(實作不用重啟提醒更新的效果)
應用示例
-
首先安裝
RabbitMQ,安裝即可無需配置 -
config-server添加 Bus依賴<!-- Bus --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> -
config-server添加配置application.yml# ... spring : # ··· # 配置 rabbitmq資訊,如果都是與默認值一致無需配置 rabbitmq : host : localhost port : 5672 username : guest password : guest management : endpoints : web : exposure : # 暴露觸發訊息總線的地址 # POST /actuator/bus-refresh include : bus-refresh訊息總線的地址,主要用途在觸發更新配置的請求!
-
user-service添加依賴 Bus、監控管理<!-- Bus 監聽 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> -
user-service添加配置bootstrap.yml(配置與默認一致可跳過)# .... # 配置 rabbitmq資訊,如果都是與默認值一致無需配置 rabbitmq : host : localhost port : 5672 username : guest password : guest -
user-service修改UserController添加注解@RefreshScope@RestController @RequestMapping ("user") @RefreshScope // 自動重繪組態檔 public class UserController { // .... } -
測驗
- 依次啟動
Eureka、config-server、user-server三個服務 - 訪問 http://localhost:9091/user/name ,查看頁面回傳的屬性值
- 修改 Git倉庫
user-service微服務的組態檔user-dev.yml
主要修改test.name屬性的值 (值可以隨意,自要展示效果即可) - POST請求:http://localhost:14000/actuator/bus-refresh (配置中心的埠 14000)觸發訊息總線進行重繪配置
- 重新訪問 http://localhost:9091/user/name (回傳更改后的值表示成功)
說明: POST請求可通過 IDEA內置HTTP呼叫 (善用Ctrl+F)
- 依次啟動
倉庫代碼 : https://gitee.com/Sanscan12/spring-cloud-examples.git
官方檔案 : Spring Cloud Bus 參考手冊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350846.html
標籤:其他
