行业知识
SpringBoot整合rabbitmq-主题交换机队列(四)
2026-11-06 19:57:43
来源:互联网转载
说���:Topic主题交换机它的大致流程是交换机和一个或者多个队列绑定,这个绑定的Routingkey是包含通配符的,满足通配符的队列会接收到消息。
通配符规则:
#:匹配一个或多个词
*:匹配一个词
例如:
topic.#:能匹配 topic.xxx 或者 topic.xxx.xxx
topic.*:只能匹配 topic.xxx
工程图:
A.总体maven依赖
pom.xml
4.0.0 spring-boot-starter-parent org.springframework.boot 2.2.2.RELEASE RabbitMqSpringbootDemo RabbitMqSpringbootDemo 1.0-SNAPSHOT pom MqCustomer MqProducer RabbitMqSpringbootDemo Maven Webapp http://www.example.com UTF-8 1.8 1.8 RabbitMqSpringbootDemo maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
B.生产者
1.pom.xml
RabbitMqSpringbootDemo RabbitMqSpringbootDemo 1.0-SNAPSHOT 4.0.0 MqProducer jar MqProducer Maven Webapp http://www.example.com UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-starter-amqp com.alibaba fastjson 1.2.78 org.apache.commons commons-lang3 3.4 org.projectlombok lombok 1.16.10 MqProducer maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
2.application.yml
server: port: 8080spring: rabbitmq: port: 5672 host: 192.168.18.145 username: admin password: admin virtual-host: /
3.TopicRabbitConfig
package com.dev.config;import org.springframework.amqp.core.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 类名称:主题交换机的使用 * * @author lqw * @date 2024年02月28日 10:40 */@Configurationpublic class TopicRabbitConfig { //绑定键 public final static String high = "topic.highSchool.one"; public final static String middle = "topic.middleSchool.one"; @Bean public Queue oneQueue() { return new Queue(TopicRabbitConfig.high); } @Bean public Queue twoQueue() { return new Queue(TopicRabbitConfig.middle); } @Bean TopicExchange exchange() { return new TopicExchange("topicExchange"); } @Bean Binding bindingExchangeMessageA() { return BindingBuilder.bind(oneQueue()).to(exchange()).with("topic.highSchool.#"); } @Bean Binding bindingExchangeMessageB() { return BindingBuilder.bind(twoQueue()).to(exchange()).with("topic.middleSchool.#"); }} 4.TopicRabbitController
package com.dev.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * 类名称:主题交换机 消息生产者 * * @author lqw * @date 2024年02月27日 14:47 */@Slf4j@RestController@RequestMapping("topic")public class TopicRabbitController { @Autowired RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法 /** * topic测试 * @return */ @GetMapping("/sendMessageA")//匹配到 topic.highSchool.one public String sendMessageA() { Map map = new HashMap(); map.put("name","张龙"); map.put("school","高中"); rabbitTemplate.convertAndSend("topicExchange", "topic.highSchool.two", map); return "ok"; } @GetMapping("/sendMessageB")//匹配到 topic.middleSchool.one public String sendMessageB() { Map map = new HashMap(); map.put("name","赵虎"); map.put("school","初中"); rabbitTemplate.convertAndSend("topicExchange", "topic.middleSchool.two", map); return "ok"; } @GetMapping("/sendMessageBB")//匹配到 topic.middleSchool.one public String sendMessageBB() { Map map = new HashMap(); map.put("name","赵虎B"); map.put("school","初中B"); rabbitTemplate.convertAndSend("topicExchange", "topic.middleSchool.two.cc", map); return "ok"; }} 5.AppP
package com.dev;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 类名称: * * @author 李庆伟 * @date 2024年02月27日 14:20 */@SpringBootApplicationpublic class AppP { public static void main(String[] args) { SpringApplication.run(AppP.class); }} C.消费者
1.pom.xml
RabbitMqSpringbootDemo RabbitMqSpringbootDemo 1.0-SNAPSHOT 4.0.0 MqCustomer jar MqCustomer Maven Webapp http://www.example.com UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-starter-amqp org.apache.commons commons-lang3 3.4 org.projectlombok lombok 1.16.10 MqCustomer maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
2.application.yml
server: port: 8081spring: rabbitmq: port: 5672 host: 192.168.18.145 username: admin password: admin
3.TopicRabbitListenerA
package cn.ct.listeners;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;import java.util.Map;/** * 类名称:主题交换机,消息消费者 * 直连 * @author lqw * @date 2024年02月27日 14:58 */@Componentpublic class TopicRabbitListenerA { @RabbitListener(queues = "topic.highSchool.one") @RabbitHandler public void process(Map msg) { System.out.println("Rabbitmq topic : " + msg); System.out.println("Rabbitmq topic : " + msg); }} 4.TopicRabbitListenerB
package cn.ct.listeners;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;import java.util.Map;/** * 类名称:主题交换机,消息消费者 * 直连 * @author lqw * @date 2024年02月27日 14:58 */@Componentpublic class TopicRabbitListenerB { @RabbitListener(queues = "topic.middleSchool.one") @RabbitHandler public void process(Map msg) { System.out.println("Rabbitmq topic : " + msg); System.out.println("Rabbitmq topic : " + msg); }} 5.AppC
package cn.ct;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 类名称: * * @author lqw * @date 2024年02月27日 14:20 */@SpringBootApplicationpublic class AppC { public static void main(String[] args) { SpringApplication.run(AppC.class); }}