这篇文章主要讲解了“RabbitMQ主题模式怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“RabbitMQ主题模式怎么实现”吧!
Springboot RabbitMQ 主题模式
生产者声明了三个队列和一个主题切换。队列分别与主题交换机绑定,并设置了路由键统一字符。如果路由键满足交换机和队列之间的通配符要求,则将消息存储在队列中
#通配符可以匹配一个或多个单词,*通配符可以匹配一个单词;如果Exchange和队列之间的routing key通配符是#hello.#,则表示中间所有带hello的routing key都满足条件,消息会被存入队列
导入 com.example.rabbitmq.constant.RabbitConstant;导入 org.springframework.amqp.core.Binding;导入 org.springframework.amqp.core.BindingBuilder;导入 org.springframework.amqp.core.Queue;导入 org.springframework.amqp.core.TopicExchange;导入 org.springframework.context.annotation.Bean;导入 org.springframework.context.annotation.Configuration;@Configuration public class RabbitTopicProvider { @Bean public Queue topicFirstQueue() { return new Queue(RabbitConstant.TOPICS_FIRST_QUEUE_NAME); } @Bean 公共队列 topicSecondQueue() { return new Queue(RabbitConstant.TOPICS_SECOND_QUEUE_NAME); } @Bean 公共队列 topicThirdQueue() { return new Queue(RabbitConstant.TOPICS_THIRD_QUEUE_NAME); } @Bean public TopicExchange topicExchange() { // 创建一个主题类型切换,表示交换机会发送消息到 routing_key 通配符匹配队列成功 return new TopicExchange(RabbitConstant.TOPICS_EXCHANGE_NAME); } @Bean public Binding topicFirstQueueBindExchange() { // 绑定topic类型切换到队列1并设置routing_key通配符#first.# return BindingBuilder.bind(topicFirstQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_FIRST_WILDCARD); } @Bean public Binding topicSecondQueueBindExchange() { //第二个队列绑定主题类型切换,设置路由_key通配符为* second.# return BindingBuilder.bind(topicSecondQueue()).to(topicExchange()) .with(RabbitConstant.TOPICS_ROUTING_KEY_SECOND_WILDCARD); } @Bean public Binding topicThirdQueueBindExchange() { // 三个队列绑定主题切换,设置routing_key通配符为*third.* return BindingBuilder.bind(topicThirdQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_THRID_WILDCARD); } }
消费者监听队列并消费
导入 com.example.rabbitmq.constant.RabbitConstant;导入 org.springframework.amqp.rabbit.annotation.RabbitHandler;导入 org.springframework.amqp.rabbit.annotation.RabbitListener;导入 org.springframework.stereotype.Component;@Component public class RabbitTopicsConsumer { @RabbitListener(queues = RabbitConstant.TOPICS_FIRST_QUEUE_NAME) @RabbitHandler public void topicFirstQueue(String context) { System.out.println("rabbit topics queue first receiver:" + context); } @RabbitListener(queues = RabbitConstant.TOPICS_SECOND_QUEUE_NAME) @RabbitHandler public void topicSecondQueue(String context) { System.out.println("兔子主题队列第二个接收者:" + context); } @RabbitListener(queues = RabbitConstant.TOPICS_THIRD_QUEUE_NAME) @RabbitHandler public void topicThirdQueue(String context) { System.out.println("rabbit 主题队列第三个接收者:" + context); } }
单元测试
@Test public void topics() { // 向第一个队列发送消息。这时候队列可以接收到消息,因为队列的通配符是#first.#,而routing_key是topics first。路由。键,匹配成功 rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_FIRST_QUEUE_ROUTING_KEY, "topics hello"); // 向第二个队列发送消息。这时候队列也能收到消息了,因为队列的通配符是*秒#,而routing_key是topic秒。路由。键,匹配成功 rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_SECOND_QUEUE_ROUTING_KEY, "topics hello"); // 向第三个队列发送消息。此时队列无法接受消息,因为队列通配符是*第三个*,而routing_key是topics第三个。路由。键,匹配失败 rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_THIRD_QUEUE_ROUTING_KEY, "topics hello"); }
响应结果
感谢各位的阅读,以上就是“RabbitMQ主题模式怎么实现”的内容了,经过本文的学习后,相信大家对RabbitMQ主题模式怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!