SpringBoot整合RabbitMQ實現(xiàn)六種工作模式的示例
RabbitMQ主要有六種種工作模式,本文整合SpringBoot分別介紹工作模式的實現(xiàn)。
前提概念
生產(chǎn)者
消息生產(chǎn)者或者發(fā)送者,使用P表示:

隊列
消息從生產(chǎn)端發(fā)送到消費端,一定要通過隊列轉發(fā),使用queue_name表示:

消費者
消費的消費者或者接收者,使用C表示,如果有多個消費者也可以用C1、C2表示:

SpringBoot整合RabbitMQ基本配置添加maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>添加application.yml 配置
spring:
rabbitmq:
host: 192.168.3.19
port: 5672
username: admin
password: 123456消息生產(chǎn)
生產(chǎn)端發(fā)送消息,調(diào)用RabbitTemplate發(fā)送消息,比如:
@Autowired
private RabbitTemplate rabbitTemplate;
public String send() {
rabbitTemplate.convertAndSend("routingKey","send message");
}消費消息
消費消息使用隊列監(jiān)聽注解@RabbitListener,添加隊列名稱就能消費發(fā)送到隊列上的消息了:
@RabbitListener(queuesToDeclare = @Queue("queue_name"))
public void consume(String message) {
// 接收消息
}1. 簡單(simple)模式

最簡單的消息發(fā)送
特點生產(chǎn)者是消費者是一一對應,也叫做點對點模式,生產(chǎn)者發(fā)送消息經(jīng)過隊列直接發(fā)送給消費者。
生產(chǎn)者和消費者在發(fā)送和接收消息時,只需要指定隊列名稱,而不需要指定Exchange 交換機。
生產(chǎn)消息:
@GetMapping("/simple-send")
public String simpleSend() {
rabbitTemplate.convertAndSend("simple","this is news");
return "ok";
}消費消息
@RabbitListener(queuesToDeclare = @Queue("simple"))
public void consume(String message) {
System.out.println(message);
}輸出:
this is news
無需創(chuàng)建交換機和綁定隊列,只需要匹配發(fā)送端和消費端的隊列名稱就能成功發(fā)送消息。
2. 工作模式

在多個消費者之間分配任務
- 特點
工作模式和簡單模式差不多,只需要生產(chǎn)端、消費端、隊列。 - 不同在于一個生產(chǎn)者、一個隊列對應
多個消費者,也就是一對多的關系。 - 在多個消費者之間分配消息(競爭消費者模式),類似輪詢發(fā)送消息,每個消息都只發(fā)給一個消費者。
生產(chǎn)消息:
@GetMapping("/work-send")
public String simpleSend() {
rabbitTemplate.convertAndSend("work","this is news");
return "ok";
}消費消息:
@RabbitListener(queuesToDeclare = @Queue("work"))
public void consume(String message) {
System.out.println("first:" + message);
}
@RabbitListener(queuesToDeclare = @Queue("work"))
public void consumeSecond(String message) {
System.out.println("second:" + message);
}創(chuàng)建一個生產(chǎn)者,兩個消費者,發(fā)送兩條消息,兩個消費者分別接收到消息,輸出:
first:this is news
second:this is news
兩個消費者,輪流消費消息。類似nginx負載均衡。
3. 發(fā)布訂閱模式

一次向多個消費者發(fā)送消息
特點
- 發(fā)布訂閱類似廣播消息,每個消息可以同時發(fā)送給訂閱該消息的消費者,
- 上圖中的
X表示交換機,使用的扇形交換機(fanout),它將發(fā)送的消息發(fā)送到所有綁定交換機的隊列。
創(chuàng)建隊列、交換機以及綁定:
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("PUBLISH_SUBSCRIBE_EXCHANGE");
}
@Bean
public Queue psFirstQueue() {
return new Queue("psFirstQueue");
}
@Bean
public Queue psSecondQueue() {
return new Queue("psSecondQueue");
}
@Bean
public Queue psThirdQueue() {
return new Queue("psThirdQueue");
}
@Bean
public Binding routingFirstBinding() {
return BindingBuilder.bind(psFirstQueue()).to(fanoutExchange());
}
@Bean
public Binding routingSecondBinding() {
return BindingBuilder.bind(psSecondQueue()).to(fanoutExchange());
}
@Bean
public Binding routingThirdBinding() {
return BindingBuilder.bind(psThirdQueue()).to(fanoutExchange());
}
- 上面定義一個交換機
fanoutExchange。 - 分別綁定三個隊列
psFirstQueue、psSecondQueue、psThirdQueue。 - 隊列綁定交換機不需要
routingKey,直接綁定即可。

生產(chǎn)端:
@GetMapping("/publish-sub-send")
public String publishSubSend() {
rabbitTemplate.convertAndSend("PUBLISH_SUBSCRIBE_EXCHANGE", null, "publish/subscribe hello");
return "ok";
}無需指定routingKey,設置為null。
消費端:
@RabbitListener(queues = "psFirstQueue")
public void pubsubQueueFirst(String message) {
System.out.println("【first】:" + message);
}
@RabbitListener(queues = "psSecondQueue")
public void pubsubQueueSecond(String message) {
System.out.println("【second】:" + message);
}
@RabbitListener(queues = "psThirdQueue")
public void pubsubQueueThird(String message) {
System.out.println("【third】:" + message);
}輸出:
【first】: publish/subscribe hello
【second】: publish/subscribe hello
【third】: publish/subscribe hello
發(fā)送一條消息,綁定的隊列都能接收到消息。
4. 路由模式

根據(jù)routingKey有選擇性的接收消息
特點
- 每個隊列根據(jù)不同
routingKey綁定交換機消息發(fā)送到交換機后 - 通過
routingKey發(fā)送給特定的隊列,然后傳到消費者消費。 - 交換由
扇形交換機(fanout)改成直連交換機(direct)。
創(chuàng)建隊列、交換機以及綁定:
@Bean
public Queue routingFirstQueue() {
return new Queue("routingFirstQueue");
}
@Bean
public Queue routingSecondQueue() {
return new Queue("routingSecondQueue");
}
@Bean
public Queue routingThirdQueue() {
return new Queue("routingThirdQueue");
}
@Bean
public DirectExchange routingExchange() {
return new DirectExchange("routingExchange");
}
@Bean
public Binding routingFirstBind() {
return BindingBuilder.bind(routingFirstQueue()).to(routingExchange()).with("firstRouting");
}
@Bean
public Binding routingSecondBind() {
return BindingBuilder.bind(routingSecondQueue()).to(routingExchange()).with("secondRouting");
}
@Bean
public Binding routingThirdBind() {
return BindingBuilder.bind(routingThirdQueue()).to(routingExchange()).with("thirdRouting");
}
- 創(chuàng)建一個交換機,根據(jù)不同的路由規(guī)則匹配不同的隊列
routingExchange,根據(jù)不同的routingKey綁定不同的隊列: firstRouting路由鍵綁定routingFirstQueue隊列。secondRouting路由鍵綁定routingSecondQueue隊列。thirdRouting路由鍵綁定routingThirdQueue隊列。

生產(chǎn)消息:
@GetMapping("/routing-first")
public String routingFirst() {
// 使用不同的routingKey 轉發(fā)到不同的隊列
rabbitTemplate.convertAndSend("routingExchange","firstRouting"," first routing message");
rabbitTemplate.convertAndSend("routingExchange","secondRouting"," second routing message");
rabbitTemplate.convertAndSend("routingExchange","thirdRouting"," third routing message");
return "ok";
}消費消息:
@RabbitListener(queues = "routingFirstQueue")
public void routingFirstListener(String message) {
System.out.println("【routing first】" + message);
}
@RabbitListener(queues = "routingSecondQueue")
public void routingSecondListener(String message) {
System.out.println("【routing second】" + message);
}
@RabbitListener(queues = "routingThirdQueue")
public void routingThirdListener(String message) {
System.out.println("【routing third】" + message);
}輸出:
【routing first】first routing message
【routing second】second routing message
【routing third】third routing message
分析:
rabbitTemplate.convertAndSend("routingExchange","firstRouting"," first routing message");
消息從生產(chǎn)者指定firstRouting路由鍵,找到對應的綁定隊列routingFirstQueue,就被routingFirstQueue隊列消費了。
5. 主題模式

基于某個主題接收消息
特點
路由模式發(fā)送的消息,是需要指定固定的routingKey,如果想要針對一類路由。
比如:
- 只接收以
.com結尾的消息。 www.開頭的消息。
主題模式就派上場了,路由模式和主題模式類似,路由模式是設置特定的routingKey綁定唯一的隊列,而主題模式的是使用通配符匹配一個或者多個隊列。
創(chuàng)建交換機和隊列:
@Bean
public Queue topicFirstQueue() {
return new Queue("topicFirstQueue");
}
@Bean
public Queue topicSecondQueue() {
return new Queue("topicSecondQueue");
}
@Bean
public Queue topicThirdQueue() {
return new Queue("topicThirdQueue");
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange");
}
使用通配符綁定交換機和交換機:
@Bean
public Binding topicFirstBind() {
// .com 為結尾
return BindingBuilder.bind(topicFirstQueue()).to(topicExchange()).with("*.com");
}
@Bean
public Binding topicSecondBind() {
// www.為開頭
return BindingBuilder.bind(topicSecondQueue()).to(topicExchange()).with("www.#");
}通配符有兩種,*和#,
*表示可以匹配一個。#表示可以匹配多個。
比如:
#.com表示接收多個以.com結尾的字段。
例如: taobao.com、www.taobao.com、www.jd.com。
*.com表示接收一個以.com結尾的字段。
例如: taobao.com、jd.com。多個字段是無法匹配的,比如www.taobao.com、cn.taobao.com。
www.#可以匹配多個以www開頭的字段。
例如www.taobao、www.jd。
www.*可以匹配一個以www開頭的字段。
例如:www.taobao、www.jd。多個字段是無法匹配的,比如www.taobao.com、www.jd.com。
生產(chǎn)消息:
@GetMapping("/topic-first-send")
public String topicFirstSend() {
rabbitTemplate.convertAndSend("topicExchange","www.taobao.com","www.taobao.com");
rabbitTemplate.convertAndSend("topicExchange","taobao.com","taobao.com");
rabbitTemplate.convertAndSend("topicExchange","www.jd","www.jd");
return "topic ok";
}消費消息:
@RabbitListener(queues = "topicFirstQueue")
public void topicFirstListener(String message) {
System.out.println("【topic first】" + message);
}
@RabbitListener(queues = "topicSecondQueue")
public void topicSecondListener(String message) {
System.out.println("【topic second】" + message);
}輸出:
【topic second】www.taobao.com
【topic first】taobao.com
【topic second】www.jd
www.#可以匹配多個以www.開頭的路由鍵,例如www.taobao.com、www.jd。而*.com只能匹配一個以.com結尾的路由鍵,例如taobao.com,而無法匹配www.taobao.com。
6. RPC模式

消息有返回值
特點
PRC模式和上面的幾種模式唯一不同的點在于,該模式可以收到消費端的返回值。- 生成端接收消費端的返回值。
消費端添加返回值:
@RabbitListener(queuesToDeclare =@Queue("rpcQueue"))
public String rpcListener(String message) {
System.out.println("【rpc接收消息】" + message);
return "rpc 返回" + message;
}生產(chǎn)端發(fā)送消息:
@GetMapping("/rpc-send")
public void rpcSend() {
Object receive = rabbitTemplate.convertSendAndReceive("rpcQueue","rpc send message");
System.out.println("【發(fā)送消息消息】" + receive);
}輸出:
【rpc接收消息】rpc send message
【發(fā)送端接收消息】rpc 返回rpc send message
交換機類型
上面的 訂閱發(fā)布模式、路由模式以及主題模式使用到了不同的交換機,分別是:
- 直連交換機 Direct
- 扇形交換機 Fanout
- 主題交換器 Topic

Direct Exchange(直連)

直連交換機被應用在路由模式下,該交換機需要通過特定的routingKey來綁定隊列,交換機只有接收到了匹配的routingKey才會將消息轉發(fā)到對應的隊列中,否則就不會轉發(fā)消息。
路由模式使用直連交換機,該模式下根據(jù)routingKey綁定特定的隊列。
Fanout Exchange(扇形)

扇形交換機沒有路由鍵的概念,只需將隊列綁定在交換機上,發(fā)送到交換機上的消息會轉發(fā)到交換機所以綁定的隊列里面,類似廣播,只要打開收音機都能接收到廣播消息。扇形交換機應用于發(fā)布訂閱模式。
Topic Exchange(主題)

主題模式是將路由鍵根據(jù)一個主題進行分類,和直連模式不同的是,直連模式綁定特定的路由鍵,而主題模式使用通配符綁定路由鍵,綁定鍵有兩種:
*表示可以匹配僅一個。#表示可以匹配零個或多個。
總結
整合SpringBoot實現(xiàn)RabbitMQ六種工作模式,并詳細講解RabbitMQ六種工作模式:
- 簡單模式
無需創(chuàng)建交換機,匹配生產(chǎn)端和消費的routingKey即可。
- 工作模式
多個消費端公平競爭同一個消息。
- 發(fā)布訂閱模式
一次向多個消費者發(fā)送消息。
- 路由模式
根據(jù)特定的路由鍵轉發(fā)消息。
- 主題模式
根據(jù)通配符,匹配路由鍵轉發(fā)消息。
- RPC模式
生產(chǎn)端接收消費端發(fā)送的返回值。
源碼示例
參考文獻
到此這篇關于SpringBoot整合RabbitMQ實現(xiàn)六種工作模式的文章就介紹到這了,更多相關SpringBoot整合RabbitMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot集成ElasticSearch(ES)實現(xiàn)全文搜索功能
Elasticsearch是一個開源的分布式搜索和分析引擎,它被設計用于處理大規(guī)模數(shù)據(jù)集,它提供了一個分布式多用戶能力的全文搜索引擎,本文將給大家介紹SpringBoot集成ElasticSearch(ES)實現(xiàn)全文搜索功能,需要的朋友可以參考下2024-02-02
Jmeter內(nèi)置變量vars和props的使用詳解
JMeter是一個功能強大的負載測試工具,它提供了許多有用的內(nèi)置變量來支持測試過程,其中最常用的變量是 vars 和 props,本文通過代碼示例詳細給大家介紹了Jmeter內(nèi)置變量vars和props的使用,需要的朋友可以參考下2024-08-08
springboot中事務管理@Transactional的注意事項與使用場景
今天小編就為大家分享一篇關于springboot中事務管理@Transactional的注意事項與使用場景,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04

