Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ
文章目录
本文,讲解 Spring Boot 如何集成 RabbitMQ,实现消息队列。
什么是 RabitMQ
RabbitMQ 是一个在 AMQP 基础上完整的,可复用的企业消息系统。
关于 RabbitMQ 的使用,可以阅读之前的 RabbitMQ 实战教程。
- 【译】RabbitMQ 实战教程(一) Hello World!
- 【译】RabbitMQ 实战教程(二) 工作队列
- 【译】RabbitMQ 实战教程(三) 发布/订阅
- 【译】RabbitMQ 实战教程(四) 路由
- 【译】RabbitMQ 实战教程(五) 主题
Spring Boot 整合 RabbitMQ
Spring Boot 整合 RabbitMQ 是非常容易,只需要两个步骤。
首先,在 pom.xml 中增加 RabbitMQ 依赖。
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- </dependency>
第二步,在 src/main/resources/application.properties 中配置信息。
- #rabbitmq
- spring.rabbitmq.host=localhost
- spring.rabbitmq.port=5672
- spring.rabbitmq.username=guest
- spring.rabbitmq.password=guest
实战演练
一个简单的实战开始
我们来实现一个简单的发送、接收消息。
Configuration
在 Spring Boot 中使用 @Bean 注册一个队列。
- @Configuration
- public class RabbitMQConfig {
- public static final String QUEUE_NAME = "spring-boot-simple";
- @Bean
- public Queue queue() {
- return new Queue(QUEUE_NAME);
- }
- }
消息生产者
创建消息生产者 Sender。通过注入 AmqpTemplate 接口的实例来实现消息的发送。
- @Service
- public class Sender {
- @Autowired
- private AmqpTemplate rabbitTemplate;
- public void send() {
- System.out.println("梁桂钊 发送消息...");
- rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "你好, 梁桂钊!");
- }
- }
消息消费者
创建消息消费者 Receiver。通过 @RabbitListener 注解定义对队列的监听。
- @Service
- public class Receiver {
- @Autowired
- private AmqpTemplate rabbitTemplate;
- @RabbitListener(queues = "spring-boot-simple")
- public void receiveMessage(String message) {
- System.out.println("Received <" + message + ">");
- }
- }
运行
- @SpringBootApplication
- @EnableAutoConfiguration
- @ComponentScan(basePackages = { "com.lianggzone.springboot" })
- public class RunMain {
- public static void main(String[] args) {
- SpringApplication.run(RunMain.class, args);
- }
- }
单元测试
创建单元测试用例
- public class RabbitMQTest {
- @Autowired
- private Sender sender;
- @Test
- public void send() throws Exception {
- sender.send();
- }
- }
路由的实战演练
经过上面的实战案例,我们对 Spring Boot 整合 RabbitMQ 有了一定的了解。现在,我们再来看下 RabbitMQ 路由场景。
Configuration
在 RabbitMQConfig 中,我们注册 队列,转发器,监听等。
- @Configuration
- public class RabbitMQConfig2 {
- public static final String QUEUE_NAME = "spring-boot";
- public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange";
- @Bean
- public Queue queue() {
- // 是否持久化
- boolean durable = true;
- // 仅创建者可以使用的私有队列,断开后自动删除
- boolean exclusive = false;
- // 当所有消费客户端连接断开后,是否自动删除队列
- boolean autoDelete = false;
- return new Queue(QUEUE_NAME, durable, exclusive, autoDelete);
- }
- @Bean
- public TopicExchange exchange() {
- // 是否持久化
- boolean durable = true;
- // 当所有消费客户端连接断开后,是否自动删除队列
- boolean autoDelete = false;
- return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);
- }
- @Bean
- public Binding binding(Queue queue, TopicExchange exchange) {
- return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
- }
- @Bean
- SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
- MessageListenerAdapter listenerAdapter) {
- SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
- container.setConnectionFactory(connectionFactory);
- container.setQueueNames(QUEUE_NAME);
- container.setMessageListener(listenerAdapter);
- return container;
- }
- @Bean
- MessageListenerAdapter listenerAdapter(Receiver receiver) {
- return new MessageListenerAdapter(receiver, "receiveMessage");
- }
- }
消息生产者
创建消息生产者 Sender。通过注入 AmqpTemplate 接口的实例来实现消息的发送。
- @Service
- public class Sender {
- @Autowired
- private AmqpTemplate rabbitTemplate;
- public void send() {
- System.out.println("梁桂钊 发送消息...");
- rabbitTemplate.convertAndSend(RabbitMQConfig2.QUEUE_NAME, "你好, 梁桂钊!");
- }
- }
消息消费者
创建消息消费者 Receiver。通过 @RabbitListener 注解定义对队列的监听。
- @Service
- public class Receiver {
- public void receiveMessage(String message) {
- System.out.println("Received <" + message + ">");
- }
- }
运行
- @SpringBootApplication
- @EnableAutoConfiguration
- @ComponentScan(basePackages = { "com.lianggzone.springboot" })
- public class RunMain {
- public static void main(String[] args) {
- SpringApplication.run(RunMain.class, args);
- }
- }
单元测试
创建单元测试用例
- public class RabbitMQTest {
- @Autowired
- private Sender sender;
- @Test
- public void send() throws Exception {
- sender.send();
- }
- }
源代码
相关示例完整代码: springboot-action
(完)
如果觉得我的文章对你有帮助,请随意打赏。

- 版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
- 转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
- 文章标题:Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ
- 文章链接:http://blog.720ui.com/2017/springboot_06_mq_rabbitmq/
Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ的更多相关文章
- Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置
Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...
- Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...
- Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...
- Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机
文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点
文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控
文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控
文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...
- Spring Boot 揭秘与实战(八) 发布与部署 - 远程调试
文章目录 1. 依赖 2. 部署 3. 调试 4. 源代码 设置远程调试,可以在正式环境上随时跟踪与调试生产故障. 依赖 在 pom.xml 中增加远程调试依赖. <plugins> &l ...
- Spring Boot 揭秘与实战(八) 发布与部署 - 开发热部署
文章目录 1. spring-boot-devtools 实现热部署 2. Spring Loaded 实现热部署 3. 模板文件热部署 4. 源代码 Spring Boot 支持页面与类文件的热部署 ...
随机推荐
- (待解决,效率低下)47. Permutations II C++回溯法
思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的v ...
- img 标签
设计网页时经常使用的图片有三种,它们的相同点是都经过了压缩,压缩比越高,图像品质越差. GIF(Graphics Interchange Format):最多支持256色,支持透明,支持多帧动画显示效 ...
- vs 编译库文件 Qt编译库文件
QT 库能不能用 需要关注是minGW 还是MSVC编译的 Qt MinGW与MSVC对比 转:https://blog.csdn.net/u013185164/article/details/48 ...
- C#中使用SqlBulkCopy的批量插入和OracleBulkCopy的批量插入
1.首先我们做一下准备工作,在sql server和oracle分别建立一个Student表 oracle中 --创建Student表 -- create table Student( stuId n ...
- 转Generative Model 与 Discriminative Model
没有完全看懂,以后再看,特别是hmm,CRF那里,以及生成模型产生的数据是序列还是一个值,hmm应该是序列,和图像的关系是什么. [摘要] - 生成模型(Generative Model) :无 ...
- java输入一个字符串,输出该字符串的所有的排序
public class Sort { public static void arrangeSequence(char[] strArr,int i){ char temp; ArrayList< ...
- information_schema
views 视图表,查看当前数据库有哪些视图 select table_catalog,table_schema,table_name,is_updatable,definer,security_ty ...
- Java 将图片转成base64,传到前台展示
后台代码: public String getBase64(SysFile sysFile){ String imgStr = ""; try { File file = new ...
- 自旋锁(Spin Lock)
转载请您注明出处: http://www.cnblogs.com/lsh123/p/7400625.html 0x01 自旋锁简介 自旋锁也是一种同步机制,它能保证某个资源只能被一个线程所拥有, ...
- 网口扫盲三:以太网芯片MAC和PHY的关系(转)
问:如何实现单片以太网微控制器? 答:诀窍是将微控制器.以太网媒体接入控制器(MAC)和物理接口收发器(PHY)整合进同一芯片,这样能去掉许多外接元器件.这种方案可使MAC和PHY实现很好的匹配, ...