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操作】
推荐阅读
- |人为财死、鸟为食亡,如何让自己快速在职场中混出一席之地?
- 9个月宝宝睡眠时间
- 新生儿20天长多少正常
- 动物|白鲟、儒艮之后 我国两种长臂猿被宣布野外灭绝
- 张飞之死告诉我们什么道理?张飞的死和诸葛亮有关系吗
- 广州车展红旗h9+ 红旗h9报价及图片2020款汽车之家
- 育碧|《赛博朋克2077:往日之影》明年问世
- 摩擦力是两个物体之间发生相互时产生的阻碍运动的力 相互接触并有相对运动的两物体间必有摩擦力
- 余承东|全程diss特斯拉 问界各方面碾压之!M5 EV订单量超3万 余承东:感谢大家信赖
- 建文帝生死之谜一?建文帝生死之谜研究