六 SpringCloud - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)( 四 )

3.2.5 请求测试

六 SpringCloud - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)

文章插图
3.2.5.1 一个消费者消费者One消费了队列One中的所有信息;
六 SpringCloud - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)

文章插图
3.2.5.2 两个消费者消费者One消费了队列One中的所有信息;
消费者Two消费了队列Two中的所有信息;
六 SpringCloud - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)

文章插图
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);}}3.3.2 消息生产者/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQ 主题模式消息生产者 */@Slf4j@Componentpublic class RabbitMQTopicProducer {@Autowiredprivate RabbitTemplate rabbitTemplate;/*** @author : huayu* @date: 1/11/2022* @param: [topicExchange, topicRoutingKey, topicMsg]* @return : void* @description : 使用主题模式,发送消息到主题交换机,主题交换机会根据发送消息的路由键  , 根据匹配规则将消息投递到匹配的队列中*/public void sendTopicMsg2TopicExchange(String topicExchange,String topicRoutingKey,String topicMsg){log.info("++++++direct模式消息生产者,发送直连消息:{},到交换机:{} , 路由键:{} ++++++",topicMsg,topicExchange,topicRoutingKey);rabbitTemplate.convertAndSend(topicExchange,topicRoutingKey,topicMsg);}}3.3.3 消费者3.3.3.1 消费者One/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQTopicConsumerOne */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_ONE)public class RabbitMQTopicConsumerOne {@RabbitHandlerpublic void consumeTopicMsgFromTopicQueue(String topicMapJson){log.info("****** Topic 主题模式 , 消费One,消费队列One,消息:{}******",topicMapJson);// TODO 核心业务逻辑处理}}3.3.3.2 消费者Two/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQTopicConsumerTwo */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_TWO)public class RabbitMQTopicConsumerTwo {@RabbitHandlerpublic void consumeTopicMsgFromTopicQueue(String topicMapJson){log.info("****** Topic 主题模式,消费 Two,消费队列 Two,消息:{}******",topicMapJson);// TODO 核心业务逻辑处理}}3.3.3.3 消费者Three/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQTopicConsumerThree */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_THREE)public class RabbitMQTopicConsumerThree {@RabbitHandlerpublic void consumeTopicMsgFromTopicQueue(String topicMapJson){log.info("****** Topic 主题模式,消费 Three,消费队列 Three,消息:{}******",topicMapJson);// TODO 核心业务逻辑处理}}3.3.4 请求测试方法/** * Created On : 1/11/2022. * <p> * Author : huayu * <p> * Description: 测试 RabbitMQ 消息队列的操作入口 */@Slf4j@RestControllerpublic class RabbitMQController {@Autowiredprivate RabbitMQTopicProducer rabbitMQTopicProducer;@GetMapping("/topic")public RequestResult<String> testRabbitMQTopic(@RequestParam String topicMsg){log.info("------- topic 主题模式,发送消息 -------");//模拟发送5条直连消息Stream.of(95,96,97,98,99).forEach(directNo ->{//模拟创建消息对象Map<String,Object> fanoutMap =new HashMap<>();fanoutMap.put("directNo",directNo);fanoutMap.put("directData",topicMsg);fanoutMap.put("directTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//调用主题模式消息生产者 , 发送消息//场景1:使用唯一路由键 rabbitmq_topic_routing_key_kh96.only , 发送消息rabbitMQTopicProducer.sendTopicMsg2TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96,RabbitMQConstant.RABBITMQ_TOPIC_ROUTING_KEY_KH96_ONLY,JSON.toJSONString(fanoutMap));//场景2:使用单词匹配路由键 rabbitmq_topic_routing_key_kh96.*,发送消息//rabbitMQTopicProducer.sendTopicMsg2TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96//,"rabbitmq_topic_routing_key_kh96.abc"//,JSON.toJSONString(fanoutMap));//场景3:0 或多词匹配 rabbitmq_topic_routing_key_kh96.#,发送消息//rabbitMQTopicProducer.sendTopicMsg2TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96//,"rabbitmq_topic_routing_key_kh96.abc.def"//,JSON.toJSONString(fanoutMap));});return ResultBuildUtil.success("使用主题模式 。发送消息成功");}}

推荐阅读