3.2.3 消费者3.2.3.1 消费者One/** * Created On : 1/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQFanoutConsumerOne */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_FANOUT_QUEUE_NAME_KH96_ONE)public class RabbitMQFanoutConsumerOne {@RabbitHandlerpublic void fanoutConsumeOneFanoutMsgFromFanoutQueueOne(String fanoutMsgJson){log.info("****** Fanout扇形模式,消费One,消费队列One,消息:{}******",fanoutMsgJson);// TODO 核心业务逻辑处理}}3.2.3.2 消费者Two/** * Created On : 1/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQFanoutConsumerTwo */@Slf4j@Component//@RabbitListener(queues = RabbitMQConstant.RABBITMQ_FANOUT_QUEUE_NAME_KH96_TWO)public class RabbitMQFanoutConsumerTwo {//@RabbitHandlerpublic void fanoutConsumeTwoFanoutMsgFromFanoutQueueTwo(String fanoutMsgJson){log.info("****** Fanout扇形模式,消费Two,消费队列Two,消息:{}******",fanoutMsgJson);// TODO 核心业务逻辑处理}}3.2.4 请求测试方法/** * Created On : 1/11/2022. * <p> * Author : huayu * <p> * Description: 测试 RabbitMQ 消息队列的操作入口 */@Slf4j@RestControllerpublic class RabbitMQController {@Autowiredprivate RabbitMQFanoutProducer rabbitMQFanoutProducer;/*** @author : huayu* @date: 1/11/2022* @param: [fanoutMsg]* @return : com.kgc.scd.uitl.RequestResult<java.lang.String>* @description : 测试扇形(广播)模式,发送和消费信息*/@GetMapping("/fanout")public RequestResult<String> testRabbitMQFanout(@RequestParam String fanoutMsg){log.info("------- fanout 扇形模式,发送消息 -------");//模拟发送5条直连消息Stream.of(66,77,88,99,96).forEach(directNo ->{//模拟创建消息对象Map<String,Object> fanoutMap =new HashMap<>();fanoutMap.put("directNo",directNo);fanoutMap.put("directData",fanoutMsg);fanoutMap.put("directTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//调用扇形模式消息生产者,发送消息rabbitMQFanoutProducer.sendFanoutMsg2FanoutExchange(RabbitMQConstant.RABBITMQ_FANOUT_EXCHANGE_KH96,null,JSON.toJSONString(fanoutMap));});return ResultBuildUtil.success("使用扇形模式 。发送消息成功");}}3.2.5 请求测试

文章插图
3.2.5.1 一个消费者消费者One消费了队列One中的所有信息;

文章插图
3.2.5.2 两个消费者消费者One消费了队列One中的所有信息;
消费者Two消费了队列Two中的所有信息;

文章插图
3.3 Topic 主题模式3.3.1 配置类
/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: Topic 主题模式,自动配置类 */@Configurationpublic class RabbitMQTopicConfig {//======== 队列//Topic 主题模式 队列One@Beanpublic Queue topicQueueOne(){return new Queue(RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_ONE,true);}//Topic 主题模式 队列Two@Beanpublic Queue topicQueueTwo(){return new Queue(RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_TWO,true);}//Topic 主题模式 队列Three@Beanpublic Queue topicQueueThree(){return new Queue(RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_THREE,true);}//======= 交换机//Topic 主题模式 交换机@Beanpublic TopicExchange topicExchange(){return new TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96);}//======= 绑定// 队列One 绑定 Topic主题模式交换机和 路由键-唯一匹配规则@Beanpublic Binding topicBindingQueueOne(){return BindingBuilder.bind(topicQueueOne()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_TOPIC_ROUTING_KEY_KH96_ONLY);}// 队列Two 绑定 Topic主题模式交换机和 路由键-单个单词词匹配规则@Beanpublic Binding topicBindingQueueTwo(){return BindingBuilder.bind(topicQueueTwo()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_TOPIC_ROUTING_KEY_KH96_WORLD);}// 队列Two 绑定 Topic主题模式交换机和 路由键-模糊匹配规则@Beanpublic Binding topicBindingQueueThree(){return BindingBuilder.bind(topicQueueThree()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_TOPIC_ROUTING_KEY_KH96_LIKE);}}
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 六盘水有什么特色美食 贵州六盘水最有名的七大美食
- 浙江丽水最有名的六大名菜
- 河南菜有哪些特色菜 河南最有名的六大特色名菜
- 北京菜有哪些特色菜 北京最有名的六大特色名菜
- 陕西菜有哪些特色菜 陕西最有名的六大特色名菜
- 2023年1月27日入宅好不好 2023年农历正月初六入宅吉日
- 2023年农历正月初六珠宝开光吉日 2023年1月27日珠宝开光黄道吉日
- 2023年1月27日祭祀吉日一览表 2023年农历正月初六祭祀吉日
- 2023年1月27日拜神行吗 2023年农历正月初六宜拜神吗
- 2023年1月27日请佛好吗 2023年农历正月初六请佛吉日
