RabbitMQ
一、消息中间件概述
(一) 为什么学习消息队列
电子商务应用中,经常需要对庞大的海量数据进行监控,随着网络技术和软件开发技术的不断提高,在实战开发中MQ的使用与日俱增,特别是RabbitMQ在分布式系统中存储转发消息,可以保证数据不丢失,也能保证高可用性,即集群部署的时候部分机器宕机可以继续运行。在大型电子商务类网站,如京东、淘宝、去哪儿等网站有着深入的应用 。
队列的主要作用是消除高并发访问高峰,加快网站的响应速度
在不使用消息队列的情况下,用户的请求数据直接写入数据库,在高并发的情况下,会对数据库造成巨大的压力,同时也使得系统响应延迟加剧。
(二) 什么是消息中间件
MQ全称为 Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。
介绍:消息队列就是基础数据结构中的“先进先出”的一种数据机构。想一下,生活中买东西,需要排队,先排的人先买消费,就是典型的“先进先出”
**消息传递:**指的是程序之间通过消息发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。
**排队:**指的是应用程序通过队列来通信。
业务场景说明:
消息队列在大型电子商务类网站,如京东、淘宝、去哪儿等网站有着深入的应用,为什么会产生消息队列?有几个原因:
不同进程(process)之间传递消息时,两个进程之间耦合程度过高,改动一个进程,引发必须修改另一个进程,为了隔离这两个进程,在两进程间抽离出一层(一个模块),所有两进程之间传递的消息,都必须通过消息队列来传递,单独修改某一个进程,不会影响另一个;
不同进程(process)之间传递消息时,为了实现标准化,将消息的格式规范化了,并且,某一个进程接受的消息太多,一下子无法处理完,并且也有先后顺序,必须对收到的消息进行排队,因此诞生了事实上的消息队列;
在项目中,可将一些无需即时返回且耗时的操作提取出来,进行异步处理,而这种异步处理的方式大大的节省了服务器的请求响应时间,从而提高了系统的吞吐量。
(三) 消息队列应用场景
首先我们先说一下消息中间件的主要的作用:异步处理、解耦服务、流量削峰;这三点是我们使用消息中间件最主要的目的
1、应用解耦
传统模式:
传统模式的缺点:系统间耦合性太强,如上图所示,系统A在代码中直接调用系统B和系统C的代码,如果将来D系统接入,系统A还需要修改代码,过于麻烦!
中间件模式:
中间件模式的的优点:将消息写入消息队列,需要消息的系统自己从消息队列中订阅,从而系统A不需要做任何修改
2、异步处理
场景说明:用户注册后,需要发注册邮件和注册短信,传统的做法有两种:串行的方式、并行的方式
(1) 串行方式
将注册信息写入数据库后,发送注册邮件,再发送注册短信,以上三个任务全部完成后才返回给客户端。 这有一个问题是,邮件,短信并不是必须的,它只是一个通知,而这种做法让客户端等待没有必要等待的东西。
(2) 并行方式
将注册信息写入数据库后,发送邮件的同时,发送短信,以上三个任务完成后,返回给客户端,并行的方式能提高处理的时间。
假设三个业务节点分别使用50ms,串行方式使用时间150ms,并行使用时间100ms。虽然并行已经提高了处理时间,但是,前面说过,邮件和短信对我正常的使用网站没有任何影响,客户端没有必要等着其发送完成才显示注册成功,应该是写入数据库后就返回
(3) 消息队列
引入消息队列后,把发送邮件,短信不是必须的业务逻辑异步处理
由此可以看出,引入消息队列后,用户的响应时间就等于写入数据库的时间+写入消息队列的时间(可以忽略不计),引入消息队列后处理后,响应时间是串行的3分之1,是并行的2分之1。
**传统模式的缺点:**一些非必要的业务逻辑以同步的方式运行,太耗费时间
中间件模式的的优点:将消息写入消息队列,非必要的业务逻辑以异步的方式运行,加快响应速度
3、流量削峰
流量削峰一般在秒杀活动中应用广泛
场景: 秒杀活动,一般会因为流量过大,导致应用挂掉,为了解决这个问题,一般在应用前端加入消息队列。
传统模式:
如订单系统,在下单的时候就会往数据库写数据。但是数据库只能支撑每秒1000左右的并发写入,并发量再高就容易宕机。低峰期的时候并发也就100多个,但是在高峰期时候,并发量会突然激增到5000以上,这个时候数据库肯定卡死了。
缺点:并发量大的时候,所有的请求直接怼到数据库,造成数据库连接异常
**中间件模式:**消息被MQ保存起来了,然后系统就可以按照自己的消费能力来消费,比如每秒1000个数据,这样慢慢写入数据库,这样就不会卡死数据库了。
**中间件模式的的优点:**系统A慢慢的按照数据库能处理的并发量,从消息队列中慢慢拉取消息。在生产中,这个短暂的高峰期积压是允许的。
流量削峰也叫做削峰填谷,使用了MQ之后,限制消费消息的速度为1000,但是这样一来,高峰期产生的数据势必会被积压在MQ中,高峰就被“削”掉了。但是因为消息积压,在高峰期过后的一段时间内,消费消息的速度还是会维持在1000QPS,直到消费完积压的消息,这就叫做“填谷”
4、什么是 QPS,PV
QPS:即每秒查询率,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准
每秒查询率,因特网上,经常用每秒查询率来衡量域名系统服务器的机器的性能,即为QPS,或者理解:每秒的响应请求数,也即是最大吞吐能力
计算关系:
- QPS = 并发量 / 平均响应时间
- 并发量 = QPS * 平均响应时间
原理:每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间
公式:( 总PV数 * 80% ) / ( 每天秒数 * 20% ) = 峰值时间每秒请求数(QPS)
机器:峰值时间每秒QPS / 单台机器的QPS = 需要的机器
5、什么是PV , UV , PR
PV:(page view) 即页面浏览量,或点击量;通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标
对 PV 的解释是,一个访问者在24小时(0点到24点)内到底看了你网站几个页面。这里需要强调:同一个人浏览你网站同一个页面,不重复计算 PV 量,点100次也算1次。说白了,PV 就是一个访问者打开了你的几个页面
PV之于网站,就像收视率之于电视,从某种程度上已成为投资者衡量商业网站表现的最重要尺度
pv的计算:当一个访问者访问的时候,记录他所访问的页面和对应的IP,然后确定这个IP今天访问了这个页面没有。如果你的网站到了23点,单纯IP有60万条的话,每个访问者平均访问了3个页面,那么pv表的记录就要有180万条
UV:(unique visitor) 指访问某个站点或点击某条新闻的不同IP地址的人数
在同一天内,UV 只记录第一次进入网站的具有独立IP的访问者,在同一天内再次访问该网站则不计数。独立IP访问者提供了一定时间内不同观众数量的统计指标,而没有反应出网站的全面活动。
PR:PR值,即 PageRank,网页的级别技术,用来标识网页的等级/重要性。级别从1到10级,10级为满分。PR值越高说明该网页越受欢迎(越重要)
例如:一个PR值为1的网站表明这个网站不太具有流行度,而PR值为7到10则表明这个网站非常受欢迎(或者说极其重要)
(四) AMQP 和 JMS
MQ是消息通信的模型;实现MQ的大致有两种主流方式:AMQP、JMS
1、AMQP
AMQP,即 Advanced,Message Queuing Protocol(高级消息队列协议),是一个网络协议,是应用层协议的一个开放标准,为面向消息的中间件设计。基于此协议的客户端与消息中间件可传递消息,并不受客户端/中间件不同产品,不同的开发语言等条件的限制。2006年,AMQP 规范发布,类比HTTP
2、JMS
JMS即Java消息服务(JavaMessage Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信
3、AMQP 与 JMS 区别
- JMS是定义了统一的接口,来对消息操作进行统一;AMQP是通过规定协议来统一数据交互的格式
- JMS限定了必须使用Java语言;AMQP只是协议,不规定实现方式,因此是跨语言的。
- JMS规定了两种消息模式;而AMQP的消息模式更加丰富
(五) 消息队列产品
市场上常见的消息队列有如下:
- ActiveMQ:基于JMS
- ZeroMQ:基于C语言开发
- Rabbitmq:基于AMQP协议,erlang语言开发,稳定性好
- RocketMQ:基于JMS,阿里巴巴产品
- Kafka:类似MQ的产品;分布式消息系统,高吞吐量
(六) RabbitMQ
RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队列协议)协议实现的消息队列,它是一种应用程序之间的通信方法,消息队列在分布式系统开发中应用非常广泛。
RabbitMQ官方地址:http://www.rabbitmq.com/
RabbitMQ提供了6种模式:简单模式,work模式 ,Publish/Subscribe发布与订阅模式,Routing路由模式,Topics主题模式,RPC远程调用模式(远程调用,不太算MQ;暂不作介绍);
官网对应模式介绍:https://www.rabbitmq.com/getstarted.html
1、RabbitMQ 的概念
RabbitMQ 是一个消息中间件:它接受并转发消息。你可以把它当做一个快递站点,当你要发送一个包裹时,你把你的包裹放到快递站,快递员最终会把你的快递送到收件人那里,按照这种逻辑 RabbitMQ 是一个快递站,一个快递员帮你传递快件。RabbitMQ 与快递站的主要区别在于,它不处理快件而是接收,存储和转发消息数据。
2、四大核心概念
![image-20220309214309033](https://i-blog.csdnimg.cn/blog_migrate/99d259c84453bd75a85516da71f52e36.png)
生产者:产生数据发送消息的程序是生产者
交换机:交换机是 RabbitMQ 非常重要的一个部件,一方面它接收来自生产者的消息,另一方面它将消息推送到队列中。交换机必须确切知道如何处理它接收到的消息,是将这些消息推送到特定队列还是推送到多个队列,亦或者是把消息丢弃,这个得有交换机类型决定
队列:队列是 RabbitMQ 内部使用的一种数据结构,尽管消息流经 RabbitMQ 和应用程序,但它们只能存储在队列中。队列仅受主机的内存和磁盘限制的约束,本质上是一个大的消息缓冲区。许多生产者可以将消息发送到一个队列,许多消费者可以尝试从一个队列接收数据。这就是我们使用队列的方式
消费者:消费与接收具有相似的含义。消费者大多时候是一个等待接收消息的程序。请注意生产者,消费者和消息中间件很多时候并不在同一机器上。同一个应用程序既可以是生产者又是可以是消费者
4、各个名词介绍
Broker:接收和分发消息的应用,RabbitMQ Server 就是 Message Broker
Virtual host:出于多租户和安全因素设计的,把 AMQP 的基本组件划分到一个虚拟的分组中,类似于网络中的 namespace 概念。当多个不同的用户使用同一个 RabbitMQ server 提供的服务时,可以划分出多个 vhost,每个用户在自己的 vhost 创建 exchange/queue 等
Connection:publisher/consumer 和 broker 之间的 TCP 连接
Channel:如果每一次访问 RabbitMQ 都建立一个 Connection,在消息量大的时候建立 TCPConnection 的开销将是巨大的,效率也较低。Channel 是在 connection 内部建立的逻辑连接,如果应用程序支持多线程,通常每个 thread 创建单独的 channel 进行通讯,AMQP method 包含了 channel id 帮助客户端和 message broker 识别 channel,所以 channel 之间是完全隔离的。Channel 作为轻量级的 Connection 极大减少了操作系统建立TCP connection 的开销
Exchange:message 到达 broker 的第一站,根据分发规则,匹配查询表中的 routing key,分发消息到 queue 中去。常用的类型有:direct (point-to-point), topic (publish-subscribe) and fanout(multicast)
Queue:消息最终被送到这里等待 consumer 取走
Binding:exchange 和 queue 之间的虚拟连接,binding 中可以包含 routing key,Binding 信息被保存到 exchange 中的查询表中,用于 message 的分发依据
二、安装及配置RabbitMQ
(一) 下载和安装
1、下载
下载Erlang的rpm包
RabbitMQ是Erlang语言编写,所以Erang环境必须要有,注:Erlang环境一定要与RabbitMQ版本匹配:https://www.rabbitmq.com/which-erlang.html
![image-20220309220818824](https://i-blog.csdnimg.cn/blog_migrate/78b8a7078cc61e479981689e7413d6d1.png)
Erlang下载地址:https://www.rabbitmq.com/releases/erlang/(根据自身需求及匹配关系,下载对应rpm包)
https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/21/el/7/x86_64/erlang-21.3.8.9-1.el7.x86_64.rpm
下载socat的rpm包
rabbitmq安装依赖于socat,所以需要下载socat
socat下载地址:http://repo.iotti.biz/CentOS/7/x86_64/socat-1.7.3.2-5.el7.lux.x86_64.rpm
下载RabbitMQ的rpm包
RabbitMQ下载地址:https://www.rabbitmq.com/download.html(根据自身需求及匹配关系,下载对应rpm包)rabbitmq-server-3.8.1-1.el7.noarch.rpm
2、安装
在Xftp中把安装包拖过来
![image-20220309223710742](https://i-blog.csdnimg.cn/blog_migrate/e88b557a1d03b6eedcb769e69aa42b2b.png)
安装 Erlang、Socat、RabbitMQ
- rpm -ivh erlang-21.3.8.9-1.el7.x86_64.rpm
- rpm -ivh socat-1.7.3.2-1.el6.lux.x86_64.rpm
- rpm -ivh rabbitmq-server-3.8.1-1.el7.noarch.rpm
tips:在安装rabbitmq之前需要先安装socat,否则会报错,可以采用yum安装方式:yum install socat,我们这里采用rpm安装方式
/usr/lib/rabbitmq/bin/
启用管理插件
- rabbitmq-plugins enable rabbitmq_management
启动 RabbitMQ
- systemctl start rabbitmq-server.service
- systemctl status rabbitmq-server.service
- systemctl restart rabbitmq-server.service
- systemctl stop rabbitmq-server.service
查看进程
- ps -ef | grep rabbitmq
测试
- 关闭防火墙:systemctl stop firewalld.service
- 在web浏览器中输入地址:http://虚拟机ip:15672/ 例如:http://192.168.200.188:15672/
- 输入默认账号密码: guest : guest,guest用户默认不允许远程连接
![image-20220309225003616](https://i-blog.csdnimg.cn/blog_migrate/09cf40e800527852537fae713e7ff3ff.png)
增加自定义账号
- 添加管理员账号密码:rabbitmqctl add_user admin admin
- 分配账号角色:rabbitmqctl set_user_tags admin administrator
- 修改密码:rabbitmqctl change_password admin 123456
- 查看用户列表:rabbitmqctl list_users
使用新账号登录,成功界面
管理界面标签页介绍:
- Overview:概览
- Connections:无论生产者还是消费者,都需要与RabbitMQ建立连接后才可以完成消息的生产和消费,在这里可以查看连接情况
- Channels:通道,建立连接后,会形成通道,消息的投递获取依赖通道
- Exchanges:交换机,用来实现消息的路由
- Queues:队列,即消息队列,消息存放在队列中,等待消费,消费后被移除队列
端口:
- 5672:rabbitMq 的编程语言客户端连接端口
- 15672:rabbitMq 管理界面端口
- 25672:rabbitMq 集群的端口
3、卸载
- rpm -qa | grep rabbitmq
- rpm -e rabbitmq-server
(二) 管理界面
1、添加用户
如果不使用guest,我们也可以自己创建一个用户:
![image-20220309225957551](https://i-blog.csdnimg.cn/blog_migrate/1efc4027972c007e0a49ca798eea955d.png)
超级管理员(administrator):可登录管理控制台,可查看所有的信息,并且可以对用户,策略(policy)进行操作
监控者(monitoring):可登录管理控制台,同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)
策略制定者(policymaker):可登录管理控制台, 同时可以对policy进行管理。但无法查看节点的相关信息
普通管理者(management):仅可登录管理控制台,无法看到节点信息,也无法对策略进行管理
其他:无法登录管理控制台,通常就是普通的生产者和消费者
2、创建 Virtual Hosts
虚拟主机:类似于mysql中的database,他们都是以“/”开头
![image-20220309230307097](https://i-blog.csdnimg.cn/blog_migrate/a247efc176693cbe0885c68e1bef06c2.png)
3、设置权限
![image-20220309230717672](https://i-blog.csdnimg.cn/blog_migrate/a06751b6c7ba27b2aab3e0239e597625.png)
三、RabbitMQ入门
- 官网: https://www.rabbitmq.com/
- 需求:使用简单模式完成消息传递
- 步骤:
- 创建工程(生成者、消费者)
- 分别添加依赖
- 编写生产者发送消息
- 编写消费者接收消息
(一) 搭建示例工程
父工程:rabbitmq
![image-20220309231138722](https://i-blog.csdnimg.cn/blog_migrate/a2ffcc4be463fd43318ad6e131442d28.png)
子模块:rabbitmq-producer
![image-20220309231410461](https://i-blog.csdnimg.cn/blog_migrate/5bcc5ce69d604c2e4b3f0066766b288d.png)
子模块:rabbitmq-consumer
![image-20220309231519953](https://i-blog.csdnimg.cn/blog_migrate/0c1fb60a6398f562bc8466053fb828a1.png)
往两个rabbitmq的pom.xml文件中添加如下依赖:
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
(二) 编写生产者
com.atguigu.rabbitmq.simple.SimpleProducer
public class SimpleProducer {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
// 主机地址
connectionFactory.setHost("192.168.200.188");
// 连接端口;默认为 5672
connectionFactory.setPort(5672);
// 虚拟主机名称;默认为 /
connectionFactory.setVirtualHost("/");
// 连接用户名;默认为guest
connectionFactory.setUsername("admin");
// 连接密码;默认为guest
connectionFactory.setPassword("123456");
// 创建连接
Connection connection = connectionFactory.newConnection();
// 创建信道
Channel channel = connection.createChannel();
/**
* 声明(创建)队列
*
* @param queue 参数1:队列名称
* @param durable 参数2:持久化队列,true:当(服务器)重启之后依然还在
* @param exclusive 参数3:是否独占本次连接
* ① 是否独占,只能有一个消费者监听这个队列
* ② 当connection关闭时,是否删除队列
* @param autoDelete 参数4:是否在不使用的时候自动删除队列,当没有consumer时,自动删除
* @param arguments 参数5:队列属性设置。例如:队列长度、队列超时时间等
*/
channel.queueDeclare("simple_queue", true, false, false, null);
// 要发送的信息
String message = "你好;小兔子!";
/**
* @param exchange:指定消息发送的交换机名称,如果没有指定则使用默认Default Exchage
* @param routingKey:队列与交换机绑定路由key。简单消息模型,底层存在默认交换机。RoutingKey指定队列名称即可
* @param props:设置消息属性。例如:消息超时时间等
* @param body:消息数据
*/
channel.basicPublish("", "simple_queue", null, message.getBytes());
System.out.println("已发送消息:" + message);
// 关闭资源
channel.close();
connection.close();
}
}
Channel 的 queueDeclare 方法:
Channel 的 basicPublish 方法:
props:消息的其他属性 - 路由标头等
运行程序:http://192.168.200.188:15672
在执行上述的消息发送之后;可以登录rabbitMQ的管理控制台,可以发现队列和其消息:
已发送消息:你好;小兔子!
(三) 编写消费者
编写消息的消费者 com.atguigu.rabbitmq.simple.SimpleConsumer
public class SimpleConsumer {
public static void main(String[] args) throws Exception {
// 1、创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
// 2、设置参数
// 主机地址
connectionFactory.setHost("192.168.200.188");
// 连接端口;默认为 5672
connectionFactory.setPort(5672);
// 虚拟主机名称;默认为 /
connectionFactory.setVirtualHost("/");
// 连接用户名;默认为guest
connectionFactory.setUsername("admin");
// 连接密码;默认为guest
connectionFactory.setPassword("123456");
// 3、创建连接
Connection connection = connectionFactory.newConnection();
// 4、创建信道
Channel channel = connection.createChannel();
// 5、创建队列Queue
// 消费者侧,其实不需要再声明队列了,因为生产者已经声明过了。如果消费者再次声明,需要保证参数必须与生产者侧一致。如果没有一个名字叫simple_queue的队列,则会创建该队列,如果有则不会创建
/**
* 声明(创建)队列
*
* @param queue 参数1:队列名称
* @param durable 参数2:持久化队列,true:当(服务器)重启之后依然还在
* @param exclusive 参数3:是否独占本次连接
* ① 是否独占,只能有一个消费者监听这个队列
* ② 当connection关闭时,是否删除队列
* @param autoDelete 参数4:是否在不使用的时候自动删除队列,当没有consumer时,自动删除
* @param arguments 参数5:队列属性设置。例如:队列长度、队列超时时间等
*/
channel.queueDeclare("simple_queue", true, false, false, null);
// 接收消息
DefaultConsumer consumer = new DefaultConsumer(channel) {
/**
* 回调方法,当收到消息后,会自动执行该方法
* @param consumerTag 标识
* @param envelope 获取一些信息,交换机,路由key···
* @param properties 配置信息
* @param body 数据
* @throws IOException
*/
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("consumerTag:" + consumerTag);
System.out.println("Exchange:" + envelope.getExchange());
System.out.println("RoutingKey:" + envelope.getRoutingKey());
System.out.println("properties:" + properties);
System.out.println("body:" + new String(body));
}
};
/**
* 消费者类似一个监听程序,主要是用来监听消息
*
* basicConsume(String queue, boolean autoAck, Consumer callback)
*
* queue:队列名称,从哪个队列获取消息
* autoAck:true表示自动确认。False表示手动确认,类似咱们发短信,发送成功会收到一个确认消息
* callback:消费者,是Consumer对象接口。回调对象
*/
channel.basicConsume("simple_queue", true, consumer);
}
}
控制台打印
consumerTag:amq.ctag-ohk_UdCSfFCmSjtnaZGQmQ
Exchange:
RoutingKey:simple_queue
properties:#contentHeader<basic>(content-type=null, content-encoding=null, headers=null, delivery-mode=null, priority=null, correlation-id=null, reply-to=null, expiration=null, message-id=null, timestamp=null, type=null, user-id=null, app-id=null, cluster-id=null)
body:你好;小兔子!
(四) 小结
上述的入门案例中中其实使用的是如下的简单模式:
在上图的模型中,有以下概念:
- P:生产者,也就是要发送消息的程序
- C:消费者:消息的接受者,会一直等待消息到来。
- queue:消息队列,图中红色部分,类似一个邮箱,可以缓存消息;生产者向其中投递消息,消费者从其中取出消息
四、AMQP
(一) 相关概念介绍
AMQP 一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计
RabbitMQ是AMQP协议的Erlang的实现
概念 | 说明 |
---|---|
连接Connection | 一个网络连接,比如TCP/IP套接字连接 |
信道Channel | 多路复用连接中的一条独立的双向数据流通道。为会话提供物理传输介质 |
客户端Client | AMQP连接或者会话的发起者。AMQP是非对称的,客户端生产和消费消息,服务器存储和路由这些消息 |
服务节点Broker | 消息中间件的服务节点;一般情况下可以将一个RabbitMQ Broker看作一台RabbitMQ 服务器 |
端点 | AMQP对话的任意一方。一个AMQP连接包括两个端点(一个是客户端,一个是服务器) |
消费者Consumer | 一个从消息队列里请求消息的客户端程序 |
生产者Producer | 一个向交换机发布消息的客户端应用程序 |
(二) RabbitMQ运转流程
在入门案例中:
- 生产者发送消息
- 生产者创建连接(Connection),开启一个信道(Channel),连接到RabbitMQ Broker;
- 声明队列并设置属性;如是否排它,是否持久化,是否自动删除;
- 将路由键(空字符串)与队列绑定起来;
- 发送消息至RabbitMQ Broker;
- 关闭信道;
- 关闭连接;
- 消费者接收消息
- 消费者创建连接(Connection),开启一个信道(Channel),连接到RabbitMQ Broker
- 向Broker 请求消费相应队列中的消息,设置相应的回调函数;
- 等待Broker投递响应队列中的消息,消费者接收消息;
- 确认(ack,自动确认)接收到的消息;
- RabbitMQ从队列中删除相应已经被确认的消息;
- 关闭信道;
- 关闭连接;
五、RabbitMQ工作模式
(一) Work queues工作队列模式
1、模式说明
Work Queues与入门程序的简单模式相比,多了一个或一些消费端,多个消费端共同消费同一个队列中的消息
应用场景:对于任务过重或任务较多情况使用工作队列可以提高任务处理的速度
2、代码
Work Queues与入门程序的简单模式的代码是几乎一样的;可以完全复制,并复制多一个消费者进行多个消费者同时消费消息的测试。
(1) 抽取工具类
common 的 pom.xml
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.6.0</version>
</dependency>
</dependencies>
ConnectionUtil:com.atguigu.rabbitmq.util.ConnectionUtil
public class ConnectionUtil {
public static Connection getConnection() throws Exception {
// 定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 设置服务地址
factory.setHost("192.168.200.188");
// 端口
factory.setPort(5672);
// 设置账号信息,用户名、密码、vhost
factory.setVirtualHost("/");
factory.setUsername("admin");
factory.setPassword("123456");
// 通过工程获取连接
Connection connection = factory.newConnection();
return connection;
}
public static void main(String[] args) throws Exception {
Connection con = ConnectionUtil.getConnection();
System.out.println(con);
// amqp://admin@192.168.137.118:5672/
con.close();
}
}
在 rabbitmq-consumer 和 rabbitmq-producer 的 pom.xml 中加入 common 的坐标
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
(2) 生产者
com.atguigu.rabbitmq.work.WorkQueueProducer
public class WorkQueueProducer {
static final String QUEUE_NAME = "work_queue";
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
for (int i = 0; i < 10; i++) {
String body = i + "hello rabbitmq~~~";
channel.basicPublish("", QUEUE_NAME, null, body.getBytes());
}
channel.close();
connection.close();
}
}
(3) 消费者1
com.atguigu.rabbitmq.work.WorkQueueConsumer1
public class WorkQueueConsumer1 {
static final String QUEUE_NAME = "work_queue";
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 设置消息抓取数量,手动确认之后再抓取下一个
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body1:" + new String(body));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 手动确认,并直接确认多个
channel.basicAck(envelope.getDeliveryTag(), true);
}
};
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}
basicQos:限流,后面高级特性消费端限流部分会说,channel.basicQos(1); 表示消息抓取数量,手动确认之后再抓取下一个
prefetchCount - 服务器将传递的最大消息数,如果无限制则为 0
basicAck:确认一条或多条收到的消息。从 AMQP.Basic.GetOk 或 AMQP.Basic.Deliver 方法提供 deliveryTag,其中包含正在确认的接收消息
deliveryTag – 收到的 AMQP.Basic.GetOk 或 AMQP.Basic.Deliver 的标签
multiple – true 以确认所有消息,包括提供的交付标签; false 仅确认提供的交付标签
(4) 消费者2
com.atguigu.rabbitmq.work.WorkQueueConsumer2
WorkQueueConsumer2 和 WorkQueueConsumer1 一样
3、测试
启动两个消费者,然后再启动生产者发送消息;到IDEA的两个消费者对应的控制台查看是否竞争性的接收到消息
WorkQueueConsumer1 控制台:
body1:0hello rabbitmq~~~
body1:2hello rabbitmq~~~
body1:4hello rabbitmq~~~
body1:6hello rabbitmq~~~
body1:8hello rabbitmq~~~
WorkQueueConsumer2 控制台:
body1:1hello rabbitmq~~~
body1:3hello rabbitmq~~~
body1:5hello rabbitmq~~~
body1:7hello rabbitmq~~~
body1:9hello rabbitmq~~~
不断被消费
4、小结
- 在一个队列中如果有多个消费者,那么消费者之间对于同一个消息的关系是竞争的关系
- Work Queues 对于任务过重或任务较多情况使用工作队列可以提高任务处理的速度
(二) 订阅模式类型
订阅模式示例图:
前面2个案例中,只有3个角色:
- P:生产者,也就是要发送消息的程序
- C:消费者:消息的接受者,会一直等待消息到来
- Queue:消息队列,图中红色部分
而在订阅模型中,多了一个exchange角色,而且过程略有变化:
- P:生产者,也就是要发送消息的程序,但是不再发送到队列中,而是发给X(交换机)
- C:消费者,消息的接受者,会一直等待消息到来
- Queue:消息队列,接收消息、缓存消息
- Exchange:交换机,图中的X。一方面,接收生产者发送的消息。另一方面,知道如何处理消息,例如递交给某个特别队列、递交给所有队列、或是将消息丢弃。到底如何操作,取决于Exchange的类型;Exchange有常见以下3种类型:
- Fanout:广播(扇出),将消息交给所有绑定到交换机的队列
- Direct:定向,把消息交给符合指定routing key 的队列
- Topic:主题(通配符),把消息交给符合 routing pattern(路由模式) 的队列
Exchange(交换机)只负责转发消息,不具备存储消息的能力,因此如果没有任何队列与Exchange绑定,或者没有符合路由规则的队列,那么消息会丢失!
(三) Publish/Subscribe发布与订阅模式
1、模式说明
发布订阅模式:
- 每个消费者监听自己的队列
- 生产者将消息发给broker,由交换机将消息转发到绑定此交换机的每个队列,每个绑定交换机的队列都将接收到消息
2、代码
(1) 生产者
com.atguigu.rabbitmq.fanout.FanoutProducer
public class FanoutProducer {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String exchangeName = "test_fanout";
/**
* 创建(声明)交换机
*
* Exchange.DeclareOk exchangeDeclare(String exchange,
* BuiltinExchangeType type,
* boolean durable,
* boolean autoDelete,
* boolean internal,
* Map<String, Object> arguments) throws IOException;
*
* @param exchange 交换机名称
* @param type 交换机类型:
* DIRECT("direct"):定向
* FANOUT("fanout"):扇形(广播)发送消息到每一个与之绑定队列
* TOPIC("topic"):通配符的方式
* HEADERS("headers"):参数匹配
* @param durable 是否持久化
* @param autoDelete 自动删除
* @param internal 内部使用。 一般false
* @param arguments 参数
* @return a declaration-confirm method to indicate the exchange was successfully declared
*/
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT, true, false, false, null);
// 创建队列
String queue1Name = "test_fanout_queue1";
String queue2Name = "test_fanout_queue2";
channel.queueDeclare(queue1Name, true, false, false, null);
channel.queueDeclare(queue2Name, true, false, false, null);
/**
* 绑定队列和交换机
* Bind a queue to an exchange, with no extra arguments.
* @param queue the name of the queue
* @param exchange the name of the exchange
* @param routingKey the routing key to use for the binding
* 如果交换机的类型为fanout ,routingKey设置为""
* @return a binding-confirm method if the binding was successfully created
* @throws java.io.IOException if an error is encountered
*/
channel.queueBind(queue1Name, exchangeName, "");
channel.queueBind(queue2Name, exchangeName, "");
String body = "日志信息:张三调用了findAll方法...日志级别:info...";
// 发送消息
channel.basicPublish(exchangeName, "", null, body.getBytes());
// 释放资源
channel.close();
connection.close();
}
}
Channel 的 exchangeDeclare 方法:
BuiltinExchangeType:
Channel 的 queueBind 方法:
启动:
(2) 消费者1
com.atguigu.rabbitmq.fanout.FanoutConsumer1
public class FanoutConsumer1 {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String queue1Name = "test_fanout_queue1";
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:" + new String(body));
System.out.println("将日志信息打印到控制台.....");
}
};
channel.basicConsume(queue1Name, true, consumer);
}
}
(3) 消费者2
com.atguigu.rabbitmq.fanout.FanoutConsumer2
public class FanoutConsumer2 {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String queue2Name = "test_fanout_queue2";
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:" + new String(body));
System.out.println("将日志信息打印到控制台.....");
}
};
channel.basicConsume(queue2Name, true, consumer);
}
}
3、测试
启动所有消费者,然后使用生产者发送消息;在每个消费者对应的控制台可以查看到生产者发送的所有消息;到达广播的效果
body:日志信息:张三调用了findAll方法...日志级别:info...
将日志信息打印到控制台.....
在执行完测试代码后,其实到RabbitMQ的管理后台找到Exchanges选项卡,点击 fanout_exchange 的交换机,可以查看到如下的绑定:
4、小结
交换机需要与队列进行绑定,绑定之后;一个消息可以被多个消费者都收到
发布订阅模式与工作队列模式的区别:
- 工作队列模式不用定义交换机,而发布/订阅模式需要定义交换机
- 发布/订阅模式的生产方是面向交换机发送消息,工作队列模式的生产方是面向队列发送消息(底层使用默认交换机)
- 发布/订阅模式需要设置队列和交换机的绑定,工作队列模式不需要设置,实际上工作队列模式会将队列绑定到默认的交换机
(四) Routing路由模式
1、模式说明
- 队列与交换机的绑定,不能是任意绑定了,而是要指定一个RoutingKey(路由key)
- 消息的发送方在向 Exchange发送消息时,也必须指定消息的 RoutingKey
- Exchange不再把消息交给每一个绑定的队列,而是根据消息的RoutingKey进行判断,只有队列的Routingkey与消息的 Routingkey完全一致,才会接收到消息
图解:
- P:生产者,向Exchange发送消息,发送消息时,会指定一个routing key
- X:Exchange(交换机),接收生产者的消息,然后把消息递交给与routing key完全匹配的队列
- C1:消费者,其所在队列指定了需要 routing key 为 error 的消息
- C2:消费者,其所在队列指定了需要 routing key 为 info、error、warning 的消息
2、代码
在编码上与 Publish/Subscribe 发布与订阅模式的区别是交换机的类型为:Direct;还有队列绑定交换机的时候需要指定 routing key
(1) 生产者
com.atguigu.rabbitmq.routing.RoutingProducer
public class RoutingProducer {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String exchangeName = "test_direct";
// 创建交换机
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, false, null);
// 创建队列
String queue1Name = "test_direct_queue1";
String queue2Name = "test_direct_queue2";
// 声明(创建)队列
channel.queueDeclare(queue1Name, true, false, false, null);
channel.queueDeclare(queue2Name, true, false, false, null);
// 队列绑定交换机
// 队列1绑定 error
channel.queueBind(queue1Name, exchangeName, "error");
// 队列2绑定 info、error、warning
channel.queueBind(queue2Name, exchangeName, "info");
channel.queueBind(queue2Name, exchangeName, "error");
channel.queueBind(queue2Name, exchangeName, "warning");
String message = "日志信息:张三调用了delete方法.错误了,日志级别warning";
// 发送消息
for (int i = 0; i < 10; i++) {
channel.basicPublish(exchangeName, "warning", null, message.getBytes());
System.out.println(message);
}
channel.close();
connection.close();
}
}
运行:
生产者控制台:
日志信息:张三调用了delete方法.错误了,日志级别warning
(2) 消费者1
com.atguigu.rabbitmq.routing.RoutingConsumer1
public class RoutingConsumer1 {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String queue1Name = "test_direct_queue1";
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:" + new String(body));
System.out.println("将日志信息打印到控制台.....");
}
};
Thread.sleep(5000);
channel.basicConsume(queue1Name, true, consumer);
}
}
(3) 消费者2
com.atguigu.rabbitmq.routing.RoutingConsumer2
public class RoutingConsumer2 {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String queue2Name = "test_direct_queue2";
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:" + new String(body));
System.out.println("将日志信息存储到数据库.....");
}
};
channel.basicConsume(queue2Name, true, consumer);
}
}
3、测试
启动所有消费者,然后使用生产者发送消息;在消费者对应的控制台可以查看到生产者发送对应routing key对应队列的消息;到达按照需要接收的效果
消费者1控制台:啥都没得
消费者2控制台:
body:日志信息:张三调用了delete方法.错误了,日志级别warning
将日志信息打印到控制台.....
body:日志信息:张三调用了delete方法.错误了,日志级别warning
将日志信息打印到控制台.....
......
4、小结
Routing模式要求队列在绑定交换机时要指定routing key,消息会转发到符合routing key的队列
(五) Topics通配符模式
1、模式说明
Topic类型与Direct相比,都是可以根据RoutingKey把消息路由到不同的队列。只不过Topic类型Exchange可以让队列在绑定Routing key 的时候使用通配符!
Routingkey 一般都是有一个或多个单词组成,多个单词之间以”.”分割,例如: item.insert
通配符规则:
- #:匹配零个或多个词
- *:匹配不多不少恰好1个词
举例:
- item.#:能够匹配item.insert.abc 或者 item.insert
- item.*:只能匹配item.insert
图解:
- 红色Queue:绑定的是 usa.# ,因此凡是以 usa.开头的 routing key 都会被匹配到
- 黄色Queue:绑定的是 #.news,因此凡是以 .news 结尾的 routing key 都会被匹配
2、代码
(1) 生产者
使用 topic 类型的 Exchange,发送消息的 routing key有3种: order.info
com.atguigu.rabbitmq.topic.TopicProducer
public class TopicProducer {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String exchangeName = "test_topic";
// 声明交换机
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, true, false, false, null);
String queue1Name = "test_topic_queue1";
String queue2Name = "test_topic_queue2";
// 声明队列
channel.queueDeclare(queue1Name, true, false, false, null);
channel.queueDeclare(queue2Name, true, false, false, null);
/**
* 绑定队列和交换机
* routing key:系统的名称.日志的级别
* 需求: 所有error级别的日志存入数据库,所有order系统的日志存入数据库
*/
channel.queueBind(queue1Name, exchangeName, "#.error");
channel.queueBind(queue1Name, exchangeName, "order.*");
channel.queueBind(queue2Name, exchangeName, "*.*");
String body = "日志信息:张三调用了findAll方法...日志级别:info...";
// 发送消息 goods.info,goods.error
channel.basicPublish(exchangeName, "order.info", null, body.getBytes());
channel.close();
connection.close();
}
}
运行程序:
(2) 消费者1
接收两种类型的消息:更新商品和删除商品
com.atguigu.rabbitmq.topic.TopicConsumer1
public class TopicConsumer1 {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String queue1Name = "test_topic_queue1";
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:" + new String(body));
}
};
channel.basicConsume(queue1Name, true, consumer);
}
}
(3) 消费者2
接收所有类型的消息:新增商品,更新商品和删除商品
com.atguigu.rabbitmq.topic.TopicConsumer2
public class TopicConsumer2 {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
String queue2Name = "test_topic_queue2";
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body:" + new String(body));
}
};
channel.basicConsume(queue2Name, true, consumer);
}
}
3、测试
启动所有消费者,然后使用生产者发送消息;在消费者对应的控制台可以查看到生产者发送对应routing key对应队列的消息;到达按照需要接收的效果;并且这些routing key可以使用通配符
消费者1控制台:
body:日志信息:张三调用了findAll方法...日志级别:info...
消费者2控制台:
body:日志信息:张三调用了findAll方法...日志级别:info...
4、小结
Topic 主题模式可以实现 Publish/Subscribe 发布与订阅模式 和 Routing路由模式的功能;只是Topic在配置 routing key 的时候可以使用通配符,显得更加灵活
(六) 模式总结
1、简单模式 HelloWorld
一个生产者、一个消费者,不需要设置交换机(使用默认的交换机)
2、工作队列模式 Work Queue
一个生产者、多个消费者(竞争关系),不需要设置交换机(使用默认的交换机)
3、发布订阅模式 Publish/subscribe
需要设置类型为fanout的交换机,并且交换机和队列进行绑定,当发送消息到交换机后,交换机会将消息发送到绑定的队列
4、路由模式 Routing
需要设置类型为direct的交换机,交换机和队列进行绑定,并且指定routing key,当发送消息到交换机后,交换机会根据 routing key将消息发送到对应的队列
5、通配符模式 Topic
需要设置类型为 topic 的交换机,交换机和队列进行绑定,并且指定通配符方式的 routing key,当发送消息到交换机后,交换机会根据 routing key 将消息发送到对应的队列
六、Spring 整合 RabbitMQ
(一) 搭建生产者工程
1、创建工程 spring-rabbitmq-producer
2、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3、rabbitmq.properties
rabbitmq.host=192.168.200.188
rabbitmq.port=5672
rabbitmq.username=admin
rabbitmq.password=123456
rabbitmq.virtual-host=/
4、spring-rabbitmq.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--加载配置文件-->
<context:property-placeholder location="classpath:rabbitmq.properties"/>
<!-- 定义rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<!--定义管理交换机、队列-->
<rabbit:admin connection-factory="connectionFactory"/>
<!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机
默认交换机类型为direct,名字为:"",路由键为队列的名称
-->
<rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--定义广播交换机中的持久化队列,不存在则自动创建-->
<rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
<!--定义广播交换机中的持久化队列,不存在则自动创建-->
<rabbit:queue id="spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>
<!--定义广播类型交换机;并绑定上述两个队列-->
<rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="spring_fanout_queue_1"/>
<rabbit:binding queue="spring_fanout_queue_2"/>
</rabbit:bindings>
</rabbit:fanout-exchange>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--定义广播交换机中的持久化队列,不存在则自动创建-->
<rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
<!--定义广播交换机中的持久化队列,不存在则自动创建-->
<rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
<!--定义广播交换机中的持久化队列,不存在则自动创建-->
<rabbit:queue id="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>
<rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding pattern="atguigu.*" queue="spring_topic_queue_star"/>
<rabbit:binding pattern="atguigu.#" queue="spring_topic_queue_well"/>
<rabbit:binding pattern="guigu.#" queue="spring_topic_queue_well2"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>
5、发送消息
com.atguigu.rabbitmq.SpringProducer
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq.xml")
public class SpringProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 只发队列消息
* 默认交换机类型为 direct
* 交换机的名称为空,路由键为队列的名称
*/
@Test
public void queueTest() {
// 路由键与队列同名
rabbitTemplate.convertAndSend("spring_queue", "只发队列spring_queue的消息。");
}
/**
* 发送广播
* 交换机类型为 fanout
* 绑定到该交换机的所有队列都能够收到消息
*/
@Test
public void fanoutTest() {
/**
* 参数1:交换机名称
* 参数2:路由键名(广播设置为空)
* 参数3:发送的消息内容
*/
rabbitTemplate.convertAndSend("spring_fanout_exchange", "", "发送到spring_fanout_exchange交换机的广播消息");
}
/**
* 通配符
* 交换机类型为 topic
* 匹配路由键的通配符,*表示一个单词,#表示多个单词
* 绑定到该交换机的匹配队列能够收到对应消息
*/
@Test
public void topicTest() {
/**
* 参数1:交换机名称
* 参数2:路由键名
* 参数3:发送的消息内容
*/
rabbitTemplate.convertAndSend("spring_topic_exchange", "atguigu.bj", "发送到spring_topic_exchange交换机atguigu.bj的消息");
rabbitTemplate.convertAndSend("spring_topic_exchange", "atguigu.bj.1", "发送到spring_topic_exchange交换机atguigu.bj.1的消息");
rabbitTemplate.convertAndSend("spring_topic_exchange", "atguigu.bj.2", "发送到spring_topic_exchange交换机atguigu.bj.2的消息");
rabbitTemplate.convertAndSend("spring_topic_exchange", "guigu.cn", "发送到spring_topic_exchange交换机guigu.cn的消息");
}
}
(二) 搭建消费者工程
1、创建工程spring-rabbitmq-consumer
2、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3、rabbitmq.properties
rabbitmq.host=192.168.200.188
rabbitmq.port=5672
rabbitmq.username=admin
rabbitmq.password=123456
rabbitmq.virtual-host=/
4、spring-rabbitmq.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--加载配置文件-->
<context:property-placeholder location="classpath:rabbitmq.properties"/>
<!-- 定义rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<bean id="springQueueListener" class="com.atguigu.rabbitmq.listener.SpringQueueListener"/>
<bean id="fanoutListener1" class="com.atguigu.rabbitmq.listener.FanoutListener1"/>
<bean id="fanoutListener2" class="com.atguigu.rabbitmq.listener.FanoutListener2"/>
<bean id="topicListenerStar" class="com.atguigu.rabbitmq.listener.TopicListenerStar"/>
<bean id="topicListenerWell" class="com.atguigu.rabbitmq.listener.TopicListenerWell"/>
<bean id="topicListenerWell2" class="com.atguigu.rabbitmq.listener.TopicListenerWell2"/>
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
<rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
<rabbit:listener ref="fanoutListener1" queue-names="spring_fanout_queue_1"/>
<rabbit:listener ref="fanoutListener2" queue-names="spring_fanout_queue_2"/>
<rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/>
<rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/>
<rabbit:listener ref="topicListenerWell2" queue-names="spring_topic_queue_well2"/>
</rabbit:listener-container>
</beans>
5、消息监听器
(1) 队列监听器
com.atguigu.rabbitmq.listener.SpringQueueListener
public class SpringQueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String msg = new String(message.getBody(), "utf-8");
System.out.printf("接收路由名称为:%s,路由键为:%s,队列名为:%s的消息:%s \n",
message.getMessageProperties().getReceivedExchange(),
message.getMessageProperties().getReceivedRoutingKey(),
message.getMessageProperties().getConsumerQueue(),
msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加一个测试类,进行测试:com.atguigu.rabbitmq.ConsumerTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq.xml")
public class ConsumerTest {
/**
* 如果不加while(true),这个test方法一运行就立即结束,运行方法结束了那么这个IOC容器也不存在了,
* 从而使得IOC容器中声明的监听器bean对象也不在了,那样就不能监听到消息了,就可能出现监听器还没来得及消费消息,
* 这个单元测试方法就已经结束了;加了while(true)后这个单元测试方法不会结束,只要有消息来了就可以消费了
*/
@Test
public void test1() {
boolean flag = true;
while (flag) {
}
}
}
(2) 广播监听器1
创建 FanoutListener1.java
public class FanoutListener1 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String msg = new String(message.getBody(), "utf-8");
System.out.printf("广播监听器1:接收路由名称为:%s,路由键为:%s,队列名为:%s的消息:%s \n",
message.getMessageProperties().getReceivedExchange(),
message.getMessageProperties().getReceivedRoutingKey(),
message.getMessageProperties().getConsumerQueue(),
msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
(3) 广播监听器2
创建 FanoutListener2.java
public class FanoutListener2 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String msg = new String(message.getBody(), "utf-8");
System.out.printf("广播监听器2:接收路由名称为:%s,路由键为:%s,队列名为:%s的消息:%s \n",
message.getMessageProperties().getReceivedExchange(),
message.getMessageProperties().getReceivedRoutingKey(),
message.getMessageProperties().getConsumerQueue(),
msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
(4) 星号通配符监听器
创建 TopicListenerStar.java
public class TopicListenerStar implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String msg = new String(message.getBody(), "utf-8");
System.out.printf("通配符*监听器:接收路由名称为:%s,路由键为:%s,队列名为:%s的消息:%s \n",
message.getMessageProperties().getReceivedExchange(),
message.getMessageProperties().getReceivedRoutingKey(),
message.getMessageProperties().getConsumerQueue(),
msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
(5) 井号通配符监听器
创建 TopicListenerWell.java
public class TopicListenerWell implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String msg = new String(message.getBody(), "utf-8");
System.out.printf("通配符#监听器:接收路由名称为:%s,路由键为:%s,队列名为:%s的消息:%s \n",
message.getMessageProperties().getReceivedExchange(),
message.getMessageProperties().getReceivedRoutingKey(),
message.getMessageProperties().getConsumerQueue(),
msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
(6) 井号通配符监听器2
创建 TopicListenerWell2.java
public class TopicListenerWell2 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String msg = new String(message.getBody(), "utf-8");
System.out.printf("通配符#监听器2:接收路由名称为:%s,路由键为:%s,队列名为:%s的消息:%s \n",
message.getMessageProperties().getReceivedExchange(),
message.getMessageProperties().getReceivedRoutingKey(),
message.getMessageProperties().getConsumerQueue(),
msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
七、RabbitMQ高级特性
(一) 消息的可靠投递
在使用 RabbitMQ 的时候,作为消息发送方希望杜绝任何消息丢失或者投递失败景。RabbitMQ 为我们提供了两种方式用来控制消息的投递可靠性模式
- confirm 确认模式
- return 退回模式
rabbitmq 整个消息投递的路径为:producer—>rabbitmq broker—>exchange—>queue—>consumer
- 消息从 producer 到 exchange 则会返回一个 confirmCallback
- 消息从 exchange–>queue 投递失败则会返回一个 returnCallback
我们将利用这两个 callback 控制消息的可靠性投递
1、生产者代码实现
(1) 搭建 rabbitmq-advanced-features-producer
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
rabbitmq.properties
rabbitmq.host=192.168.200.188
rabbitmq.port=5672
rabbitmq.username=admin
rabbitmq.password=123456
rabbitmq.virtual-host=/
spring-rabbitmq-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--加载配置文件-->
<context:property-placeholder location="classpath:rabbitmq.properties"/>
<!-- 定义rabbitmq connectionFactory
确认模式开启:publisher-confirms="true"
-->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"
publisher-confirms="true"
publisher-returns="true"
/>
<!--定义管理交换机、队列-->
<rabbit:admin connection-factory="connectionFactory"/>
<!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
<!--消息可靠性投递(生产端)-->
<rabbit:queue id="test_queue_confirm" name="test_queue_confirm"/>
<rabbit:direct-exchange name="test_exchange_confirm">
<rabbit:bindings>
<rabbit:binding queue="test_queue_confirm" key="confirm"/>
</rabbit:bindings>
</rabbit:direct-exchange>
</beans>
(2) 创建测试类,添加确认模式
com.atguigu.rabbitmq.ProducerTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 确认模式:
* 步骤:
* 1. 确认模式开启:ConnectionFactory中开启publisher-confirms="true"
* 2. 在rabbitTemplate定义ConfirmCallBack回调函数
*/
@Test
public void testConfirm() {
// 定义回调
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if (ack) {
//接收成功
System.out.println("接收成功消息" + cause);
} else {
//接收失败
System.out.println("接收失败消息" + cause);
//做一些处理,让消息再次发送。
}
}
});
// 发送消息
for (int i = 0; i < 5; i++) {
rabbitTemplate.convertAndSend("test_exchange_confirm", "confirm", "message confirm....");//成功
//rabbitTemplate.convertAndSend("test_exchange_confirm000", "confirm", "message confirm....");//失败
}
}
}
运行程序,控制台打印:【成功】
接收成功消息null
接收成功消息null
接收成功消息null
接收成功消息null
接收成功消息null
运行程序,控制台打印:【失败】
接收失败消息channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'test_exchange_confirm000' in vhost '/', class-id=60, method-id=40)
接收失败消息channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'test_exchange_confirm000' in vhost '/', class-id=60, method-id=40)
接收失败消息channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'test_exchange_confirm000' in vhost '/', class-id=60, method-id=40)
接收失败消息channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'test_exchange_confirm000' in vhost '/', class-id=60, method-id=40)
接收失败消息channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'test_exchange_confirm000' in vhost '/', class-id=60, method-id=40)
消息队列中还是5条消息
(3) 添加回退模式
com.atguigu.rabbitmq.ProducerTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 回退模式: 当消息发送给Exchange后,Exchange路由到Queue失败时 才会执行 ReturnCallBack
* 步骤:
* 1. 开启回退模式:publisher-returns="true"
* 2. 设置ReturnCallBack
* 3. 设置Exchange处理消息的模式:
* 1). 如果消息没有路由到Queue,则丢弃消息(默认)
* 2). 如果消息没有路由到Queue,返回给消息发送方ReturnCallBack
* rabbitTemplate.setMandatory(true);
*/
@Test
public void testReturn() {
// 设置交换机处理失败消息的模式
rabbitTemplate.setMandatory(true);
// 设置ReturnCallBack
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
/**
* @param message 消息对象
* @param replyCode 错误码
* @param replyText 错误信息
* @param exchange 交换机
* @param routingKey 路由键
*/
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
System.out.println("return 执行了....");
System.out.println("message = " + message);
System.out.println("replyCode = " + replyCode);
System.out.println("replyText = " + replyText);
System.out.println("exchange = " + exchange);
System.out.println("routingKey = " + routingKey);
// 处理
}
});
for (int i = 0; i < 5; i++) {
// 发送消息
rabbitTemplate.convertAndSend("test_exchange_confirm", "confirm", "message confirm....");//成功
// rabbitTemplate.convertAndSend("test_exchange_confirm", "confirm1", "message confirm....");//失败
}
}
}
运行程序,控制台打印:【成功】啥都没有
队列中有10条消息(加上确认模式的5条)
运行程序,控制台打印:【失败】
return 执行了....
message = (Body:'message confirm....' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, deliveryTag=0])
replyCode = 312
replyText = NO_ROUTE
exchange = test_exchange_confirm
routingKey = confirm1
...打印五次
消息队列中还是10条消息
2、消息的可靠投递小结
设置 ConnectionFactory 的 publisher-confirms=“true” 开启确认模式
- 使用 rabbitTemplate.setConfirmCallback 设置回调函数,当消息发送到 exchange 后回调 confirm 方法,在方法中判断 ack,如果为 true,则发送成功,如果为false,则发送失败,需要处理
设置 ConnectionFactory 的 publisher-returns=“true” 开启 退回模式
- 使用 rabbitTemplate.setReturnCallback 设置退回函数,当消息从 exchange 路由到 queue 失败后,如果设置了 rabbitTemplate.setMandatory(true) 参数,则会将消息退回给 producer 并执行回调函数 returnedMessage
3、消费者 Ack
ack 指 Acknowledge,确认,表示消费端收到消息后的确认方式
有二种确认方式:
- 自动确认:acknowledge=“none” 默认
- 手动确认:acknowledge=“manual”
其中自动确认是指,当消息一旦被 Consumer 接收到,则自动确认收到,并将相应 message 从 RabbitMQ 的消息缓存中移除,但是在实际业务处理中,很可能消息接收到,业务处理出现异常,那么该消息就会丢失
如果设置了手动确认方式,则需要在业务处理成功后,调用 channel.basicAck(),手动签收;如果出现异常,则调用channel.basicNack()方法,让其自动重新发送消息
(1) 搭建 rabbitmq-advanced-features-consumer
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
rabbitmq.properties
rabbitmq.host=192.168.200.188
rabbitmq.port=5672
rabbitmq.username=admin
rabbitmq.password=123456
rabbitmq.virtual-host=/
rabbitmq.switch=true
spring-rabbitmq-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--加载配置文件-->
<context:property-placeholder location="classpath:rabbitmq.properties"/>
<!-- 定义rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<context:component-scan base-package="com.atguigu.listener"/>
<!--定义监听器容器
acknowledge="none":自动确认
acknowledge="manual":手动确认
-->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="none">
<rabbit:listener ref="ackListener" queue-names="test_queue_confirm"></rabbit:listener>
</rabbit:listener-container>
</beans>
(2) 自动确认
添加监听器:com.atguigu.listener.AckListener
@Component
public class AckListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println(new String(message.getBody()));
}
}
(3) 添加测试类
com.atguigu.ConsumerTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class ConsumerTest {
@Test
public void test() {
while (true) {
}
}
}
启动,会一直监听消息:控制台打印
message confirm....
message confirm....
message confirm....
message confirm....
message confirm....
// 10次【生产者确认模式+回退模式发送的10次】
(4) 手动确认
修改 spring-rabbitmq-consumer.xml,acknowledge="manual"
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
<rabbit:listener ref="ackListener" queue-names="test_queue_confirm"></rabbit:listener>
</rabbit:listener-container>
修改监听器:AckListener
/**
* 自动确认:implements MessageListener
* acknowledge="none"
* <p>
* 手动确认:implements ChannelAwareMessageListener
* acknowledge="manual"
*
* @author: yangfan
* @Description:
* @create: 2022-03-11 14:02
*/
@Component
public class AckListener implements ChannelAwareMessageListener {
@Value("${rabbitmq.switch}")
private Boolean mysSwitch;
/// 自动确认:implements MessageListener
// @Override
// public void onMessage(Message message) {
// if (mysSwitch) {
// System.out.println(new String(message.getBody()));
// } else {
// System.out.println("开关未打开");
// }
// }
/// 手动确认:implements ChannelAwareMessageListener
@Override
public void onMessage(Message message, Channel channel) throws Exception {
Thread.sleep(1000);
// 获取消息传递标记
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try {
// ① 接收消息
System.out.println(new String(message.getBody()));
// ② 处理业务逻辑
System.out.println("处理业务逻辑");
int i = 3 / 0; // 出现错误
// ③ 手动签收
/**
* 第一个参数:表示收到的标签
* 第二个参数:如果为true表示可以签收所有的消息
*/
channel.basicAck(deliveryTag, true);
} catch (Exception e) {
e.printStackTrace();
// ④ 拒绝签收
/**
* 第三个参数:requeue:重回队列。
* 设置为true,则消息重新回到 queue,broker 会重新发送该消息给消费端
*/
channel.basicNack(deliveryTag, true, true);
}
}
}
tips:RabbitMQ中channel.basicAck的第二参数的作用是什么?https://www.zhihu.com/question/400248948
每个channel发送的消息都是有一个唯一标记的,就是deliveryTag,从1开始递增。消费端 channel.basicQos(10); 执行这行代码的意思是每次从broker中获取不超过10条消息。举个例子,假设broker中有10条消息,这10条消息的deliveryTag是 1 2 3 … 10,那么消费端只需要和 broker 有一次网络开销就可以取出这10条消息。那么在消费端执行以下代码:
channel.basicQos(10); //每次读取10条消息
channel.basicConsume(AckProvider.QUEUE,false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
long tag=envelope.getDeliveryTag();
System.out.println("消费消息: deliveryTag:"+tag);
//到了最后一条消息,deliveryTag=10。这时候才和broker ack,意思是 deliveryTag<=10的消息都ack了。这样只需要和broker一次网络开销
if(tag==10){
getChannel().basicAck(10,true);
}
}
});
启动测试类,会一直监听消息,控制台打印:
message confirm....
处理业务逻辑
java.lang.ArithmeticException: / by zero
at com.atguigu.listener.AckListener.onMessage(AckListener.java:52)
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:272)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1542)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1468)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer$$Lambda$51/1051876890.invokeListener(Unknown Source)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1456)
... 打印了10次
当程序报错,程序会拒绝签收,直到修改错误,修改上面的监听器,注释除 0 错误,重新运行程序
当注释掉错误重新启动测试类后:
控制台:
message confirm....
处理业务逻辑
...打印n次
消息队列:
4、消费者 Ack 小结
- 在 rabbit:listener-container 标签中设置 acknowledge 属性,设置 ack 方式 none:自动确认,manual:手动确认
- 如果在消费端没有出现异常,则调用 channel.basicAck(deliveryTag,true); 方法确认签收消息
- 如果出现异常,则在 catch 中调用 basicNack,拒绝消息,让MQ重新发送消息
(二) 消费端限流
1、QosListener
在项目 rabbitmq-advanced-features-consumer,新建 com.atguigu.listener.QosListener
/**
* Consumer 限流机制
* 1. 确保消息被确认。不确认是不继续处理其他消息的
* 2. listener-container配置属性
* prefetch = 1,表示消费端每次从mq拉去一条消息来消费,直到手动确认消费完毕后,才会继续拉取下一条消息
*
* @author: yangfan
* @Description:
* @create: 2022-03-11 16:35
*/
@Component
public class QosListener implements ChannelAwareMessageListener {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
//1.获取消息
System.out.println(new String(message.getBody()));
}
}
2、修改 spring-rabbitmq-consumer.xml
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" prefetch="1">
<!--<rabbit:listener ref="ackListener" queue-names="test_queue_confirm"/>-->
<rabbit:listener ref="qosListener" queue-names="test_queue_confirm"/>
</rabbit:listener-container>
3、测试
-
运行消费者,等待消息….
-
在 rabbitmq-advanced-features-producer 运行 ProducerTest
消费者测试类控制台:每次消费一条消息:
查看后台 ,有 4 条消息待消费 ,有 1 条消息未确认
修改消费者配置文件 ,去掉 prefetch=“1” 会发现一次就可以消费所有消息
运行消费者测试类 ConsumerTest
运行前:
运行后:
修改 QosListener,添加手动签收方法,这样就可以确认消费限流
// 2.签收
channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
basicAck:确认一条或多条收到的消息。从 AMQP.Basic.GetOk 或 AMQP.Basic.Deliver 方法提供 deliveryTag,其中包含正在确认的接收消息
deliveryTag – 收到的 AMQP.Basic.GetOk 或 AMQP.Basic.Deliver 的标签
multiple – true 以确认所有消息,包括提供的交付标签; false 仅确认提供的交付标签
运行消费者测试程序
运行前:
运行后:
4、消费端限流小结
- 在 rabbit:listener-container 中配置 prefetch 属性设置消费端一次拉取多少条消息
- 消费端的必须确认才会继续处理其他消息
(三) TTL
TTL 全称 Time To Live(存活时间/过期时间)
当消息到达存活时间后,还没有被消费,会被自动清除,RabbitMQ可以对消息设置过期时间,也可以对整个队列(Queue)设置过期时间
TTL:过期时间
- 队列统一过期
- 消息单独过期
如果设置了消息的过期时间,也设置了队列的过期时间,它以时间短的为准
1、控制后台演示消息过期
(1) 修改管理后台界面,增加队列
- 参数:表示过期时间,单位毫秒 ,10000表示10秒
(2) 增加交换机
(3) 绑定队列
(4) 发送消息
Delivery mode:2-Persistent表示需要进行持久化
tips:如果是在 Exchange 里边发送消息的话是没有 Delivery mode 选项的
在 Queues 中发布消息是有 Delivery mode 选项的
(5) 查看消息
可以看到消息,但十秒之后,消息自动消失,因为我们设置了十秒消息过期
2、代码实现
(1) 队列统一过期
修改 rabbitmq-advanced-features-producer 项目的 配置文件 spring-rabbitmq-producer.xml
加上 TTL 的配置:
<!--TTL 队列-->
<rabbit:queue name="test_queue_ttl" id="test_queue_ttl">
<!--设置queue的参数-->
<rabbit:queue-arguments>
<!--
设置x-message-ttl队列的过期时间
默认情况下value-type的类型是String类型,但时间的类型是number类型,所以需要设置成integer类型
-->
<entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"/>
</rabbit:queue-arguments>
</rabbit:queue>
<!--设置交换机-->
<rabbit:topic-exchange name="test_exchange_ttl">
<!--交换机绑定队列-->
<rabbit:bindings>
<rabbit:binding pattern="ttl.#" queue="test_queue_ttl"/>
</rabbit:bindings>
</rabbit:topic-exchange>
测试类 ProducerTest
在测试类 ProducerTest 中,添加 testTTL 测试方法,发送消息
tips:启动测试类之前先把之前手动在控制台创建的交换机和队列删除,否则会报错
@Test
public void testTTL() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("test_exchange_ttl", "ttl.hehe", "message ttl");
}
}
查看控制台,发现有10条消息,十秒之后自动过期
![image-20220311180608354](https://gitee.com/yangfan1814012468/picgo/raw/master/img/202203111806679.png)
(2) 消息过期
/**
* TTL:过期时间
* 1. 队列统一过期
* 2. 消息单独过期
* 如果设置了消息的过期时间,也设置了队列的过期时间,它以时间短的为准。
*/
@Test
public void testMessageTtl() {
// 消息后处理对象,设置一些消息的参数信息
MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
// 1.设置message的信息
// 第二个方法:消息的过期时间 ,5秒之后过期
message.getMessageProperties().setExpiration("5000");
// 2.返回该消息
return message;
}
};
// 消息单独过期
rabbitTemplate.convertAndSend("test_exchange_ttl", "ttl.hehe", "message ttl....", messagePostProcessor);
}
(四) 死信队列
死信队列,英文缩写:DLX 。DeadLetter Exchange(死信交换机),当消息成为 Dead message 后,可以被重新发送到另一个交换机,这个交换机就是DLX
什么是死信队列:
先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,producer 将消息投递到 broker 或者直接到 queue里了,consumer 从 queue 取出消息进行消费,但某些时候由于特定的原因导致 queue 中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信,自然就有了死信队列;
消息成为死信的三种情况:
- 原队列存在消息过期设置,消息到达超时时间未被消费;
- 队列消息数量到达限制;比如队列最大只能存储10条消息,而发了11条消息,根据先进先出,最先发的消息会进入死信队列;
- 消费者拒接消费消息,basicNack/basicReject,并且不把消息重新放入原目标队列,requeue=false;
死信的处理方式:
死信的产生既然不可避免,那么就需要从实际的业务角度和场景出发,对这些死信进行后续的处理,常见的处理方式大致有下面几种:
- 丢弃,如果不是很重要,可以选择丢弃
- 记录死信入库,然后做后续的业务分析或处理
- 通过死信队列,由负责监听死信的应用程序进行处理
综合来看更常用的做法是第三种,即通过死信队列,将产生的死信通过程序的配置路由到指定的死信队列,然后应用监听死信队列,对接收到的死信做后续的处理
队列绑定死信交换机:
给队列设置参数: x-dead-letter-exchange 和 x-dead-letter-routing-key
1、过期时间代码实现
修改生产者项目的配置文件 spring-rabbitmq-producer.xml,增加如下代码
<!--
死信队列:
1. 声明正常的队列(test_queue_dlx)和交换机(test_exchange_dlx)
2. 声明死信队列(queue_dlx)和死信交换机(exchange_dlx)
3. 正常队列绑定死信交换机
设置两个参数:
* x-dead-letter-exchange:死信交换机名称
* x-dead-letter-routing-key:发送给死信交换机的 routingkey
-->
<!--
1. 声明正常的队列(test_queue_dlx)和交换机(test_exchange_dlx)
-->
<rabbit:queue name="test_queue_dlx" id="test_queue_dlx">
<!--3. 正常队列绑定死信交换机-->
<rabbit:queue-arguments>
<!--3.1 x-dead-letter-exchange:死信交换机名称-->
<entry key="x-dead-letter-exchange" value="exchange_dlx"/>
<!--3.2 x-dead-letter-routing-key:发送给死信交换机的routingkey-->
<entry key="x-dead-letter-routing-key" value="dlx.hehe"/>
<!--4.1 设置队列的过期时间 ttl-->
<entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"/>
<!--4.2 设置队列的长度限制 max-length -->
<entry key="x-max-length" value="10" value-type="java.lang.Integer"/>
</rabbit:queue-arguments>
</rabbit:queue>
<!--正常交换机-->
<rabbit:topic-exchange name="test_exchange_dlx">
<rabbit:bindings>
<rabbit:binding pattern="test.dlx.#" queue="test_queue_dlx"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<!--
2. 声明死信队列(queue_dlx)和死信交换机(exchange_dlx)
-->
<rabbit:queue name="queue_dlx" id="queue_dlx"/>
<rabbit:topic-exchange name="exchange_dlx">
<rabbit:bindings>
<rabbit:binding pattern="dlx.#" queue="queue_dlx"/>
</rabbit:bindings>
</rabbit:topic-exchange>
在测试类中,添加如下方法,进行测试
/**
* 发送测试死信消息:
* 1. 过期时间
* 2. 长度限制
* 3. 消息拒收
*/
@Test
public void testDlx() {
//1. 测试过期时间,死信消息
rabbitTemplate.convertAndSend("test_exchange_dlx", "test.dlx.haha", "我是一条消息,我会死吗?");
}
2、长度限制代码实现
/**
* 发送测试死信消息:
* 1. 过期时间
* 2. 长度限制
* 3. 消息拒收
*/
@Test
public void testDlx() {
//1. 测试过期时间,死信消息
//rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","我是一条消息,我会死吗?");
//2. 测试长度限制后,消息死信
for (int i = 0; i < 20; i++) {
rabbitTemplate.convertAndSend("test_exchange_dlx", "test.dlx.haha", "我是一条消息,我会死吗?");
}
}
3、测试消息拒收
在消费者工程 创建 com.atguigu.listener.DlxListener
@Component
public class DlxListener implements ChannelAwareMessageListener {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try {
//1.接收转换消息
System.out.println(new String(message.getBody()));
//2. 处理业务逻辑
System.out.println("处理业务逻辑...");
int i = 3 / 0;//出现错误
//3. 手动签收
channel.basicAck(deliveryTag, true);
} catch (Exception e) {
//e.printStackTrace();
System.out.println("出现异常,拒绝接受");
//4.拒绝签收,不重回队列 requeue=false
channel.basicNack(deliveryTag, true, false);
}
}
}
修改消费者配置文件 spring-rabbitmq-consumer.xml
<!--定义监听器容器
acknowledge="none":自动确认
acknowledge="manual":手动确认
-->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
<!--<rabbit:listener ref="ackListener" queue-names="test_queue_confirm"/>-->
<!--<rabbit:listener ref="qosListener" queue-names="test_queue_confirm"/>-->
<!--定义监听器,监听正常队列-->
<rabbit:listener ref="dlxListener" queue-names="test_queue_dlx"/>
</rabbit:listener-container>
运行消费者测试类,queue_dlx 中的21条消息没有变化,因为 queue_dlx 是死信队列,而消费者监听的是正常的队列 test_queue_dlx
修改生产者测试代码
/**
* 发送测试死信消息:
* 1. 过期时间
* 2. 长度限制
* 3. 消息拒收
*/
@Test
public void testDlx() {
//1. 测试过期时间,死信消息
//rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","我是一条消息,我会死吗?");
//2. 测试长度限制后,消息死信
// for (int i = 0; i < 20; i++) {
// rabbitTemplate.convertAndSend("test_exchange_dlx", "test.dlx.haha", "我是一条消息,我会死吗?");
// }
//3. 测试消息拒收
rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","我是一条消息,我会死吗?");
}
}
发送消息,运行程序,查看控制天和后台管理界面
我是一条消息,我会死吗?
处理业务逻辑...
出现异常,拒绝接受
4、死信队列小结
- 死信交换机和死信队列和普通的没有区别
- 当消息成为死信后,如果该队列绑定了死信交换机,则消息会被死信交换机重新路由到死信队列
- 消息成为死信的三种情况:
- 原队列存在消息过期设置,消息到达超时时间未被消费;
- 队列消息长度(数量)到达限制;
- 消费者拒接消费消息,并且不重回队列;
(五) 延迟队列
延迟队列存储的对象肯定是对应的延时消息,所谓”延时消息”是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者才拿到这个消息进行消费。
场景:在订单系统中,一个用户下单之后通常有30分钟的时间进行支付,如果30分钟之内没有支付成功,那么这个订单将进行取消处理。这时就可以使用延时队列将订单信息发送到延时队列。
需求:
- 下单后,30分钟未支付,取消订单,回滚库存
- 新用户注册成功30分钟后,发送短信问候
实现方式:延迟队列
很可惜,在RabbitMQ中并未提供延迟队列功能。但是可以使用:TTL+死信队列 组合实现延迟队列的效果
1、生产者
修改生产者代码 ,修改生产者配置文件 spring-rabbitmq-producer.xml
<!--
1. 声明正常的队列(order_queue)和交换机(order_exchange)
-->
<rabbit:queue name="order_queue" id="order_queue">
<!--3. 正常队列绑定死信交换机,设置正常队列过期时间为30分钟-->
<rabbit:queue-arguments>
<!--3.1 x-dead-letter-exchange:死信交换机名称-->
<entry key="x-dead-letter-exchange" value="order_exchange_dlx"/>
<!--3.2 x-dead-letter-routing-key:发送给死信交换机的routingkey-->
<entry key="x-dead-letter-routing-key" value="dlx.order.cancel"/>
<!--4.1 设置队列的过期时间 ttl-->
<entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"/>
</rabbit:queue-arguments>
</rabbit:queue>
<!--正常交换机-->
<rabbit:topic-exchange name="order_exchange">
<rabbit:bindings>
<rabbit:binding pattern="order.#" queue="order_queue"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<!--2. 声明死信队列(order_queue_dlx)和死信交换机(order_exchange_dlx)-->
<rabbit:queue name="order_queue_dlx" id="order_queue_dlx"/>
<rabbit:topic-exchange name="order_exchange_dlx">
<rabbit:bindings>
<rabbit:binding pattern="dlx.order.#" queue="order_queue_dlx"/>
</rabbit:bindings>
</rabbit:topic-exchange>
修改生产者,添加测试方法
@Test
public void testDelay() throws InterruptedException {
// 1.发送订单消息。 将来是在订单系统中,下单成功后,发送消息
rabbitTemplate.convertAndSend("order_exchange", "order.msg", "订单信息:id=1,time=2022年3月14日13:42:39");
// 2.打印倒计时10秒
for (int i = 10; i > 0; i--) {
System.out.println(i + "...");
Thread.sleep(1000);
}
}
运行程序创建订单延时队列
倒计时前:
10...
9...
8...
7...
6...
5...
4...
3...
2...
1...
十秒后
2、消费者
修改消费者项目,添加 com.atguigu.listener.OrderListener
@Component
public class OrderListener implements ChannelAwareMessageListener {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try {
// 1.接收转换消息
System.out.println(new String(message.getBody()));
// 2. 处理业务逻辑
System.out.println("处理业务逻辑...");
System.out.println("根据订单id查询其状态...");
System.out.println("判断状态是否为支付成功");
System.out.println("取消订单,回滚库存....");
// 3. 手动签收
channel.basicAck(deliveryTag, true);
} catch (Exception e) {
//e.printStackTrace();
System.out.println("出现异常,拒绝接受");
// 4.拒绝签收,不重回队列 requeue=false
channel.basicNack(deliveryTag, true, false);
}
}
}
修改消费者配置文件 spring-rabbitmq-consumer.xml
消费者监听死信队列,消费由普通队列10s后过期路由到死信队列的消息
<!--定义监听器容器
acknowledge="none":自动确认
acknowledge="manual":手动确认
-->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
<!--<rabbit:listener ref="ackListener" queue-names="test_queue_confirm"/>-->
<!--<rabbit:listener ref="qosListener" queue-names="test_queue_confirm"/>-->
<!--定义监听器,监听正常队列-->
<!--<rabbit:listener ref="dlxListener" queue-names="test_queue_dlx"/>-->
<rabbit:listener ref="orderListener" queue-names="order_queue_dlx"/>
</rabbit:listener-container>
运行消费者测试类
订单信息:订单Id=1,time=20220314140456533
处理业务逻辑...
根据订单id查询其状态...
判断状态是否为支付成功
取消订单,回滚库存....
订单信息:订单Id=2,time=20220314140456728
处理业务逻辑...
根据订单id查询其状态...
判断状态是否为支付成功
取消订单,回滚库存....
订单信息:订单Id=3,time=20220314140456730
处理业务逻辑...
根据订单id查询其状态...
判断状态是否为支付成功
取消订单,回滚库存....
订单信息:订单Id=4,time=20220314140456730
处理业务逻辑...
根据订单id查询其状态...
判断状态是否为支付成功
取消订单,回滚库存....
订单信息:订单Id=5,time=20220314140456732
处理业务逻辑...
根据订单id查询其状态...
判断状态是否为支付成功
取消订单,回滚库存....
八、SpringBoot 整合 RabbitMQ
(一) 搭建生产者工程
实现步骤:
- 创建生产者SpringBoot工程
- 引入依赖坐标
- 编写yml配置,基本信息配置
- 定义交换机,队列以及绑定关系的配置类
- 注入RabbitTemplate,调用方法,完成消息发送
1、创建工程
项目名字 :producer-springboot
2、pom.xml
(1) 父工程 rabbitmq 的 pom.xml
<!--父工程依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
(2) producer-springboot 的 pom.xml
<dependencies>
<!--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>
</dependency>
</dependencies>
3、配置整合 application.yml
spring:
rabbitmq:
host: 192.168.200.188
port: 5672
username: admin
password: 123456
virtual-host: /
4、创建配置类
com.atguigu.config.RabbitMqConfig
@Configuration
public class RabbitMqConfig {
public static final String EXCHANGE_NAME = "boot_topic_exchange";
public static final String QUEUE_NAME = "boot_queue";
/**
* 1.交换机
*
* @return
*/
@Bean("bootExchange")
public Exchange bootExchange() {
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
/**
* 2.Queue 队列
*
* @return
*/
@Bean("bootQueue")
public Queue bootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
}
/**
* 3. 队列和交互机绑定关系 Binding
*
* @param queue 知道哪个队列
* @param exchange 知道哪个交换机
* routing key
* noargs():表示不指定参数
* @return
*/
@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
@Qualifier("bootExchange") Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}
5、创建启动类
com.atguigu.ProducerApplication
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
}
}
6、发送消息
创建测试文件 com.atguigu.ProducerTest.java
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 第一个参数:交换机名字
* 第二个参数:routingKey
* 第三个参数:发送的消息
*/
@Test
public void testSend() {
rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "boot.haha", "mq hello");
}
}
运行测试类,发送消息
(二) 搭建消费者工程
实现步骤
- 创建消费者 SpringBoot 工程
- 引入 start 依赖坐标
- 编写 yml 配置,基本信息配置
- 定义监听类,使用 @RabbitListener 注解完成队列监听
1、创建工程
创建项目 consumber-springboot
2、pom.xml
<dependencies>
<!--RabbitMQ 启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
3、配置整合 application.yml
spring:
rabbitmq:
host: 192.168.200.188
port: 5672
username: admin
password: 123456
virtual-host: /
4、消息监听器
队列监听器
创建 com.atguigu.listener.RabbitMqListener
@Component
public class RabbitMqListener {
@RabbitListener(queues = "boot_queue")
public void listenerQueue(Message message) {
System.out.println(new String(message.getBody()));
System.out.println("hello world");
}
}
5、创建启动类
com.atguigu.ConsumerApplication
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
(三) 小结
- SpringBoot 提供了快速整合 RabbitMQ 的方式
- 基本信息在 yml 中配置,队列交互机以及绑定关系在配置类中使用 Bean 的方式配置
- 生产端直接注入 RabbitTemplate 完成消息发送
- 消费端直接使用 @RabbitListener 完成消息接收
九、消息百分百成功投递
谈到消息的可靠性投递,无法避免的,在实际的工作中会经常碰到,比如一些核心业务需要保障消息不丢失,接下来我们看一个可靠性投递的流程图,说明可靠性投递的概念:
Step 1: 首先把消息信息(业务数据)存储到数据库中,紧接着,我们再把这个消息记录也存储到一张消息记录表里(或者另外一个同源数据库的消息记录表)
Step 2:发送消息到 MQ Broker 节点(采用 Confirm 方式发送,会有异步的返回结果)
Step 3、4:生产者端接受 MQ Broker 节点返回的 Confirm 确认消息结果,然后进行更新消息记录表里的消息状态。比如默认Status = 0 当收到消息确认成功后,更新为1即可!
Step 5:但是在消息确认这个过程中可能由于网络闪断、MQ Broker 端异常等原因导致回送消息失败或者异常。这个时候就需要发送方(生产者)对消息进行可靠性投递了,保障消息不丢失,100%的投递成功!(有一种极限情况是闪断,Broker 返回的成功确认消息,但是生产端由于网络闪断没收到,这个时候重新投递可能会造成消息重复,需要消费端去做幂等处理)所以我们需要有一个定时任务,(比如每5分钟拉取一下处于中间状态的消息,当然这个消息可以设置一个超时时间,比如超过1分钟 Status = 0 ,也就说明了1分钟这个时间窗口内,我们的消息没有被确认,那么会被定时任务拉取出来)
Step 6:接下来我们把中间状态的消息进行重新投递 retry send,继续发送消息到MQ ,当然也可能有多种原因导致发送失败
Step 7:我们可以采用设置最大努力尝试次数,比如投递了3次,还是失败,那么我们可以将最终状态设置为Status = 2 ,最后交由人工解决处理此类问题(或者把消息转储到失败表中)
数据库文件
-- ----------------------------
-- Table structure for broker_message_log
-- ----------------------------
DROP TABLE IF EXISTS `broker_message_log`;
CREATE TABLE `broker_message_log` (
`message_id` varchar(255) NOT NULL COMMENT '消息唯一ID',
`message` varchar(4000) NOT NULL COMMENT '消息内容',
`try_count` int(4) DEFAULT '0' COMMENT '重试次数',
`status` varchar(10) DEFAULT '' COMMENT '消息投递状态 0投递中,1投递成功,2投递失败',
`next_retry` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '下一次重试时间',
`create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for t_order
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`message_id` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2018091102 DEFAULT CHARSET=utf8;
十、消息幂等性保障
幂等性指一次和多次请求某一个资源,对于资源本身应该具有同样的结果。也就是说,其任意多次执行对资源本身所产生的影响均与一次执行的影响相同。
在MQ中指,消费多条相同的消息,得到与消费该消息一次相同的结果
消息幂等性保障 乐观锁机制
生产者发送消息:
id=1,money=500,version=1
消费者接收消息:
id=1,money=500,version=1
id=1,money=500,version=1
消费者需要保证幂等性:第一次执行SQL语句
#第一次执行:version=1
update account set money = money - 500 , version = version + 1
where id = 1 and version = 1
消费者需要保证幂等性:第二次执行SQL语句
#第二次执行:version=2
update account set money = money - 500 , version = version + 1
where id = 1 and version = 1