RabbitMQ之springboot操作( 二 )

topic 模型topic模型与路由一致,就是将固定的路由key改成路由通配符,其实原理都一致,mq提供的路由通配符 * 和 #
*:匹配一个任意的单词
#: 匹配多个任意的单词
举例: 生产者发送的路由key : “test.topic”
消费者1设置的路由key :"test.* " ,消费者2设置的路由key: "test.# "
那么两个消费者都可以接收到该消息 ,但是当生产者将路由key改为 test.topic.one
这时只有消费者2可以接收到消息 因为#通配符可以接收任意多个单词
//生产者@GetMapping("topicMQ")public void topicMQ(){rabbitTemplate.convertAndSend("test-topic","key.topic","hello");}//消费者@Componentpublic class TopicCustomer {@RabbitListener(bindings = {@QueueBinding(value = https://www.isolves.com/it/cxkf/jiagou/2022-09-07/@Queue,// 代表使用的临时队列exchange = @Exchange(value= "test-topic",type = "topic") ,// 该参数默认类型就是directkey = { "key.#","key.*"})})public void receive(String msg) throws InterruptedException {System.out.println("receive msg is "+msg);}@RabbitListener(bindings = {@QueueBinding(value = @Queue,// 代表使用的临时队列exchange = @Exchange(value= "test-topic",type = "topic") ,// 该参数默认类型就是directkey = { "key.#"})})public void receive1(String msg) throws InterruptedException {System.out.println("receive1 msg is "+msg);}@RabbitListener(bindings = {@QueueBinding(value = @Queue,// 代表使用的临时队列exchange = @Exchange(value= "test-topic",type = "topic") ,// 该参数默认类型就是directkey = { "key.*"})})public void receive2(String msg) throws InterruptedException {System.out.println("receive2 msg is "+msg);}来源:
https://blog.csdn.NET/pgcdnameming/article/details/126666982

【RabbitMQ之springboot操作】


推荐阅读