Spring Boot:整合RabbitMQ
一.pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.3 . 0 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wj</groupId> <artifactId>boot-rabbit</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>boot-rabbit</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
二.application.yml
spring:
rabbitmq:
addresses: amqp://guest:guest@192.168.10.132:5672
virtual-host: /
connection-timeout: 10000
#生产端配置
publisher-confirm-type: CORRELATED #实现一个监听器用于监听Broker端给我们返回确认请求
publisher-returns: true #保证消息对Broker端是可达的
template:
mandatory: true #这个属性必须设置为true,才能保证监听有效
#消费端配置
listener:
simple:
#配置手工确认模式,用于ACK的手工处理
acknowledge-mode: manual
#配置消费端监听个数和最大个数用于控制消费端并发情况
concurrency: 5
max-concurrency: 10
三.配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Configuration public class MainConfig { @Bean public TopicExchange topicExchange(){ return new TopicExchange("boot-exchange",true,false); } @Bean public Queue queue(){ return new Queue("boot.queue",true) ; } @Bean public Binding binding(){ return BindingBuilder.bind(this.queue()).to(topicExchange()).with("springboot.#"); } } |
四.生产端代码
生产者类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | @Component public class Producer { @Autowired private RabbitTemplate rabbitTemplate; RabbitTemplate.ConfirmCallback confirmCallback = (correlationData, ack, cause) -> { System.out.println(correlationData); System.out.println(ack); if(!ack){ System.out.println("异常处理中。。"); } }; ReturnCallback returnCallback = (message, replyCode, replyText, exchange, routingKey) -> { System.out.println(exchange +":"+routingKey); System.out.println(replyCode+":"+replyText); System.out.println(new String(message.getBody())); }; public void send(Object message, Map< String ,Object> properties){ MessageHeaders headers = new MessageHeaders(properties); Message msg = MessageBuilder.createMessage(message, headers); rabbitTemplate.setConfirmCallback(confirmCallback); rabbitTemplate.setReturnCallback(returnCallback); //消息的全局唯一id CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); rabbitTemplate.convertAndSend("boot-exchange","springboot.rabbit",msg,correlationData); } } |
测试代码:
1 2 3 4 5 6 7 8 9 10 | @Autowired private Producer producer; @Test public void testSend(){ Map< String , Object> map = new HashMap<>(); map.put("name","wj"); map.put("id","1234"); producer.send("hello spring boot amqp",map); } |
测试结果:
五.消费端代码
消费端监听注解@RabbitListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Component public class Consumer { @RabbitListener ( bindings = @QueueBinding ( value = @Queue (value = "boot.queue" , durable = "true" ) , exchange = @Exchange ( value = "boot-exchange" ,type = "topic" ,ignoreDeclarationExceptions = "true" ) ,key = "springboot.#" ) ) @RabbitHandler public void onMessage(Message message, Channel channel){ Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG); System.out.println(message.getPayload()); } } |
六.发送实体类消息
注意:实体类需要实现序列化接口,否则报错
生产端:
1 2 3 4 5 | public void sendUser(User user,Map<String,Object> properties){ //消息的全局唯一id CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); rabbitTemplate.convertAndSend( "boot-exchange" , "springboot.rabbit" ,user,correlationData); } |
消费端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @RabbitListener ( bindings = @QueueBinding ( value = @Queue (value = "boot.queue" , durable = "true" ) , exchange = @Exchange ( value = "boot-exchange" ,type = "topic" ,ignoreDeclarationExceptions = "true" ) ,key = "springboot.#" ) ) @RabbitHandler public void onMessage( @Payload User user, Channel channel, @Headers Map<String,Object> properties){ System.out.println(user); System.out.println(properties.get(AmqpHeaders.DELIVERY_TAG)); } |
测试:
1 2 3 4 5 | @Test public void sendUser(){ User user = new User( "张三" , 12 ); producer.sendUser(user, null ); } |
标签:
Spring Boot
, RabbitMQ
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix