跟我學SpringCloud | 第八篇:Spring Cloud Bus 消息總線

SpringCloud系列教程 | 第八篇:Spring Cloud Bus 消息總線

Springboot: 2.1.6.RELEASEhtml

SpringCloud: Greenwich.SR1git

如無特殊說明,本系列教程全採用以上版本github

前面兩篇文章咱們聊了Spring Cloud Config配置中心,當咱們在更新github上面的配置之後,若是想要獲取到最新的配置,須要手動刷新或者利用webhook的機制每次提交代碼發送請求來刷新客戶端,客戶端愈來愈多的時候,須要每一個客戶端都執行一遍,這種方案就不太適合了。使用Spring Cloud Bus(國人很形象的翻譯爲消息總線,我比較喜歡叫消息巴士)能夠完美解決這一問題。web

1. Spring Cloud Bus

Spring cloud bus經過輕量消息代理鏈接各個分佈的節點。這會用在廣播狀態的變化(例如配置變化)或者其餘的消息指令。Spring bus的一個核心思想是經過分佈式的啓動器對spring boot應用進行擴展,也能夠用來創建一個多個應用之間的通訊頻道。目前惟一實現的方式是用AMQP消息代理做爲通道,一樣特性的設置(有些取決於通道的設置)在更多通道的文檔中。spring

你們能夠將它理解爲管理和傳播全部分佈式項目中的消息既可,其實本質是利用了MQ的廣播機制在分佈式的系統中傳播消息,目前經常使用的有Kafka和RabbitMQ。利用bus的機制能夠作不少的事情,其中配置中心客戶端刷新就是典型的應用場景之一,咱們用一張圖來描述bus在配置中心使用的機制。bootstrap

根據此圖咱們能夠看出利用Spring Cloud Bus作配置更新的步驟:瀏覽器

  1. 提交代碼觸發post給客戶端A發送bus/refresh
  2. 客戶端A接收到請求從Server端更新配置而且發送給Spring Cloud Bus
  3. Spring Cloud bus接到消息並通知給其它客戶端
  4. 其它客戶端接收到通知,請求Server端獲取最新配置
  5. 所有客戶端均獲取到最新的配置

2. 項目示例

咱們使用上一篇文章中的config-server和config-client來進行改造,mq使用rabbitmq來作示例。springboot

2.1 客戶端config-client

2.1.1 添加依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

須要多引入spring-cloud-starter-bus-amqp包,增長對消息總線的支持併發

2.1.2 配置文件 bootstrap.properties

spring.application.name=spring-cloud-config-client
server.port=8081

spring.cloud.config.name=springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

management.endpoints.web.exposure.include=*

## 開啓消息跟蹤
spring.cloud.bus.trace.enabled=true

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=
spring.rabbitmq.password=

配置文件須要增長RebbitMq的相關配置,這樣客戶端代碼就改造完成了。app

2.1.3 測試

依次啓動eureka,config-serve,config-client。

修改config-client啓動配置,同時在8081和8082端口啓動服務。

啓動完成後,瀏覽器分別訪問鏈接:http://localhost:8081/hellohttp://localhost:8082/hello, 能夠發現頁面顯示的內容都是:hello dev update1,說明客戶端都已經讀取到了server端的內容。

如今咱們更新github上的配置文件,將配置內容改成hello dev update,先訪問一下http://localhost:8081/hello,能夠看到頁面依然顯示爲:hello dev update1。

咱們對端口爲8081的服務發送一個/actuator/bus-refresh的POST請求,在win10下使用下面命令來模擬webhook。

curl -X POST http://localhost:8081/actuator/bus-refresh

注意: 在springboot2.x的版本中刷新路徑爲:/actuator/bus-refresh,在springboot1.5.x的版本中刷新路徑爲:/bus/refresh。

執行完成後,咱們先訪問http://localhost:8082/hello,能夠看到頁面打印內容已經變爲:hello dev update,這樣說明,咱們8081端口的服務已經把更新後的信息經過rabbitmq推送給了8082端口的服務,這樣咱們就實現了圖一中的示例。

2.2 改進版

上面的流程中,雖然咱們作到了利用一個消息總線觸發刷新,而刷新全部客戶端配置的目的,可是這種方式並不合適,以下:

  • 打破了微服務的職責單一性。微服務自己是業務模塊,它本不該該承擔配置刷新的職責。
  • 破壞了微服務各節點的對等性。
  • 若是客戶端ip有變化,這時咱們就須要修改WebHook的配置。

咱們能夠將上面的流程改進一下:

這時Spring Cloud Bus作配置更新步驟以下:

  1. 提交代碼觸發post給Server端發送bus/refresh
  2. Server端接收到請求併發送給Spring Cloud Bus
  3. Spring Cloud bus接到消息並通知給其它客戶端
  4. 其它客戶端接收到通知,請求Server端獲取最新配置
  5. 所有客戶端均獲取到最新的配置

這樣的話咱們在server端的代碼作一些改動,來支持/actuator/bus-refresh

和上面的client端的改動基本一致

2.2.1 添加依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

須要多引入spring-cloud-starter-bus-amqp包,增長對消息總線的支持

2.2.2 配置文件application.yml

server:
  port: 8080
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/meteor1993/SpringCloudLearning
          search-paths: chapter6/springcloud-config
          username: 
          password: 
  rabbitmq:
    host: 217.0.0。1
    port: 5672
    username: 
    password: 
management:
  endpoints:
    web:
      exposure:
        include: "*"
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

配置文件須要增長RebbitMq的相關配置,actuator開啓全部訪問。

2.2.3 測試

依次啓動eureka,config-serve,config-client。

修改config-client啓動配置,同時在8081和8082端口啓動服務。

按照上面的測試方式,訪問兩個客戶端測試都可以正確返回信息。一樣修改配置文件,將值改成:hello im dev update並提交到倉庫中。在win10下使用下面命令來模擬webhook。

curl -X POST http://localhost:8081/actuator/bus-refresh

執行完成後,依次訪問兩個客戶端,返回:hello im dev update。說明三個客戶端均已經拿到了最新配置文件的信息,這樣咱們就實現了上圖中的示例。

示例代碼-Github

參考:http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html

相關文章
相關標籤/搜索