RabbitMQ--topic
生活随笔
收集整理的這篇文章主要介紹了
RabbitMQ--topic
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
-
Topic類型的Exchange與Direct相比,都是可以根據(jù)RoutingKey把消息路由到不同的隊列。只不過Topic類型Exchange可以讓隊列在綁定Routing key 的時候使用通配符!
-
Routingkey 一般都是有一個或多個單詞組成,多個單詞之間以”.”分割,例如: item.insert
通配符規(guī)則:
-
#:匹配一個或多個詞
-
*:匹配不多不少恰好1個詞
舉例:
-
item.#:能夠匹配item.spu.insert 或者 item.spu
-
item.*:只能匹配item.spu
解釋:
- Queue1:綁定的是china.# ,因此凡是以 china.開頭的routing key 都會被匹配到。包括china.news和china.weather
- Queue2:綁定的是#.news ,因此凡是以 .news結(jié)尾的 routing key 都會被匹配。包括china.news和japan.news
舉例:
public_topic 消息生產(chǎn)者
@RunWith(SpringRunner.class) @SpringBootTest public class TestMqTopic {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testTopic1(){//發(fā)送的目標交換機String exchange = "itcast.topic";String message="我王二天下無敵1";rabbitTemplate.convertAndSend(exchange,"china.RAP",message);}@Testpublic void testTopic2(){//發(fā)送的目標交換機String exchange = "itcast.topic";String message="我王二天下無敵2";rabbitTemplate.convertAndSend(exchange,"yangshi.news",message);}@Testpublic void testTopic3(){//發(fā)送的目標交換機String exchange = "object.json";Student stu = new Student("王二", 1, new String[]{"sing", "dance", "rap"});rabbitTemplate.convertAndSend(exchange,"china.json",stu);} }配置
spring:rabbitmq:host: 192.168.23.130port: 5672username: itcastpassword: 123321virtual-host: /consumer_topic 消息消費者
@Component public class SpringRabbitListener {//- Topic交換機接收的消息RoutingKey必須是多個單詞,以 `**.**` 分割//- Topic交換機與隊列綁定時的bindingKey可以指定通配符//- `#`:代表0個或多個詞//- `*`:代表1個詞@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue1"),exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),key = {"china.#"}))public void listenerTopicQueue1(String msg){System.out.println("topicQueue1接收的消息為"+msg);}@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue2"),exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),key = {"#.news"}))public void listenerTopicQueue2(String msg){System.out.println("topicQueue2接收的消息為"+msg);}@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue3"),exchange = @Exchange(value = "object.json",type = ExchangeTypes.TOPIC),key = {"#.json"}))public void listenerTopicQueue3(Student msg){System.out.println("topicQueue3接收的對象為"+msg);} }啟動類配置對象轉(zhuǎn)化可在可視化界面查看
@SpringBootApplication public class MqTopicApplication {public static void main(String[] args) {SpringApplication.run(MqTopicApplication.class);}//配置消息轉(zhuǎn)換器@Beanpublic MessageConverter jsonMessageConverter(){return new Jackson2JsonMessageConverter();} }配置
spring:rabbitmq:host: 192.168.23.130port: 5672username: itcastpassword: 123321virtual-host: /所需依賴
<!--AMQP依賴,包含RabbitMQ--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId> </dependency><!--json轉(zhuǎn)化依賴--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>父依賴
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.9.RELEASE</version><relativePath/></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><!--AMQP依賴,包含RabbitMQ--><dependencies><!--json轉(zhuǎn)化依賴--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--AMQP依賴,包含RabbitMQ--><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></dependency></dependencies>總結(jié)
以上是生活随笔為你收集整理的RabbitMQ--topic的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 选购蓝牙耳机要注意哪些
- 下一篇: RabbitMQ消息