RabbitMQ 作为一款高性能的消息中间件,被广泛应用于微服务架构中的异步通信、解耦、削峰填谷等场景。而 SpringBoot 凭借其 “约定优于配置” 的特性,极大简化了与 RabbitMQ 的整合过程。本文将通过注解驱动的方式,实现一个最简的 SpringBoot + RabbitMQ 案例,涵盖生产者发送消息、消费者接收消息的核心流程。
一、环境准备
-
RabbitMQ 服务部署首先需要本地或服务器部署 RabbitMQ 服务,可通过 Docker 快速启动(推荐):
# 拉取 RabbitMQ 镜像(带管理界面) docker pull rabbitmq:3-management # 启动容器,映射端口(5672:通信端口,15672:管理界面端口) docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management启动后,访问
http://localhost:15672,默认账号密码为guest/guest,可进入 RabbitMQ 管理界面。 -
SpringBoot 项目创建使用 Spring Initializr 创建一个 SpringBoot 项目,引入以下核心依赖:
- Spring Web:(可选)用于通过接口触发消息发送
- Spring for RabbitMQ:RabbitMQ 整合核心依赖
Maven 依赖配置(
pom.xml):<dependencies> <!-- Spring Boot Web 依赖(可选,用于测试) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot RabbitMQ 依赖 --> <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> </dependency> </dependencies>
二、核心配置:RabbitMQ 连接与队列声明
1. 配置 RabbitMQ 连接信息
在 application.yml(或 application.properties)中配置 RabbitMQ 的连接参数,SpringBoot 会自动加载这些配置并创建连接工厂:
spring:
# RabbitMQ 配置
rabbitmq:
host: localhost # RabbitMQ 服务地址
port: 5672 # 通信端口
username: guest # 用户名
password: guest # 密码
virtual-host: / # 虚拟主机(默认/)
2. 注解声明队列(核心)
Spring AMQP 提供了 @Queue 和 @Exchange 注解,可通过配置类快速声明队列、交换机及绑定关系。本文使用最简的默认交换机(Direct Exchange) + 队列直连方式:
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* RabbitMQ 配置类:声明队列
*/
@Configuration
public class RabbitMQConfig {
/**
* 定义队列名称常量
*/
public static final String SIMPLE_QUEUE = "simple_queue";
/**
* 声明队列:默认是持久化、非排他、非自动删除的队列
* @return 队列对象
*/
@Bean
public Queue simpleQueue() {
// Queue(String name, boolean durable, boolean exclusive, boolean autoDelete)
return new Queue(SIMPLE_QUEUE, true, false, false);
}
}
说明:默认交换机是 RabbitMQ 的内置交换机,名称为空字符串,类型为 Direct,会将消息路由到与 routing key 同名的队列。
三、生产者开发:发送消息
使用 Spring 提供的 RabbitTemplate 来发送消息,这是 Spring AMQP 封装的核心工具类,支持多种消息发送方式。
1. 生产者组件
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 消息生产者
*/
@Component
public class MessageProducer {
/**
* 注入 RabbitTemplate,用于发送消息
*/
@Resource
private RabbitTemplate rabbitTemplate;
/**
* 发送消息到指定队列
* @param message 要发送的消息内容
*/
public void sendMessage(String message) {
// convertAndSend(队列名/路由键, 消息内容)
rabbitTemplate.convertAndSend(RabbitMQConfig.SIMPLE_QUEUE, message);
System.out.println("生产者发送消息:" + message);
}
}
2. 测试接口(可选)
为了方便测试,创建一个 Web 接口,触发消息发送:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 测试接口:触发消息发送
*/
@RestController
public class MessageController {
@Resource
private MessageProducer messageProducer;
/**
* 发送消息接口
* @param msg 消息内容
* @return 响应信息
*/
@GetMapping("/send")
public String sendMessage(@RequestParam String msg) {
messageProducer.sendMessage(msg);
return "消息发送成功!内容:" + msg;
}
}
四、消费者开发:接收消息
SpringBoot 整合 RabbitMQ 后,消费者开发可以通过注解驱动的方式实现,核心注解是 @RabbitListener。
1. 消费者组件
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消息消费者
*/
@Component
public class MessageConsumer {
/**
* 监听指定队列的消息:注解指定队列名
* @param message 接收到的消息内容
*/
@RabbitListener(queues = RabbitMQConfig.SIMPLE_QUEUE)
public void receiveMessage(String message) {
System.out.println("消费者接收消息:" + message);
// 此处可添加业务逻辑处理
}
}
关键说明:
@RabbitListener(queues = "队列名"):注解标注方法为消费者方法,监听指定队列。- 方法参数会自动映射消息内容(支持 String、Object 等类型,Spring 会自动序列化 / 反序列化)。
五、测试运行
-
启动 SpringBoot 应用启动项目后,SpringBoot 会自动完成以下操作:
- 连接 RabbitMQ 服务器
- 创建声明的
simple_queue队列 - 消费者自动监听该队列
-
触发消息发送访问接口:
http://localhost:8080/send?msg=Hello RabbitMQ!,此时控制台会输出:生产者发送消息:Hello RabbitMQ! 消费者接收消息:Hello RabbitMQ! -
RabbitMQ 管理界面验证进入
http://localhost:15672,在Queues标签下可看到simple_queue队列,消息的发送、接收数量会实时更新。
六、扩展说明
本文是最简案例,实际项目中还可以扩展以下内容:
- 消息序列化:默认使用 JDK 序列化,可替换为 JSON 序列化(配置
MessageConverter)。 - 交换机与绑定:使用
@Exchange和@QueueBinding注解声明交换机、队列绑定关系。 - 消息确认机制:开启生产者确认(Publisher Confirm)、消费者手动确认(Ack),保证消息可靠性。
- 死信队列:处理失败的消息,避免消息丢失。
- 批量发送 / 消费:提升消息处理性能。
总结
本文通过注解驱动的方式,实现了 SpringBoot 与 RabbitMQ 的最简整合,涵盖了队列声明、生产者发送消息、消费者接收消息的核心流程。SpringBoot 对 RabbitMQ 的自动化配置极大简化了开发工作,而注解的使用让代码更加简洁、易维护。希望这个案例能帮助你快速上手 SpringBoot + RabbitMQ 的开发!

322

被折叠的 条评论
为什么被折叠?



