Spring Boot 揭秘与实战之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();
}
}
写在最后:欢迎留言讨论,加关注,持续更新!!!
Spring Boot 揭秘与实战之RabbitMQ的更多相关文章
- Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ
文章目录 1. 什么是 RabitMQ 2. Spring Boot 整合 RabbitMQ 3. 实战演练4. 源代码 3.1. 一个简单的实战开始 3.1.1. Configuration 3.1 ...
- Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置
Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...
- Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控
文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...
- 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. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...
- Spring Boot 揭秘与实战(八) 发布与部署 - 远程调试
文章目录 1. 依赖 2. 部署 3. 调试 4. 源代码 设置远程调试,可以在正式环境上随时跟踪与调试生产故障. 依赖 在 pom.xml 中增加远程调试依赖. <plugins> &l ...
随机推荐
- 最基础的Python的socket编程入门教程
最基础的Python的socket编程入门教程 本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在 ...
- win8(x64)下,重新安装delphi 2007时出现“Invalid Serial Number”,如何解决?
1)在添加删除程序里卸载delphi 2007 2)删除C:\ProgramData\CodeGear 3)删除C:\Program Files (x86)CodeGear 4)删除d:\Progra ...
- 图解 perspective && perspective-origin && transform3d && transform-origin
perspective && perspective-origin perspective是物体相对于画布的视距距离 perspective-origin是物体在画布位置的中心视点 t ...
- axios发post请求,后端接收不到参数的问题
axios会帮我们自动转换请求数据和响应数据 以及 自动转换JSON数据,我们的请求头转换成 Content-Type变成了application/json;charset=utf-8,然后因为我们的 ...
- 11点睛Spring4.1-Property Editor
11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...
- 常见问题:MySQL/排序
MySQL的排序分为两种,通过排序操作和按索引扫描排序. 按索引顺序扫描是一种很高效的方式,但使用的条件较为严格,只有orderby语句使用索引最左前列,或where语句与orderby语句条件列组合 ...
- request.GET、request.POST、request.body(持续更新)
1.request.GET: print(request.GET) # <QueryDict: {'page' : ['5'], 'id__gt' : ['4']}> print(requ ...
- 用Python打开文件夹
用Python读取文件夹, 然后打开文件 下面读取到文件的每一个内容, 然后加上路径 import os path = r'../Downloads/text/content' for filenam ...
- 史上最浅显易懂的RxJava入门教程
RxJava是一个神奇的框架,用法很简单,但内部实现有点复杂,代码逻辑有点绕.我读源码时,确实有点似懂非懂的感觉.网上关于RxJava源码分析的文章,源码贴了一大堆,代码逻辑绕来绕去的,让人看得云里雾 ...
- go方法
go中的方法(method),跟函数(function)不是一个概念,一定要区分,它的概念与python中的类方法类似. go中是没有类的概念的,所以,go要想实现类 多种属性集合的功能的话,必须要使 ...