業余草 SpringCloud教程 | 第八篇: 消息總線(Spring Cloud Bus)(Finchley版本)


Spring Cloud Bus 將分布式的節點用輕量的消息代理連接起來。它可以用於廣播配置文件的更改或者服務之間的通訊,也可以用於監控。本文要講述的是用Spring Cloud Bus實現通知微服務架構的配置文件的更改。

一、准備工作

本文還是基於上一篇文章來實現。按照官方文檔,我們只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;這就是說我們需要裝rabbitMq,點擊rabbitmq下載。至於怎么使用 rabbitmq,搜索引擎下。

二、改造config-client

在pom文件加上起步依賴spring-cloud-starter-bus-amqp,完整的配置文件如下:

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-config</artifactId>
 5         </dependency>
 6  
 7         <dependency>
 8             <groupId>org.springframework.boot</groupId>
 9             <artifactId>spring-boot-starter-web</artifactId>
10         </dependency>
11  
12         <dependency>
13             <groupId>org.springframework.cloud</groupId>
14             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
15         </dependency>
16  
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
20         </dependency>
21  
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-actuator</artifactId>
25         </dependency>

在配置文件application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用戶名、密碼。並需要加上spring.cloud.bus的三個配置,具體如下:

1  
2 spring.rabbitmq.host=localhost
3 spring.rabbitmq.port=5672
4 spring.rabbitmq.username=guest
5 spring.rabbitmq.password=guest
6  
7 spring.cloud.bus.enabled=true
8 spring.cloud.bus.trace.enabled=true
9 management.endpoints.web.exposure.include=bus-refresh

ConfigClientApplication啟動類代碼如下:

 1 @SpringBootApplication
 2 @EnableEurekaClient
 3 @EnableDiscoveryClient
 4 @RestController
 5 @RefreshScope
 6 public class ConfigClientApplication {
 7  
 8     /**
 9      * http://localhost:8881/actuator/bus-refresh
10      */
11  
12     public static void main(String[] args) {
13         SpringApplication.run(ConfigClientApplication.class, args);
14     }
15  
16     @Value("${foo}")
17     String foo;
18  
19     @RequestMapping(value = "/hi")
20     public String hi(){
21         return foo;
22     }
23 }

依次啟動eureka-server、confg-cserver,啟動兩個config-client,端口為:8881、8882。

訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:

foo version 3

這時我們去代碼倉庫將foo的值改為“foo version 4”,即改變配置文件foo的值。如果是傳統的做法,需要重啟服務,才能達到配置文件的更新。此時,我們只需要發送post請求:http://localhost:8881/actuator/bus-refresh,你會發現config-client會重新讀取配置文件

Paste_Image.png

重新讀取配置文件:

Paste_Image.png

這時我們再訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:

foo version 4

另外,/actuator/bus-refresh接口可以指定服務,即使用”destination”參數,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服務名為customers的所有服務。

三、分析

此時的架構圖: 
Paste_Image.png

當git文件更改的時候,通過pc端用post 向端口為8882的config-client發送請求/bus/refresh/;此時8882端口會發送一個消息,由消息總線向其他服務傳遞,從而使整個微服務集群都達到更新配置文件。

本文源碼下載:

https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter8

五、參考資料

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

業余草微信公眾號

感謝您的關注!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群進群密碼:xttblog,想加微信群的朋友,可以微信搜索:xmtxtt,備注:“xttblog”,添加助理微信拉你進群。備注錯誤不會同意好友申請。再次感謝您的關注!后續有精彩內容會第一時間發給您!原創文章投稿請發送至532009913@qq.com郵箱。商務合作可添加助理微信進行溝通!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM