SpringCloud之消息總線Spring Cloud Bus實(shí)例代碼
一、簡(jiǎn)介
在微服務(wù)架構(gòu)的系統(tǒng)中,我們通常會(huì)使用輕量級(jí)的消息代理來構(gòu)建一個(gè)共用的消息主題讓系統(tǒng)中所有微服務(wù)實(shí)例都連接上來,由于該主題中產(chǎn)生的消息會(huì)被所有實(shí)例監(jiān)聽和消費(fèi),所以我們稱它為消息總線。
二、消息代理
消息代理(Message Broker)是一種消息驗(yàn)證、傳輸、路由的架構(gòu)模式。它在應(yīng)用程序之間起到通信調(diào)度并最小化應(yīng)用之間的依賴的作用,使得應(yīng)用程序可以高效地解耦通信過程。消息代理是一個(gè)中間件產(chǎn)品,它的核心是一個(gè)消息的路由程序,用來實(shí)現(xiàn)接收和分發(fā)消息, 并根據(jù)設(shè)定好的消息處理流來轉(zhuǎn)發(fā)給正確的應(yīng)用。 它包括獨(dú)立的通信和消息傳遞協(xié)議,能夠?qū)崿F(xiàn)組織內(nèi)部和組織間的網(wǎng)絡(luò)通信。設(shè)計(jì)代理的目的就是為了能夠從應(yīng)用程序中傳入消息,并執(zhí)行一些特別的操作,下面這些是在企業(yè)應(yīng)用中,我們經(jīng)常需要使用消息代理的場(chǎng)景:
- 將消息路由到一個(gè)或多個(gè)目的地。
- 消息轉(zhuǎn)化為其他的表現(xiàn)方式。
- 執(zhí)行消息的聚集、消息的分解,并將結(jié)果發(fā)送到它們的目的地,然后重新組合響應(yīng)返回給消息用戶。
- 調(diào)用Web服務(wù)來檢索數(shù)據(jù)。
- 響應(yīng)事件或錯(cuò)誤。
- 使用發(fā)布-訂閱模式來提供內(nèi)容或基千主題的消息路由。
目前已經(jīng)有非常多的開源產(chǎn)品可以供大家使用, 比如:
- ActiveMQKafka
- RabbitMQ
- RocketMQ
- 等......
三、SpringCloud+RabbitMQ
(1)RabbitMQ簡(jiǎn)介、安裝不贅述。
(2)pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
(3)application.yml
spring: application: name: rabbitmq-hello rabbitmq: host: ***.***.***.*** port: 5672 username: guest password: guest
(4)發(fā)送者Sender
@Component public class Sender { private static final Logger log = LoggerFactory.getLogger(Sender.class); @Autowired private AmqpTemplate amqpTemplate; public void send() { String context = "hello " + new Date(); log.info("Sender : " + context); this.amqpTemplate.convertAndSend("hello", context); } }
(5)接受者Receiver
@Component @RabbitListener(queues = "hello") public class Receiver { private static final Logger log = LoggerFactory.getLogger(Receiver.class); @RabbitHandler public void process(String hello) { log.info("Receiver : " + hello); } }
(6)創(chuàng)建RabbitMQ的配置類 RabbitConfig
@Configuration public class RabbitConfig { @Bean public Queue helloQueue(){ return new Queue("hello"); } }
(7)創(chuàng)建單元測(cè)試類, 用來調(diào)用消息生產(chǎn)
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = SpringcloudbusrabbitmqApplication.class) public class HelloApplicationTests { @Autowired private Sender sender; @Test public void hello() throws Exception { sender.send(); } }
(8)測(cè)試,執(zhí)行HelloApplicationTests
(9)訪問host:15672
四、改造Config-Client(整合springcloud bus)
(1)pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
(2)bootstrap.properties
spring.application.name=configspace spring.cloud.config.label=master spring.cloud.config.profile=dev spring.cloud.config.uri= http://localhost:5588/ eureka.client.serviceUrl.defaultZone=http://localhost:5555/eureka/ server.port=5589 spring.rabbitmq.host=118.89.237.88 spring.rabbitmq.port= 5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest management.security.enabled=false
(3)其他不用改變
五、測(cè)試
(1)測(cè)試準(zhǔn)備
一個(gè)服務(wù)注冊(cè)中心,EUREKASERVER,端口為5555;
一個(gè)分布式配置中心,ConfigServer,端口為5588;
二個(gè)分布式配置,ConfigClient,端口為5589、5590;(2)訪問http://localhost:5589/from
(3)訪問http://localhost:5590/from
RabbitMQ:
(4)去倉(cāng)庫(kù)修改password的值
from=git-dev-v1.0 by springcloud config-server username=springcloud password=1234567890
(5)POST請(qǐng)求http://localhost:5589/bus/refresh或者h(yuǎn)ttp://localhost:5590/bus/refresh
成功請(qǐng)求后config-client會(huì)重新讀取配置文件
(6)再次訪問
- 如果POST請(qǐng)求的是:http://localhost:5589/bus/refresh,請(qǐng)?jiān)L問http://localhost:5590/from
- 如果訪問出現(xiàn)401,則配置需要加上management.security.enabled=false
如果POST請(qǐng)求的是:http://localhost:5590/bus/refresh,請(qǐng)?jiān)L問http://localhost:5589/from
另/bus/refresh接口可以指定服務(wù),即使用“username”參數(shù),比如 “/bus/refresh?destination=username:**”即刷新服務(wù)名為username的所有服務(wù),不管ip地址。
(7)架構(gòu)
(8)架構(gòu)調(diào)整
既然SpringCloud Bus的/bus/refresh接口提供了針對(duì)服務(wù)和實(shí)例進(jìn)行配置更新的參數(shù),那么我們的架構(gòu)也可以相應(yīng)做出一些調(diào)整。在之前的架構(gòu)中,服務(wù)的配置更新需要通過向具體服務(wù)中的某個(gè)實(shí)例發(fā)送請(qǐng)求,再觸發(fā)對(duì)整個(gè)服務(wù)集群的配置更新。雖然能實(shí)現(xiàn)功能,但是這樣的結(jié)果是,我們指定的應(yīng)用實(shí)例會(huì)不同千集群中的其他應(yīng)用實(shí)例,這樣會(huì)增加集群內(nèi)部的復(fù)雜度,不利于將來的運(yùn)維工作。比如, 需要對(duì)服務(wù)實(shí)例進(jìn)行遷移,那么我們不得不修改Web Hook中的配置等。所以要盡可能地讓服務(wù)集群中的各個(gè)節(jié)點(diǎn)是對(duì)等的。
因此, 我們將之前的架構(gòu)做了 一些調(diào)整, 如下圖所示:
主要做了以下這些改動(dòng):
- 在ConfigServer中也引入SpringCloud Bus,將配置服務(wù)端也加入到消息總線中來。
- /bus/refresh請(qǐng)求不再發(fā)送到具體服務(wù)實(shí)例上,而是發(fā)送給Config Server,并通過des巨nation參數(shù)來指定需要更新配置的服務(wù)或?qū)嵗?/li>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探究Java常量本質(zhì)及三種常量池(小結(jié))
這篇文章主要介紹了探究Java常量本質(zhì)及三種常量池(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09mybatis?實(shí)體類字段大小寫問題?字段獲取不到值的解決
這篇文章主要介紹了mybatis?實(shí)體類字段大小寫問題?字段獲取不到值的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例
這篇文章主要介紹了SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08java查找字符串中的包含子字符串的個(gè)數(shù)實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄猨ava查找字符串中的包含子字符串的個(gè)數(shù)實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06java實(shí)現(xiàn)pdf按頁(yè)轉(zhuǎn)換為圖片
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)pdf按頁(yè)轉(zhuǎn)換為圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12使用IDEA搭建一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目超詳細(xì)過程
這篇文章主要介紹了使用IDEA搭建一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目超詳細(xì)過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02如何解決freemarker靜態(tài)化生成html頁(yè)面亂碼的問題
這篇文章主要介紹了如何解決freemarker靜態(tài)化生成html頁(yè)面亂碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01