SpringCloud——消息總線 SpringCloud Bus
標簽: SpringCloud 分布式 rabbitmq java
1. 概述
Spring Cloud Bus 配合 Spring Cloud Config 使用可以實現配置的動態刷新。
Spring Cloud Bus 是用來將分布式系統的節點與輕量級消息系統鏈接起來的框架,它整合了 Java 的事件處理機制和消息中間件的功能。
Spring Clud Bus目前支持 RabbitMQ 和 Kafka。
Spring Cloud Bus 能管理和傳播分布式系統間的消息,就像一個分布式執行器, 可用于廣 播狀態更改、事件推送等,也可以當作微服務間的通信通道
什么是總線:
在微服務架構的系統中,通常會使用輕量級的消息代理來構建一個共用的消息主題, 并讓系統中所有微服務實例都連接上來。由于該主題中產生的消息會被所有實例監聽和消費,所以稱它為消息總線。在總線上的各個實例,都可以方便地廣播一些需要讓其他連接在該主題上的實例都知道的消息。
基本原理:
ConfigClient 實例都監聽MQ中同一個topic(默認是springCloudBus)。當一個服務刷新數據的時候,它會把這個信息放入到Topic中,這樣其它監聽同一Topic的服務就能得到通知,然后去更新自身的配置。
2. 動態刷新全局廣播
2.1 設計思路
思路一:利用消息總線觸發一個客戶端/bus/refresh,而刷新所有客戶端的配置(圖1 模式)
思路二:利用消息總線觸發一個服務端 ConfigServer 的 /bus/refresh 端點,而刷新所有客戶端的配置(圖2模式)
思路一打破了微服務的職責單一性,因為微服務本身是業務模塊,它本不應該承擔配置刷新職責,破壞了微服務各節點的對等性。所以選用思路二。
2.2 實現
準備三個微服務(一個服務端3344,兩個客戶端3355、3366)。
2.2.1 服務端
pom:
<dependencies>
<!--添加消息總線支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--分布式配置支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監控支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
application.yml:
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: [email protected]:Zhangtao153/springcloud-config.git
search-paths:
- springcloud-config
label: master
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露 bus 刷新配置的端點
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
啟動類:
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args){
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
2.2.2 客戶端
pom:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
bootstrap.yml:
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支
name: config # 配置文件名
profile: dev # 環境
uri: http://localhost:3344
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
啟動類:
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args){
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
業務類:
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${server.port}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return "serverPort:"+serverPort+"\t\n\n configInfo: "+configInfo;
}
}
2.2.3 測試
修改 github 配置文件
發送 post 請求: curl -X POST "http://localhost:3344/actuator/bus-refresh"
配置中心:http://localhost:3344/master/config-dev.yml
客戶端: http://localhost:3355/configInfo
http://localhost:3366/configInfo
3. 動態刷新定點通知
指定具體某一個實例生效而不是全部。比如只通知 3355,不通知3366。
通知方式:
http://localhost:配置中心的端口號/actuator/bus-refresh/{destination}
/bus/refresh 請求不再發送到具體的服務實例上,而是發給config server并通過destination參數類指定需要更新配置的服務或實例。
3.1 案例
通知 3355而不通知 3366。
修改 github 配置文件。
發送 post 請求: curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
配置中心:http://localhost:3344/master/config-dev.yml 已更新
客戶端: http://localhost:3355/configInfo 已更新
http://localhost:3366/configInfo 未更新
智能推薦
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...
Linux C系統編程-線程互斥鎖(四)
互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...
統計學習方法 - 樸素貝葉斯
引入問題:一機器在良好狀態生產合格產品幾率是 90%,在故障狀態生產合格產品幾率是 30%,機器良好的概率是 75%。若一日第一件產品是合格品,那么此日機器良好的概率是多少。 貝葉斯模型 生成模型與判別模型 判別模型,即要判斷這個東西到底是哪一類,也就是要求y,那就用給定的x去預測。 生成模型,是要生成一個模型,那就是誰根據什么生成了模型,誰就是類別y,根據的內容就是x 以上述例子,判斷一個生產出...
猜你喜歡
styled-components —— React 中的 CSS 最佳實踐
https://zhuanlan.zhihu.com/p/29344146 Styled-components 是目前 React 樣式方案中最受關注的一種,它既具備了 css-in-js 的模塊化與參數化優點,又完全使用CSS的書寫習慣,不會引起額外的學習成本。本文是 styled-components 作者之一 Max Stoiber 所寫,首先總結了前端組件化樣式中的最佳實踐原則,然后在此基...
19.vue中封裝echarts組件
19.vue中封裝echarts組件 1.效果圖 2.echarts組件 3.使用組件 按照組件格式整理好數據格式 傳入組件 home.vue 4.接口返回數據格式...
【一只蒟蒻的刷題歷程】【藍橋杯】歷屆試題 九宮重排 (八數碼問題:BFS+集合set)
資源限制 時間限制:1.0s 內存限制:256.0MB 問題描述 如下面第一個圖的九宮格中,放著 1~8 的數字卡片,還有一個格子空著。與空格子相鄰的格子中的卡片可以移動到空格中。經過若干次移動,可以形成第二個圖所示的局面。 我們把第一個圖的局面記為:12345678. 把第二個圖的局面記為:123.46758 顯然是按從上到下,從左到右的順序記錄數字,空格記為句點。 本題目的任務是已知九宮的初態...