RabbitMQ官网:https://www.rabbitmq.com
目录
-
概述
大多应用中,可通过消息服务中间件来提升系统异步通信、扩展解耦能力。
- 消息服务中两个重要概念:
- 消息代理(message broker)和目的地(destination) 当消息发送者发送消息以后,将由消息代理接管,消息代理保证消息传递到指定目的地。
- 消息队列主要有两种形式的目的地
- 队列(queue):点对点消息通信(point-to-point)
- 主题(topic):发布(publish)/订阅(subscribe)消息通信
- 点对点式:
- 消息发送者发送消息,消息代理将其放入一个队列中,消息接收者从队列中获取消息内容,消息读取后被移出队列 消息只有唯一的发送者和接受者,但并不是说只能有一个接收者
- 发布订阅式:
- 发送者(发布者)发送消息到主题,多个接收者(订阅者)监听(订阅)这个主题,那么就会在消息到达时同时收到消息
- JMS(Java Message Service)JAVA消息服务:
- 基于JVM消息代理的规范。ActiveMQ、HornetMQ是JMS实现
- AMQP(Advanced Message Queuing Protocol)
- 高级消息队列协议,也是一个消息代理的规范,兼容JMS RabbitMQ是AMQP的实现
-
JMS和AMQP比较
| JMS | AMQP |
定义 | Java api | 网络线级协议 |
跨语言 | 否 | 是 |
跨平台 | 否 | 是 |
Model | 提供两种消息模型: (1)、Peer-2-Peer (2)、Pub/sub | 提供了五种消息模型: (1)、direct exchange (2)、fanout exchange (3)、topic change (4)、headers exchange (5)、system exchange 本质来讲,后四种和JMS的pub/sub模型没有太大差别,仅是在路由机制上做了更详细的划分; |
支持消息类型 | 多种消息类型: TextMessage MapMessage BytesMessage StreamMessage ObjectMessage Message (只有消息头和属性) | byte[] 当实际应用时,有复杂的消息,可以将消息序列化后发送。 |
综合评价 | JMS 定义了JAVA API层面的标准;在java体系中,多个client均可以通过JMS进行交互,不需要应用修改代码,但是其对跨平台的支持较差; | AMQP定义了wire-level层的协议标准;天然具有跨平台、跨语言特性。 |
-
RabbitMQ简介
- RabbitMQ简介:
RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue Protocol)的开源实现。
- 核心概念 Message
消息,消息是不具名的,它由消息头和消息体组成。消息体是不透明的,而消息头则由一系列的可选属性组成,这些属性包括routing-key(路由键)、priority(相对于其他消息的优先权)、delivery-mode(指出该消息可能需要持久性存储)等。
- Publisher
消息的生产者,也是一个向交换器发布消息的客户端应用程序。
- Exchange
交换器,用来接收生产者发送的消息并将这些消息路由给服务器中的队列。 Exchange有4种类型:direct(默认),fanout, topic, 和headers,不同类型的Exchange转发消息的策略有所区别
- Queue
消息队列,用来保存消息直到发送给消费者。它是消息的容器,也是消息的终点。一个消息可投入一个或多个队列。消息一直在队列里面,等待消费者连接到这个队列将其取走。
- Binding
绑定,用于消息队列和交换器之间的关联。一个绑定就是基于路由键将交换器和消息队列连接起来的路由规则,所以可以将交换器理解成一个由绑定构成的路由表。 Exchange 和Queue的绑定可以是多对多的关系。
- Connection
网络连接,比如一个TCP连接。
- Channel
信道,多路复用连接中的一条独立的双向数据流通道。信道是建立在真实的TCP连接内的虚拟连接,AMQP 命令都是通过信道发出去的,不管是发布消息、订阅队列还是接收消息,这些动作都是通过信道完成。因为对于操作系统来说建立和销毁 TCP 都是非常昂贵的开销,所以引入了信道的概念,以复用一条 TCP 连接。
- Consumer
消息的消费者,表示一个从消息队列中取得消息的客户端应用程序。
- Virtual Host
虚拟主机,表示一批交换器、消息队列和相关对象。虚拟主机是共享相同的身份认证和加密环境的独立服务器域。每个 vhost 本质上就是一个 mini 版的 RabbitMQ 服务器,拥有自己的队列、交换器、绑定和权限机制。vhost 是 AMQP 概念的基础,必须在连接时指定,RabbitMQ 默认的 vhost 是 / 。
- Broker
表示消息队列服务器实体
-
RabbitMQ运行机制
-
AMQP 中的消息路由
AMQP 中消息的路由过程和 Java 开发者熟悉的 JMS 存在一些差别,AMQP 中增加了 Exchange 和 Binding 的角色。生产者把消息发布到 Exchange 上,消息最终到达队列并被消费者接收,而 Binding 决定交换器的消息应该发送到那个队列。
-
Exchange 类型
Exchange分发消息时根据类型的不同分发策略有区别,目前共四种类型:direct、fanout、topic、headers 。headers 匹配 AMQP 消息的 header 而不是路由键, headers 交换器和 direct 交换器完全一致,但性能差很多,目前几乎用不到了,所以直接看另外三种类型:
1)、direct Exchange
消息中的路由键(routing key)如果和 Binding 中的 binding key 一致, 交换器就将消息发到对应的队列中。路由键与队列名完全匹配,如果一个队列绑定到交换机要求路由键为“dog”,则只转发 routing key 标记为“dog”的消息,不会转发“dog.puppy”,也不会转发“dog.guard”等等。它是完全匹配、单播的模式。
2)、fanout Exchange
每个发到 fanout 类型交换器的消息都会分到所有绑定的队列上去。fanout 交换器不处理路由键,只是简单的将队列绑定到交换器上,每个发送到交换器的消息都会被转发到与该交换器绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。fanout 类型转发消息是最快的。
3)、topic Exchange
topic 交换器通过模式匹配分配消息的路由键属性,将路由键和某个模式进行匹配,此时队列需要绑定到一个模式上。它将路由键和绑定键的字符串切分成单词,这些单词之间用点隔开。它同样也会识别两个通配符:符号“#”和符号“*”。#匹配0个或多个单词,*匹配一个单词。
-
RabbitMQ安装测试
-
RabbitMQ安装
1)、使用docker安装rabbitMQ镜像
[root@localhost ~]# docker pull rabbitmq:3-management
2)、 启动rabbitMQ镜像
[root@localhost ~]# docker run -d -p 5672:5672 -p 15672:15672 --name myRabbitMq 985adbf1306
-d:后台运行;-p:端口映射 5672主机的端口映射到docker容器的端口,这是客户端和rabbitmq通信的端口、15672管理界面的端口;--name:当前运行的名称;985adbf1306(IMAGE ID)
3)、访问RabbitMQ管理页面
用户名、密码guest
-
RabbitMQ测试
1)、RabbitMQ消息原理图
2)、创建交换器exchange
exchange.direct、exchange.fanout、exchange.topic
3)、创建消息队列 Queues
hello、hello.news、hello.emps、helloWorld.news
4)、队列和交换器进行绑定
队列要工作必须要和交换器进行绑定
分别对交换器exchange.direct、exchange.fanout、exchange.topic分别都绑定hello、hello.news、hello.emps、helloWorld.news
Topic绑定规则不一样
5)、给消息队列发送消息
- exchange.direct交换器发送消息
- exchange.fanout交换器发送消息
- exchange.topic交换器发送消息
-
SpringBoot整合RabbitMQ
1)、创建一个SpringBoot工程
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wmy.integrate</groupId>
<artifactId>springboot-integrate-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-integrate-rabbitmq</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-web</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)、RabbitMQ测试
@SpringBootTest
class SpringbootIntegrateRabbitmqApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
/**
* exchange.direct 点对点发送消息
*/
@Test
void contextLoads() {
//第一个参数路由器exchange:exchange.direct
//第二参数路由键routingKey: hello.news
//第三个参数发送的消息体
Map<String,Object> map = new HashMap<>();
map.put("msg","xchange.direct 点对点发送消息");
map.put("data", Arrays.asList("helloworld","张三",100,true));
rabbitTemplate.convertAndSend("exchange.direct","hello.news",map);
}
/**
* 获取消息
*/
@Test
void getRabbitMq(){
Object o = rabbitTemplate.receiveAndConvert("hello.news");
System.out.println("数据类型:"+o.getClass());
System.out.println("数据:"+o);
}
/**
* 广播模式exchange.fanout
*/
@Test
void sendMsg(){
//第一个参数路由器exchange:exchange.direct
//第二参数路由键routingKey:广播路由键为空
//第三个参数发送的消息体
Map<String,Object> map = new HashMap<>();
map.put("msg","exchange.fanout广播发送消息");
map.put("data", Arrays.asList("helloworld","李四",100,true));
rabbitTemplate.convertAndSend("exchange.fanout","",map);
}
}
3)、自己指定消息的序列化规则
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
4)、消息监听
@EnableRabbit//开启基于注解的rabbitMQ模式
@SpringBootApplication
public class SpringbootIntegrateRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootIntegrateRabbitmqApplication.class, args);
}
}
@Service
public class RabbitService {
/**
* 监听消息队列hello.emps
* @param map
*/
@RabbitListener(queues = {"hello.emps"})
public void receive(Map map){
System.out.println("消息监听结果:"+map);
}
/**
* 监听也可以获取消息头信息
* @param message
*/
@RabbitListener(queues = {"hello.emps"})
public void receive01(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
5)、AmqpAdmin创建RabbitMQ系统管理组件
@SpringBootTest
class SpringbootIntegrateRabbitmqApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
/**
* 利用rabbitMQ自动配置添加的AmqpAdmin系统管理功能组件,创建交换器、队列等
*/
@Test
public void createExchange(){
//创建交换器
amqpAdmin.declareExchange(new DirectExchange("rabbitmq.direct"));
//创建队列
amqpAdmin.declareQueue(new Queue("rabbitmq.queue"));
//将队列和交换器绑定
// String destination, 目的地
// DestinationType destinationType, 目的地类型
// String exchange, 交换器
// String routingKey, 路由键
// @Nullable Map<String, Object> arguments 参数头信息
amqpAdmin.declareBinding(new Binding("rabbitmq.queue",
Binding.DestinationType.QUEUE,
"rabbitmq.direct",
"rabbitmq.hello",
null));
//相关的删除可以使用amqpAdmin.delete.....
}