企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
:-: ![](https://img.kancloud.cn/ef/5b/ef5b1d9168429d2b18d0bfd7abc69c94_1123x326.png) 生产消费过程 **1. 交换机与队列绑定** ```java @Configuration public class RabbitConfig { public final static String THREE_EXCHAGE = "three.exchange"; public final static String JUPITER_QUEUE = "jupiter.queue"; public final static String SATURN_QUEUE = "saturn.queue"; public final static String JUPITER_MIDDLE_KEY = "*.jupiter.*"; public final static String SATURN_PREFIX_KEY = "saturn.*"; public final static String SATURN_SUFFIX_KEY = "*.saturn"; /** * 创建Topic交换机 */ @Bean("threeExchange") public TopicExchange threeExchange() { return new TopicExchange(THREE_EXCHAGE); } /** * 创建jupiter队列 */ @Bean("jupiterQueue") public Queue jupiterQueue() { return new Queue(JUPITER_QUEUE); } /** * 创建saturn队列 */ @Bean("saturnQueue") public Queue saturnQueue() { return new Queue(SATURN_QUEUE); } /** * jupiter队列与交换机绑定 */ @Bean public Binding jupiterBinding(@Qualifier("jupiterQueue") Queue queue, @Qualifier("threeExchange") TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(JUPITER_MIDDLE_KEY); } /** * saturn队列与交换机绑定,key=SATURN_PREFIX_KEY */ @Bean public Binding saturn01Binding(@Qualifier("saturnQueue") Queue queue, @Qualifier("threeExchange") TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(SATURN_PREFIX_KEY); } /** * saturn队列与交换机绑定,key=SATURN_SUFFIX_KEY */ @Bean public Binding saturn02Binding(@Qualifier("saturnQueue") Queue queue, @Qualifier("threeExchange") TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(SATURN_SUFFIX_KEY); } } ``` **2. 生产者生产消息** ```java @Slf4j @RestController @RequiredArgsConstructor public class ProducerController { final RabbitTemplate rabbitTemplate; @GetMapping("/topic/produce") public void topicProduce() { Map<String, Object> msgBody = new HashMap<>(16); msgBody.put("name", "zhangsan"); msgBody.put("key", "easy.jupiter.yes"); //convertAndSend(String exchange, String routingKey, Object object) rabbitTemplate.convertAndSend(RabbitConfig.THREE_EXCHAGE, "easy.jupiter.yes", msgBody); log.info("[topicProduce|生产了消息]: {}", msgBody); msgBody.put("key", "saturn.world"); rabbitTemplate.convertAndSend(RabbitConfig.THREE_EXCHAGE, "saturn.world", msgBody); log.info("[topicProduce|生产了消息]: {}", msgBody); msgBody.put("key", "much.saturn"); rabbitTemplate.convertAndSend(RabbitConfig.THREE_EXCHAGE, "much.saturn", msgBody); log.info("[topicProduce|生产了消息]: {}", msgBody); } } ``` **3. 消费者监听队列** ```java @Slf4j @Component public class RabbitMQListener { /** * 监听jupiter队列 */ @RabbitListener(queues = RabbitConfig.JUPITER_QUEUE) public void jupiterConsume(Message<Map<String, Object>> message, Channel channel) { log.info("[jupiterConsume|收到了消息]: {}", message.getPayload()); } /** * 监听saturn队列 */ @RabbitListener(queues = RabbitConfig.SATURN_QUEUE) public void saturnConsume(Message<Map<String, Object>> message, Channel channel) { log.info("[saturnConsume|收到了消息]: {}", message.getPayload()); } } ``` **4. 测试结果** ``` : [topicProduce|生产了消息]: {name=zhangsan, key=easy.jupiter.yes} : [topicProduce|生产了消息]: {name=zhangsan, key=saturn.world} : [topicProduce|生产了消息]: {name=zhangsan, key=much.saturn} : [jupiterConsume|收到了消息]: {name=zhangsan, key=easy.jupiter.yes} : [saturnConsume|收到了消息]: {name=zhangsan, key=saturn.world} : [saturnConsume|收到了消息]: {name=zhangsan, key=much.saturn} ```