54 消息總線bus

在微服務(wù)架構(gòu)中,通常會(huì)使用輕量級(jí)的消息代理來(lái)構(gòu)建一個(gè)共用的消息主題來(lái)連接各個(gè)微服務(wù)實(shí)例,它 廣播的消息會(huì)被所有在注冊(cè)中心的微服務(wù)實(shí)例監(jiān)聽(tīng)和消費(fèi),也稱消息總線。 SpringCloud中也有對(duì)應(yīng)的解決方案,SpringCloud Bus 將分布式的節(jié)點(diǎn)用輕量的消息代理連接起來(lái), 可以很容易搭建消息總線,配合SpringCloud config 實(shí)現(xiàn)微服務(wù)應(yīng)用配置信息的動(dòng)態(tài)更新。


image-20220102211404297.png

根據(jù)此圖我們可以看出利用Spring Cloud Bus做配置更新的步驟:

  • 提交代碼觸發(fā)post請(qǐng)求給bus/refresh
  • server端接收到請(qǐng)求并發(fā)送給Spring Cloud Bus
  • Spring Cloud bus接到消息并通知給其它客戶端
  • 其它客戶端接收到通知,請(qǐng)求Server端獲取最新配置
  • 全部客戶端均獲取到最新的配置

消息總線整合配置中心

(1) 引入依賴

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>

(2)服務(wù)端配置

server:
 port: 10000 #服務(wù)端口
spring:
 application:
   name: config-server #指定服務(wù)名
 cloud:
   config:
     server:
       git:
         uri: https://gitee.com/it-lemon/config-repo.git
 rabbitmq:
   host: 127.0.0.1
   port: 5672
   username: guest
   password: guest
management:
 endpoints:
   web:
     exposure:
       include: bus-refresh
eureka:
 client:
   serviceUrl:
     defaultZone: http://127.0.0.1:8761/eureka/
 instance:
   preferIpAddress: true
   instance-id: ${spring.cloud.client.ip-address}:${server.port}
#spring.cloud.client.ip-address:獲取ip地址

(3)微服務(wù)客戶端配置

server:
 port: 9002
eureka:
 client:
   serviceUrl:
     defaultZone: http://127.0.0.1:8761/eureka/
spring:
 cloud:
   config:
     name: product
     profile: dev
     label: master
     discovery:
       enabled: true
       service-id: config-server

需要在碼云對(duì)應(yīng)的配置文件中添加rabbitmq的配置信息

image-20220102211635504.png

重新啟動(dòng)對(duì)應(yīng)的eureka-server , config-server , product-service。配置信息刷新后,只需要向配置 中心發(fā)送對(duì)應(yīng)的請(qǐng)求,即可刷新每個(gè)客戶端的配置

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容