Spring Boot整合RabbitMQ實例(Topic模式)
1.Topic交換器介紹
Topic Exchange 轉(zhuǎn)發(fā)消息主要是根據(jù)通配符。 在這種交換機(jī)下,隊列和交換機(jī)的綁定會定義一種路由模式,那么,通配符就要在這種路由模式和路由鍵之間匹配后交換機(jī)才能轉(zhuǎn)發(fā)消息。
在這種交換機(jī)模式下:
路由鍵必須是一串字符,用句號(.) 隔開,比如說 agreements.us,或者 agreements.eu.stockholm 等。
路由模式必須包含一個 星號(*),主要用于匹配路由鍵指定位置的一個單詞,比如說,一個路由模式是這樣子:agreements..b.*,那么就只能匹配路由鍵是這樣子的:第一個單詞是 agreements,第四個單詞是 b。 井號(#)就表示相當(dāng)于一個或者多個單詞,例如一個匹配模式是agreements.eu.berlin.#,那么,以agreements.eu.berlin開頭的路由鍵都是可以的。
具體代碼發(fā)送的時候還是一樣,第一個參數(shù)表示交換機(jī),第二個參數(shù)表示routing key,第三個參數(shù)即消息。如下:
rabbitTemplate.convertAndSend("testTopicExchange","key1.a.c.key2", " this is RabbitMQ!");
topic 和 direct 類似, 只是匹配上支持了"模式", 在"點分"的 routing_key 形式中, 可以使用兩個通配符:
*表示一個詞.
#表示零個或多個詞.

如上圖所示:此類交換器使得來自不同的源頭的消息可以到達(dá)一個對列,其實說的更明白一點就是模糊匹配的意思,例如:上圖中紅色對列的routekey為usa.#,#代表匹配任意字符,但是要想消息能到達(dá)此對列,usa.必須匹配后面的#好可以隨意。圖中usa.news,usa.weather都能找到紅色隊列,符號“#”匹配一個或多個詞,符號“”匹配不多不少一個詞。因此“usa.#”能夠匹配到“usa.news.XXX”,但是“usa.” 只會匹配到“usa.XXX”。
注:交換器說到底是一個名稱與隊列綁定的列表。當(dāng)消息發(fā)布到交換器時,實際上是由你所連接的信道,將消息路由鍵同交換器上綁定的列表進(jìn)行比較,最后路由消息
2.示例代碼
1).RabbitMQ的Topic的bean配置
RabbitTopic.java類:
package com.example.rabbitmqtopic;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitTopic {
final static String message = "topic.message";
final static String messages = "topic.messages";
//創(chuàng)建隊列
@Bean
public Queue queueMessage() {
return new Queue(RabbitTopic.message);
}
//創(chuàng)建隊列
@Bean
public Queue queueMessages() {
return new Queue(RabbitTopic.messages);
}
//創(chuàng)建交換器
@Bean
TopicExchange exchange() {
return new TopicExchange("topicExchange");
}
//對列綁定并關(guān)聯(lián)到ROUTINGKEY
@Bean
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}
//對列綁定并關(guān)聯(lián)到ROUTINGKEY
@Bean
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");//*表示一個詞,#表示零個或多個詞
}
}
2).消息生產(chǎn)者生產(chǎn)消息
TopicSender.java類:
package com.example.rabbitmqtopic.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TopicSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "hi, i am message all";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("topicExchange", "topic.1", context);
}
public void send1() {
String context = "hi, i am message 1";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("topicExchange", "topic.message", context);
}
public void send2() {
String context = "hi, i am messages 2";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("topicExchange", "topic.messages", context);
}
}
3).消息消費者
TopicReceiver.java類:
package com.example.rabbitmqtopic.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topic.message")
public class TopicReceiver {
@RabbitHandler
public void process(String message) {
System.out.println("Topic Receiver1 : " + message);
}
}
TopicReceiver2.java類:
package com.example.rabbitmqtopic.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topic.messages")
public class TopicReceiver2 {
@RabbitHandler
public void process(String message) {
System.out.println("Topic Receiver2 : " + message);
}
}
4).測試
RabbitMQTopicTest.java類:
package com.example.rabbitmqtopic.rabbitmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTopicTest {
@Autowired
private TopicSender sender;
@Test
public void topic() throws Exception {
sender.send();
}
@Test
public void topic1() throws Exception {
sender.send1();
}
@Test
public void topic2() throws Exception {
sender.send2();
}
}
以上所述是小編給大家介紹的Spring Boot整合RabbitMQ實例(Topic模式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
redis redisson 限流器的實例(RRateLimiter)
這篇文章主要介紹了redis redisson 限流器的實例(RRateLimiter),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
文件上傳SpringBoot后端MultipartFile參數(shù)報空問題的解決辦法
這篇文章主要介紹了文件上傳SpringBoot后端MultipartFile參數(shù)報空問題的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot+Echarts實現(xiàn)請求后臺數(shù)據(jù)顯示餅狀圖
這篇文章主要介紹了SpringBoot+Echarts實現(xiàn)請求后臺數(shù)據(jù)顯示餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
java序列化和serialVersionUID的使用方法實例
這篇文章主要介紹了java序列化和serialVersionUID的使用方法實例的相關(guān)資料,這里說明很詳細(xì)的使用方法讓你徹底學(xué)會,需要的朋友可以參考下2017-08-08
SpringBoot應(yīng)用監(jiān)控Actuator使用隱患及解決方案
SpringBoot的Actuator 模塊提供了生產(chǎn)級別的功能,比如健康檢查,審計,指標(biāo)收集,HTTP 跟蹤等,幫助我們監(jiān)控和管理Spring Boot 應(yīng)用,本文將給大家介紹SpringBoot應(yīng)用監(jiān)控Actuator使用隱患及解決方案,需要的朋友可以參考下2024-07-07
Java實現(xiàn)整合文件上傳到FastDFS的方法詳細(xì)
FastDFS是一個開源的輕量級分布式文件系統(tǒng),對文件進(jìn)行管理,功能包括:文件存儲、文件同步、文件上傳、文件下載等,解決了大容量存儲和負(fù)載均衡的問題。本文將提供Java將文件上傳至FastDFS的示例代碼,需要的參考一下2022-02-02

