Spring Cloud Bus:消息總線

SpringBoot實戰電商項目mall(20k+star)地址:github.com/macrozheng/…git

摘要

Spring Cloud Bus 使用輕量級的消息代理來鏈接微服務架構中的各個服務,能夠將其用於廣播狀態更改(例如配置中心配置更改)或其餘管理指令,本文將對其用法進行詳細介紹。github

Spring Cloud Bus 簡介

咱們一般會使用消息代理來構建一個主題,而後把微服務架構中的全部服務都鏈接到這個主題上去,當咱們向該主題發送消息時,全部訂閱該主題的服務都會收到消息並進行消費。使用 Spring Cloud Bus 能夠方便地構建起這套機制,因此 Spring Cloud Bus 又被稱爲消息總線。Spring Cloud Bus 配合 Spring Cloud Config 使用能夠實現配置的動態刷新。目前 Spring Cloud Bus 支持兩種消息代理:RabbitMQ 和 Kafka,下面以 RabbitMQ 爲例來演示下使用Spring Cloud Bus 動態刷新配置的功能。web

RabbitMQ的安裝

  • 安裝完成後,進入RabbitMQ安裝目錄下的sbin目錄:

  • 在地址欄輸入cmd並回車啓動命令行,而後輸入如下命令啓動管理功能:
rabbitmq-plugins enable rabbitmq_management
複製代碼

  • 輸入帳號密碼並登陸:guest guest

動態刷新配置

使用 Spring Cloud Bus 動態刷新配置須要配合 Spring Cloud Config 一塊兒使用,咱們使用上一節中的config-server、config-client模塊來演示下該功能。spring

給config-server添加消息總線支持

  • 在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>
複製代碼
  • 添加配置文件application-amqp.yml,主要是添加了RabbitMQ的配置及暴露了刷新配置的Actuator端點;
server:
 port: 8904
spring:
 application:
 name: config-server
 cloud:
 config:
 server:
 git:
 uri: https://gitee.com/macrozheng/springcloud-config.git
 username: macro
 password: 123456
 clone-on-start: true # 開啓啓動時直接從git獲取配置
 rabbitmq: #rabbitmq相關配置
 host: localhost
 port: 5672
 username: guest
 password: guest
eureka:
 client:
 service-url:
 defaultZone: http://localhost:8001/eureka/
management:
 endpoints: #暴露bus刷新配置的端點
 web:
 exposure:
 include: 'bus-refresh'
複製代碼

給config-client添加消息總線支持

  • 在pom.xml中添加相關依賴:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
複製代碼
  • 添加配置文件bootstrap-amqp1.yml及bootstrap-amqp2.yml用於啓動兩個不一樣的config-client,兩個配置文件只有端口號不一樣;
server:
 port: 9004
spring:
 application:
 name: config-client
 cloud:
 config:
 profile: dev #啓用環境名稱
 label: dev #分支名稱
 name: config #配置文件名稱
 discovery:
 enabled: true
 service-id: config-server
 rabbitmq: #rabbitmq相關配置
 host: localhost
 port: 5672
 username: guest
 password: guest
eureka:
 client:
 service-url:
 defaultZone: http://localhost:8001/eureka/
management:
 endpoints:
 web:
 exposure:
 include: 'refresh'
複製代碼

動態刷新配置演示

  • 咱們先啓動相關服務,啓動eureka-server,以application-amqp.yml爲配置啓動config-server,以bootstrap-amqp1.yml爲配置啓動config-client,以bootstrap-amqp2.yml爲配置再啓動一個config-client,啓動後註冊中心顯示以下:

  • 啓動全部服務後,咱們登陸RabbitMQ的控制檯能夠發現Spring Cloud Bus 建立了一個叫springCloudBus的交換機及三個以 springCloudBus.anonymous開頭的隊列:

  • 咱們先修改Git倉庫中dev分支下的config-dev.yml配置文件:
# 修改前信息
config:
 info: "config info for dev(dev)"
# 修改後信息
config:
 info: "update config info for dev(dev)"  
複製代碼

update config info for dev(dev)
複製代碼

配合WebHooks使用

WebHooks至關因而一個鉤子函數,咱們能夠配置當向Git倉庫push代碼時觸發這個鉤子函數,這裏以Gitee爲例來介紹下其使用方式,這裏當咱們向配置倉庫push代碼時就會自動刷新服務配置了。bootstrap

使用到的模塊

springcloud-learning
├── eureka-server -- eureka註冊中心
├── config-server -- 配置中心服務
└── config-client -- 獲取配置的客戶端服務
複製代碼

項目源碼地址

github.com/macrozheng/…bash

公衆號

mall項目全套學習教程連載中,關注公衆號第一時間獲取。架構

公衆號圖片
相關文章
相關標籤/搜索