这篇文章主要讲解了“RabbitMQ路由方式实例代码分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“RabbitMQ路由方式实例代码分析”吧!
SpringBoot RabbitMQ 路由方式
生产者声明三个队列和一个直接交换机,将三个队列绑定到交换机,并设置交换机和队列之间的路由
import com.example.rabbitmq.constant.RabbitConstant;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class RabbitRoutingProvider { @Bean public Queue rabbitRoutingFirstQueue() { return new Queue(RabbitConstant.ROUTING_FIRST_QUEUE_NAME); } @Bean public Queue rabbitRoutingSecondQueue() { return new Queue(RabbitConstant.ROUTING_SECOND_QUEUE_NAME); } @Bean public Queue rabbitRoutingThirdQueue() { return new Queue(RabbitConstant.ROUTING_THIRD_QUEUE_NAME); } @Bean public DirectExchange directExchange() { // Create a direct switch, indicating that the exchange opportunity sends messages to routing_ Queue with identical key return new DirectExchange(RabbitConstant.ROUTING_EXCHANGE_NAME); } @Bean public Binding routingFirstQueueBindDirectExchange() { // Bind the direct switch to the queue and set the routing_key is routing_first_queue_routing_key return BindingBuilder.bind(rabbitRoutingFirstQueue()).to(directExchange()).with(RabbitConstant.ROUTING_FIRST_QUEUE_ROUTING_KEY_NAME); } @Bean public Binding routingSecondQueueBindDirectExchange() { // Queue 2 binds the direct switch and sets routing_key is routing_second_queue_routing_key return BindingBuilder.bind(rabbitRoutingSecondQueue()).to(directExchange()).with(RabbitConstant.ROUTING_SECOND_QUEUE_ROUTING_KEY_NAME); } @Bean public Binding routingThirdQueueBindDirectExchange() { // The queue 3 is bound to the direct switch and the routing is set_ Key is routing_third_queue_routing_key return BindingBuilder.bind(rabbitRoutingThirdQueue()).to(directExchange()).with(RabbitConstant.ROUTING_THIRD_QUEUE_ROUTING_KEY_NAME); } }
消费者监听队列并消费
导入 com.example.rabbitmq.constant.RabbitConstant;导入 org.springframework.amqp.rabbit.annotation.RabbitHandler;导入 org.springframework.amqp.rabbit.annotation.RabbitListener;导入 org.springframework.stereotype.Component;@Component public class RabbitRoutingConsumer { @RabbitListener(queues = RabbitConstant.ROUTING_FIRST_QUEUE_NAME) @RabbitHandler public void routingFirstQueueListener(String context) { System.out.println("rabbit routing queue first receiver:" + context); } @RabbitListener(queues = RabbitConstant.ROUTING_SECOND_QUEUE_NAME) @RabbitHandler public void routingSecondQueueListener(String context) { System.out.println("rabbit pubsub 队列第二个接收者:" + context); } @RabbitListener(queues = RabbitConstant.ROUTING_THIRD_QUEUE_NAME) @RabbitHandler public void routingThirdQueueListener(String context) { System.out.println("rabbit pubsub 队列第三个接收者:" + context); } }
单元测试
@Test public void routing() { // 向第一个队列发送消息 rabbitTemplate.convertAndSend(RabbitConstant.ROUTING_EXCHANGE_NAME, RabbitConstant.ROUTING_FIRST_QUEUE_ROUTING_KEY_NAME, "routing hello"); }
响应结果
感谢各位的阅读,以上就是“RabbitMQ路由方式实例代码分析”的内容了,经过本文的学习后,相信大家对RabbitMQ路由方式实例代码分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!