多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
:-: ![](https://img.kancloud.cn/b7/d0/b7d0a1a0de554b236a738103012a711a_1304x304.jpg) 生产消费过程 **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_SUFIX_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_SUFIX_KEY */ @Bean public Binding saturn02Binding(@Qualifier("saturnQueue") Queue queue, @Qualifier("threeExchange") TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(SATURN_SUFIX_KEY); } } ``` **2. 生产者** ```java @Slf4j @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @RequestMapping("/topic/produce") public void topicProduce() { Map<String, Object> msgBody = new HashMap<>(16); msgBody.put("name", "zhangsan"); msgBody.put("age", 25); 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 @Service public class ConsumerService { /** * 监听jupiter队列 */ @RabbitListener(queues = RabbitConfig.JUPITER_QUEUE) public void jupiterConsume(Message<Map<String, Object>> message, Channel channel) { Map<String, Object> msgBody = message.getPayload(); log.info("jupiterConsume[收到了消息]:{}", msgBody); } /** * 监听saturn队列 */ @RabbitListener(queues = RabbitConfig.SATURN_QUEUE) public void saturnConsume(Message<Map<String, Object>> message, Channel channel) { Map<String, Object> msgBody = message.getPayload(); log.info("saturnConsume[收到了消息]:{}", msgBody); } } ``` **4. 测试结果** ``` : topicProduce[生产了消息]:{name=zhangsan, age=25, key=easy.jupiter.yes} : topicProduce[生产了消息]:{name=zhangsan, age=25, key=saturn.world} : topicProduce[生产了消息]:{name=zhangsan, age=25, key=much.saturn} : jupiterConsume[收到了消息]:{name=zhangsan, age=25, key=easy.jupiter.yes} : saturnConsume[收到了消息]:{name=zhangsan, age=25, key=saturn.world} : saturnConsume[收到了消息]:{name=zhangsan, age=25, key=much.saturn} ```