RabbitMQ(一)
本文大纲:
-
消息队列概述
-
RabbitMQ介绍
-
RabbitMQ 工作模式
-
一、
MQ全称为Message Queue,消息队列作用在(一个)应用程序和(一个或多个)应用程序之间进行通信过程中保存消息的容器。【是在消息的传输过程中保存消息的容器 】
1.2 常见产品
常见的消息中间件(MOM)产品有(部分):
1.3
所有服务间调用是否都可以使用MQ?不是。
-
-
JMS即Java消息服务(JavaMessage Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。
-
JMS和AMQP区别
-
JMS是定义了统一的接口,来对消息操作进行统一;AMQP是通过规定协议来统一数据交互的格式
-
JMS限定了必须使用Java语言;AMQP只是协议,不规定实现方式,因此是跨语言的
-
-
二、
RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队列协议)协议实现的消息队列,它是一种应用程序之间的通信方法,消息队列在分布式系统开发中应用非常广泛。
RabbitMQ官方地址:http://www.rabbitmq.com/
RabbitMQ提供了6种模式:简单模式,work模式,Publish/Subscribe发布与订阅模式,Routing路由模式,Topics主题模式,RPC远程调用模式(远程调用,不太算MQ);
官网对应模式介绍:
在Spring项目中,可以使用Spring-Rabbit去操作RabbitMQ https://github.com/spring-projects/spring-amqp
尤其是在spring boot项目中只需要引入对应的amqp启动器依赖即可,方便的使用RabbitTemplate发送消息,使用注解接收消息。
一般在开发过程中:
生产者工程:
- application.yml文件配置RabbitMQ相关信息
- 在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定
- 注入RabbitTemplate对象,通过RabbitTemplate对象发送消息到交换机
消费者工程:
- application.yml文件配置RabbitMQ相关信息
- 创建消息处理类,用于接收队列中的消息并进行处理
实现:
4.1 搭建生产者工程
- 创建生产者工程springboot-rabbitmq-producer
- 添加依赖

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <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> </dependency> </dependencies>
3.创建启动类ProducerApplication

@SpringBootApplication public class RabbitMqProviderApplication { public static void main(String[] args) { SpringApplication.run(RabbitMqProviderApplication.class, args); } // 创建队列 @Bean public Queue topicQueue(){ return new Queue("topic_queue_spring_boot"); } // 创建交换机 @Bean public Exchange topicExchange(){ return new TopicExchange("topic_exchange_spring_boot"); } // 将队列绑定到交换机 @Bean public Binding topicQueueBind(Queue topicQueue, Exchange topicExchange){ return BindingBuilder.bind(topicQueue).to(topicExchange).with("item.#").noargs(); } }
4.

spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /test
username: admin
password: 123456
4.2 搭建消费者工程
- 创建消费者工程springboot-rabbitmq-consumer
- 添加依赖

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <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> </dependency> </dependencies>
3.创建启动类ConsumerApplication
@SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class); } }
4.
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /test
username: admin
password: 123456
5.编写消息监听器类MessageListener
@Component public class MessageListener { /** * 监听某个队列的消息 * @param message 接收到的消息 */ @RabbitListener(queues = "topic_queue_spring_boot") public void myListener1(String message){ System.out.println("消费者接收到的消息为:" + message); } }
4.3 测试
在生产者工程rabbitmq-producer中创建测试类RabbitMQTest,发送消息:

@RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqProviderApplicationTests { @Autowired private RabbitTemplate rabbitTemplate; @Test public void contextLoads() { rabbitTemplate.convertAndSend("topic_exchange_spring_boot","item.update", "更新"); rabbitTemplate.convertAndSend("topic_exchange_spring_boot","item.brand.insert", "插入"); } }
1、先运行上述测试程序(交换机和队列才能先被声明和绑定),然后启动消费者;在消费者工程springboot-rabbitmq-consumer中控制台查看是否接收到对应消息:
2、另外;也可以在RabbitMQ的管理控制台中查看到交换机与队列的绑定:
至此,整合完毕。