springboot整合rabbitMQ系列(五)topics模型,这个是既有路由键,也使用通配符

it2026-08-08  3

这个是第五个模式

发送者

@Test void testTopics() { //使用rabbitmq直接往队列里面放数据 // convertAndSend()第一个参数 是 rabbitTemplate.convertAndSend("topics","user.save","基于这个user.save路由键的消息"); }

这个是发送给名字为topics的交换机,路由键是user.save ,发送的消息是 : 基于这个user.save路由键的消息

消费者

@Component public class TopicCustomer { @RabbitListener(bindings = { @QueueBinding( value = @Queue, //创建临时队列 exchange = @Exchange(type = "topic",name = "topics"), //绑定的交换机 key = {"user.save","user.*"} ) }) public void receivel(String message){ System.out.println("message1="+message); } @RabbitListener(bindings = { @QueueBinding( value = @Queue, //创建临时队列 exchange = @Exchange(name = "topics",type = "topic"), //绑定的交换机 key = {"order.#"} ) }) public void receive2(String message){ System.out.println("message2="+message); } }

以上的消费者,就是有通配符的路由键的,所以,我们直接使用就可以了。

最新回复(0)