java——spring boot集成RabbitMQ——spring boot实现路由模式——消费者
pom文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springrmqtopicreceiver</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> </project>
yml配置:
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
config配置:
package org.example.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 主题交换机 * topic策略可以根据routingKey的规则(通配符方式)进行去匹配队列进行转发规则为*.#.* */ @Configuration public class RabbitTopicConfig { public final static String TOPIC_NAME = "amqp-topic"; @Bean TopicExchange topicExchange(){ return new TopicExchange(TOPIC_NAME,true,false); } @Bean Queue xiaomi(){ return new Queue("xiaomi"); } @Bean Queue huawei(){ return new Queue("huawei"); } @Bean Binding xiaomiBinding(){ //xiaomi.#:表示消息的routingKey是以xiaomi开头的就会路由到xiaomi的队列 return BindingBuilder.bind(xiaomi()).to(topicExchange()).with("xiaomi.#"); } @Bean Binding huaweiBinding(){ return BindingBuilder.bind(huawei()).to(topicExchange()).with("huawei.#"); } }
接收:
package org.example.receiver; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * * @Auther: moerhai@qq.com * @Date: 2020/10/4 14:36 */ @Component public class TopicReceiver { //分别监听名称为xiaomi、huawei的队列 @RabbitListener(queues = "xiaomi") public void handlerXM(String msg){ System.out.println("TopicReceiver:handlerXM>>>"+msg); } @RabbitListener(queues = "huawei") public void handlerHW(String msg){ System.out.println("TopicReceiver:handlerHW>>>"+msg); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构