控制台打印 ,发现两个绑定了不同队列的消费者都接受到了同一条消息查看RabbitMq 服务器:
5. 路由模式(Route模式 Direct定向) 5.1 编写路由配置类 package com.gmtgo.demo.direct; 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; /** * @author 大帅 */ @Configuration public class DirectQueueConfig { /** * 声明队列名. */ private final String direct1 = "direct_queue_1"; private final String direct2 = "direct_queue_2"; /** * 声明交换机的名字. */ private final String directExchange = "directExchange"; /** * 声明队列. * * @return */ @Bean public Queue directQueue1() { return new Queue(direct1); } @Bean public Queue directQueue2() { return new Queue(direct2); } /** * 声明路由交换机. * * @return */ @Bean public DirectExchange directExchange() { return new DirectExchange(directExchange); } /** * 队列绑定交换机,指定routingKey,也可在可视化工具中进行绑定. * * @return */ @Bean Binding bindingDirectExchange1(Queue directQueue1, DirectExchange exchange) { return BindingBuilder.bind(directQueue1).to(exchange).with("update"); } /** * 队列绑定交换机,指定routingKey,也可在可视化工具中进行绑定. * * @return */ @Bean Binding bindingDirectExchange2(Queue directQueue2, DirectExchange exchange) { return BindingBuilder.bind(directQueue2).to(exchange).with("add"); } } 5.2 编写生产者 package com.gmtgo.demo.direct; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * @author 大帅 */ @Slf4j @Component public class DirectProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessageA() { for (int i = 0; i < 5; i++) { String message = "路由模式--routingKey=update消息" + i; log.info("我是生产信息:{}", message); rabbitTemplate.convertAndSend("directExchange", "update", message); } } public void sendMessageB() { for (int i = 0; i < 5; i++) { String message = "路由模式--routingKey=add消息" + i; log.info("我是生产信息:{}", message); rabbitTemplate.convertAndSend("directExchange", "add", message); } } } 5.3 编写消费者1 package com.gmtgo.demo.direct; import com.rabbitmq.client.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.io.IOException; /** * @author 大帅 */ @Slf4j @Component public class DirectConsumers1 { @RabbitListener(queues = "direct_queue_1") public void readMessage(Message message, Channel channel) throws IOException { channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); log.info("我是消费信息1:{}", new String(message.getBody())); } } 5.4 编写消费者2 package com.gmtgo.demo.direct; import com.rabbitmq.client.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.io.IOException; /** * @author 大帅 */ @Slf4j @Component public class DirectConsumers2 { @RabbitListener(queues = "direct_queue_2") public void readMessage(Message message, Channel channel) throws IOException { channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); log.info("我是消费信息2:{}", new String(message.getBody())); } } 5.5 编写访问类 package com.gmtgo.demo.direct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 大帅 */ @RestController @RequestMapping(value = "rabbitMq") public class DirectRabbitMqController { @Autowired private DirectProducer directProducer; @RequestMapping(value = "directQueueTest1") public String directQueueTest1() { directProducer.sendMessageA(); return "success"; } @RequestMapping(value = "directQueueTest2") public String directQueueTest2() { directProducer.sendMessageB(); return "success"; } } 5.6 测试启动项目访问directQueueTest1 , directQueueTest2
访问地址:8801/rabbitMq/directQueueTest1
访问地址:8801/rabbitMq/directQueueTest2
结果:
directQueueTest1: