目录
MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发.
RabbitMQ官方地址:http://www.rabbitmq.com/
1)任务异步处理
2)应用程序解耦合

Broker:消息队列服务进程,此进程包括两个部分:Exchange和Queue。
Exchange:消息队列交换机,按一定的规则将消息路由转发到某个队列,对消息进行过虑。
Queue:消息队列,存储消息的队列,消息到达队列并转发给指定的消费方。
Producer:消息生产者,即生产方客户端,生产方客户端将消息发送到MQ。
Consumer:消息消费者,即消费方客户端,接收MQ转发的消息。
发送消息:
1、生产者和Broker建立TCP连接。
2、生产者和Broker建立通道。
3、生产者通过通道消息发送给Broker,由Exchange将消息进行转发。
4、Exchange将消息转发到指定的Queue(队列)
接收消息:
1、消费者和Broker建立TCP连接
2、消费者和Broker建立通道
3、消费者监听指定的Queue(队列)
4、当有消息到达Queue时Broker默认将消息推送给消费者。
5、消费者接收到消息
RabbitMQ的下载地址:http://www.rabbitmq.com/download.html
本项目使用Erlang/OTP 20.3版本和RabbitMQ3.7.3版本。
下载erlang: http://erlang.org/download/otp_win64_20.3.exe
erlang安装完成需要配置erlang环境变量: ERLANG_HOME=D:\Program Files\erl9.3 在path中 添加%ERLANG_HOME%\bin;
安装erlang直接下一步就好
安装RabbitMQ: Release RabbitMQ 3.7.3 · rabbitmq/rabbitmq-server · GitHub
安装完开始菜单会有显示:

RabbitMQ Service-install :安装服务
RabbitMQ Service-remove 删除服务
RabbitMQ Service-start 启动
RabbitMQ Service-stop 启动
如果没有开始菜单则进入安装目录下sbin目录手动启动:

1)安装并运行服务
rabbitmq-service.bat install 安装服务 rabbitmq-service.bat stop 停止服务 rabbitmq-service.bat start 启动服务
2)安装管理插件
安装rabbitMQ的管理插件,方便在浏览器端管理RabbitMQ
管理员身份运行 rabbitmq-plugins.bat enable rabbitmq_management
3、启动成功 登录RabbitMQ
进入浏览器,输入:http://localhost:15672
初始账号和密码:guest/guest


注:每个虚拟机就相当于一个独立的MQ,默认虚拟机的名字为/
4、如果启动失败找到.erlang.cookie,位于C:\windows\system32\config\systemprofile下,将此处的.erlang.cookie覆盖C:\user\admin.erlang.cookie后重启RabbitMQ即可解决问题
注意事项
1、安装erlang和rabbitMQ以管理员身份运行。
2、当卸载重新安装时会出现RabbitMQ服务注册失败,此时需要进入注册表清理erlang搜索RabbitMQ、ErlSrv,将对应的项全部删除。
创建生产者工程和消费者工程,分别加入RabbitMQ java client的依赖。
test-rabbitmq-producer:生产者工程
test-rabbitmq-consumer:消费者工程
com.rabbitmq
amqp‐client
4.0.3
org.springframework.boot
spring‐boot‐starter‐logging

- public class Producer01 {
- //队列名称
- private static final String QUEUE = "helloworld";
-
- public static void main(String[] args) throws IOException, TimeoutException {
- Connection connection = null;
- Channel channel = null;
- try {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- factory.setPort(5672);
- factory.setUsername("guest");
- factory.setPassword("guest");
- factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
- //创建与RabbitMQ服务的TCP连接
- connection = factory.newConnection();
- //创建与Exchange的通道,每个连接可以创建多个通道,每个通道代表一个会话任务
- channel = connection.createChannel();
-
- /**
- * 声明队列,如果Rabbit中没有此队列将自动创建
- * param1:队列名称
- * param2:durable是否持久化,如果持久化,mq重启后队列还在
- * param3:exclusive队列是否独占此连接,队列只允许在该连接中访问,如果连接关闭队列自动删除,如果将此参数设置为true可用于临时队列的创建
- * param4:autoDelete队列不再使用时是否自动删除此队列和exclusive搭配使用
- * param5:队列参数(可以设置队列的扩展参数)
- */
- channel.queueDeclare(QUEUE, true, false, false, null);
- String message = "helloworld小明" + System.currentTimeMillis();
- /**
- * 消息发布方法
- * param1:Exchange的名称,如果没有指定,则使用Default Exchange
- * param2:routingKey,消息的路由Key,是用于Exchange(交换机)将消息转发到指定的消息队列,如果使用默认交换机,routingKey设置为队列的名称
- * param3:消息包含的属性
- * param4:消息体
- */
-
- /**
- * 这里没有指定交换机,消息将发送给默认交换机,每个队列也会绑定那个默认的交换机,但是不能显
- 示绑定或解除绑定
- * 默认的交换机,routingKey等于队列名称
- */
- channel.basicPublish("", QUEUE, null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- if (channel != null) {
- channel.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- }
- }
此时显示创建了一个队列,有1条消息待消费, 消息总数为1


注:队列中是可以拿到发送的消息的

注:Purge Message可以清空该队列的消息

注:Publish message可以在指定的队列中发消息,因此生产者发送消息时要打印发送的body,消费者端做幂等,一旦出现问题可通过控制台重新发送该消息

注:两边都要声明队列,防止消费者启动在生产者之前报错
- public class Consumer01 {
- private static final String QUEUE = "helloworld";
-
- public static void main(String[] args) throws IOException, TimeoutException {
- ConnectionFactory factory = new ConnectionFactory();
- //设置MabbitMQ所在服务器的ip和端口
- factory.setHost("127.0.0.1");
- factory.setPort(5672);
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- //声明队列
- channel.queueDeclare(QUEUE, true, false, false, null);
- //定义消费方法
- DefaultConsumer consumer = new DefaultConsumer(channel) {
- /**
- * 消费者接收消息调用此方法
- * @param consumerTag 消费者的标签,在channel.basicConsume()去指定
- * @param envelope 消息包的内容,可从中获取消息id,消息routingkey,交换机,消息和重传标志
- (收到消息失败后是否需要重新发送)
- * @param properties
- * @param body
- * @throws IOException
- }
- */
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
- //交换机
- String exchange = envelope.getExchange();
- //路由key
- String routingKey = envelope.getRoutingKey();
- //消息id
- long deliveryTag = envelope.getDeliveryTag();
- //消息内容
- String msg = new String(body);
- System.out.println("receive message.." + msg);
- }
- };
- /**
- * 监听队列String queue, boolean autoAck,Consumer callback
- * 参数明细
- * 1、队列名称
- * 2、是否自动回复,设置为true为表示消息接收到自动向mq回复接收到了,mq接收到回复会删除消息,设置
- 为false则需要手动回复
- * 3、消费消息的方法,消费者接收到消息后调用此方法
- */
- channel.basicConsume(QUEUE, true, consumer);
- }
- }
RabbitMQ有以下几种工作模式 :
1、Work queues
2、Publish/Subscribe
3、Routing
4、Topics
5、Header
6、RPC
工作队列模式

work queues与入门程序相比:多了一个消费端,两个消费端共同消费同一个队列中的消息。
应用场景:对于 任务过重或任务较多情况使用工作队列可以提高任务处理的速度。
测试:
1、使用入门程序,启动多个消费者。
2、生产者发送多个消息。
结果:
1、一条消息只会被一个消费者接收;
2、rabbit采用轮询的方式将消息是平均发送给消费者的;
3、消费者在处理完某条消息后,才会收到下一条消息。
设置idea同时启用多个客户端

发布订阅模式

发布订阅模式:
1、每个消费者监听自己的队列。
2、生产者将消息发给broker,由交换机将消息转发到绑定此交换机的每个队列,每个绑定交换机的队列都将接收到消息
案例:
用户通知,当用户充值成功或转账完成系统通知用户,通知方式有短信、邮件多种方法 。
1、声明exchange_fanout_inform交换机。
2、声明两个队列并且绑定到此交换机,绑定时不需要指定routingkey
3、发送消息时不需要指定routingkey
- public class Producer02_publish {
- private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
- private static final String QUEUE_INFORM_SMS = "queue_inform_sms";
- private static final String EXCHANGE_FANOUT_INFORM = "exchange_fanout_inform";
-
- public static void main(String[] args) {
- Connection connection = null;
- Channel channel = null;
- try {
- //创建一个与MQ的连接
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("127.0.0.1");
- factory.setPort(5672);
- factory.setUsername("guest");
- factory.setPassword("guest");
- factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
- //创建一个连接
- connection = factory.newConnection();
- //创建与交换机的通道,每个通道代表一个会话
- channel = connection.createChannel();
- //声明交换机 String exchange, BuiltinExchangeType type
- /**
- * 参数明细
- * 1、交换机名称
- * 2、交换机类型,fanout、topic、direct、headers
- */
- channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM, "fanout");
- //声明队列
- // (String queue, boolean durable, boolean exclusive, boolean autoDelete, Map
arguments) - /**
- * 参数明细:
- * 1、队列名称
- * 2、是否持久化
- * 3、是否独占此队列
- * 4、队列不用是否自动删除
- * 5、参数
- */
- channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
- channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
- //交换机和队列绑定String queue, String exchange, String routingKey
- /**
- * 参数明细
- * 1、队列名称
- * 2、交换机名称
- * 3、路由key
- */
- channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_FANOUT_INFORM, "");
- channel.queueBind(QUEUE_INFORM_SMS, EXCHANGE_FANOUT_INFORM, "");
- //发送消息
- for (int i = 0; i < 10; i++) {
- String message = "inform to user" + i;
- //向交换机发送消息 String exchange, String routingKey, BasicProperties props, byte[] body
- /**
- * 参数明细
- * 1、交换机名称,不指令使用默认交换机名称 Default Exchange
- * 2、routingKey(路由key),根据key名称将消息转发到具体的队列,这里填写队列名称表示消息将发到此队列
- * 3、消息属性
- * 4、消息内容
- */
- channel.basicPublish(EXCHANGE_FANOUT_INFORM, "", null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- } finally {
- if (channel != null) {
- try {
- channel.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
可以看到新声明的交换机exchange_fanout_inform

新生成两个队列,每个队列接收到10条消息

邮件发送消费者
- public class Consumer02_subscribe_email {
- //队列名称
- private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
- private static final String EXCHANGE_FANOUT_INFORM = "inform_exchange_fanout";
-
- public static void main(String[] args) throws IOException, TimeoutException {
- //创建一个与MQ的连接
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("127.0.0.1");
- factory.setPort(5672);
- factory.setUsername("guest");
- factory.setPassword("guest");
- factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
- //创建一个连接
- Connection connection = factory.newConnection();
- //创建与交换机的通道,每个通道代表一个会话
- Channel channel = connection.createChannel();
- //声明交换机 String exchange, BuiltinExchangeType type
- /**
- * 参数明细
- * 1、交换机名称
- * 2、交换机类型,fanout、topic、direct、headers
- */
- channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM, "fanout");
- //声明队列
- // channel.queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map
arguments) - /**
- * 参数明细:
- * 1、队列名称
- * 2、是否持久化
- * 3、是否独占此队列
- * 4、队列不用是否自动删除
- * 5、参数
- */
- channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
- //交换机和队列绑定String queue, String exchange, String routingKey
- /**
- * 参数明细
- * 1、队列名称
- * 2、交换机名称
- * 3、路由key
- */
- channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_FANOUT_INFORM, "");
- //定义消费方法
- DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope,
- AMQP.BasicProperties properties, byte[] body) throws IOException {
- long deliveryTag = envelope.getDeliveryTag();
- String exchange = envelope.getExchange();
- //消息内容
- String message = new String(body);
- System.out.println(message);
- }
- };
- /**
- * 监听队列String queue, boolean autoAck,Consumer callback
- * 参数明细
- * 1、队列名称
- * 2、是否自动回复,设置为true为表示消息接收到自动向mq回复接收到了,mq接收到回复会删除消息,设置
- 为false则需要手动回复
- * 3、消费消息的方法,消费者接收到消息后调用此方法
- */
- channel.basicConsume(QUEUE_INFORM_EMAIL, true, defaultConsumer);
- }
-
- }
可以看到邮件的信息被消费了,短信的还在

可以看到该交换机绑定了两个队列


路由模式:
1、每个消费者监听自己的队列,并且设置routingkey。
2、生产者将消息发给交换机,由交换机根据routingkey来转发消息到指定的队列。
1、声明exchange_routing_inform交换机。
2、声明两个队列并且绑定到此交换机,绑定时需要指定routingkey
3、发送消息时需要指定routingkey
- public class Producer03_routing {
- //队列名称
- private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
- private static final String QUEUE_INFORM_SMS = "queue_inform_sms";
- private static final String EXCHANGE_ROUTING_INFORM = "exchange_routing_inform";
-
- public static void main(String[] args) {
- Connection connection = null;
- Channel channel = null;
- try {
- //创建一个与MQ的连接
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("127.0.0.1");
- factory.setPort(5672);
- factory.setUsername("guest");
- factory.setPassword("guest");
- factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
- //创建一个连接
- connection = factory.newConnection();
- //创建与交换机的通道,每个通道代表一个会话
- channel = connection.createChannel();
- //声明交换机 String exchange, BuiltinExchangeType type
- /**
- * 参数明细
- * 1、交换机名称
- * 2、交换机类型,fanout、topic、direct、headers
- */
- channel.exchangeDeclare(EXCHANGE_ROUTING_INFORM, "direct");
- //声明队列
- // channel.queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map
arguments) - /**
- * 参数明细:
- * 1、队列名称
- * 2、是否持久化
- * 3、是否独占此队列
- * 4、队列不用是否自动删除
- * 5、参数
- */
- channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
- channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
- //交换机和队列绑定String queue, String exchange, String routingKey
- /**
- * 参数明细
- * 1、队列名称
- * 2、交换机名称
- * 3、路由key
- */
- channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_EMAIL);
- channel.queueBind(QUEUE_INFORM_SMS, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_SMS);
- //发送邮件消息
- for (int i = 0; i < 10; i++) {
- String message = "email inform to user" + i;
- //向交换机发送消息 String exchange, String routingKey, BasicProperties props, byte[] body
- /**
- * 参数明细
- * 1、交换机名称,不指令使用默认交换机名称 Default Exchange
- * 2、routingKey(路由key),根据key名称将消息转发到具体的队列,这里填写队列名称表示消
- 息将发到此队列
- * 3、消息属性
- * 4、消息内容
- */
- channel.basicPublish(EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_EMAIL, null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- }
- //发送短信消息
- for (int i = 0; i < 10; i++) {
- String message = "sms inform to user" + i;
- //向交换机发送消息 String exchange, String routingKey, BasicProperties props, byte[] body
- channel.basicPublish(EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_SMS, null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- } finally {
- if (channel != null) {
- try {
- channel.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
可以看到创建了exchange_routing_inform交换机,并绑定了两个队列

可以看到对列绑定了Routing_key
消费者邮件发送消费者
- public class Consumer03_routing_email {
- //队列名称
- private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
- private static final String EXCHANGE_ROUTING_INFORM = "exchange_routing_inform";
-
- public static void main(String[] args) throws IOException, TimeoutException {
- //创建一个与MQ的连接
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("127.0.0.1");
- factory.setPort(5672);
- factory.setUsername("guest");
- factory.setPassword("guest");
- factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
- //创建一个连接
- Connection connection = factory.newConnection();
- //创建与交换机的通道,每个通道代表一个会话
- Channel channel = connection.createChannel();
- //声明交换机 String exchange, BuiltinExchangeType type
- /**
- * 参数明细
- * 1、交换机名称
- * 2、交换机类型,fanout、topic、direct、headers
- */
- channel.exchangeDeclare(EXCHANGE_ROUTING_INFORM, "direct");
- //声明队列
- // channel.queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map
arguments) - /**
- * 参数明细:
- * 1、队列名称
- * 2、是否持久化
- * 3、是否独占此队列
- * 4、队列不用是否自动删除
- * 5、参数
- */
- channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
- //交换机和队列绑定String queue, String exchange, String routingKey
- /**
- * 参数明细
- * 1、队列名称
- * 2、交换机名称
- * 3、路由key
- */
- channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_EMAIL);
- //定义消费方法
- DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
- long deliveryTag = envelope.getDeliveryTag();
- String exchange = envelope.getExchange();
- //消息内容
- String message = new String(body);
- System.out.println(message);
- }
- };
- /**
- * 监听队列String queue, boolean autoAck,Consumer callback
- * 参数明细
- * 1、队列名称
- * 2、是否自动回复,设置为true为表示消息接收到自动向mq回复接收到了,mq接收到回复会删除消息,设置
- 为false则需要手动回复
- * 3、消费消息的方法,消费者接收到消息后调用此方法
- */
- channel.basicConsume(QUEUE_INFORM_EMAIL, true, defaultConsumer);
- }
-
- }
1、Routing模式和Publish/subscibe有啥区别?
Routing模式要求队列在绑定交换机时要指定routingkey,消息会转发到符合routingkey的队列。

路由模式:
1、每个消费者监听自己的队列,并且设置带统配符的routingkey。
2、生产者将消息发给broker,由交换机根据routingkey来转发消息到指定的队列。
案例:
根据用户的通知设置去通知用户,设置接收Email的用户只接收Email,设置接收sms的用户只接收sms,设置两种通知类型都接收的则两种通知都有效。
- public class Producer04_topics {
- //队列名称
- private static final String QUEUE_INFORM_EMAIL = "queue_inform_email_topics";
- private static final String QUEUE_INFORM_SMS = "queue_inform_sms_topics";
- private static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
-
- public static void main(String[] args) {
- Connection connection = null;
- Channel channel = null;
- try {
- //创建一个与MQ的连接
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("127.0.0.1");
- factory.setPort(5672);
- factory.setUsername("guest");
- factory.setPassword("guest");
- factory.setVirtualHost("/");//rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
- //创建一个连接
- connection = factory.newConnection();
- //创建与交换机的通道,每个通道代表一个会话
- channel = connection.createChannel();
- //声明交换机 String exchange, BuiltinExchangeType type
- /**
- * 参数明细
- * 1、交换机名称
- * 2、交换机类型,fanout、topic、direct、headers
- */
- channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, "topic");
- //声明队列
- /**
- * 参数明细:
- * 1、队列名称
- * 2、是否持久化
- * 3、是否独占此队列
- * 4、队列不用是否自动删除
- * 5、参数
- */
- channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
- channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
-
- //绑定email通知队列
- channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_TOPICS_INFORM,"inform.#.email.#");
- //绑定sms通知队列
- channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_TOPICS_INFORM,"inform.#.sms.#");
-
- //发送邮件消息
- for (int i = 0; i < 10; i++) {
- String message = "email inform to user" + i;
- //向交换机发送消息 String exchange, String routingKey, BasicProperties props, byte[] body
- /**
- * 参数明细
- * 1、交换机名称,不指令使用默认交换机名称 Default Exchange
- * 2、routingKey(路由key),根据key名称将消息转发到具体的队列,这里填写队列名称表示消
- 息将发到此队列
- * 3、消息属性
- * 4、消息内容
- */
- channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.email", null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- }
- //发送短信消息
- for (int i = 0; i < 10; i++) {
- String message = "sms inform to user" + i;
- channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.sms", null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- }
- //发送短信和邮件消息
- for (int i = 0; i < 10; i++) {
- String message = "sms and email inform to user" + i;
- channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.sms.email", null, message.getBytes());
- System.out.println("Send Message is:'" + message + "'");
- }
- } catch (IOException | TimeoutException e) {
- e.printStackTrace();
- } finally {
- if (channel != null) {
- try {
- channel.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- }
可以看到exchange_topics_inform绑定了两个Routing_key,进行通配符匹配

统配符规则:
中间以“.”分隔。
符号#可以匹配多个词,符号*可以匹配一个词语。
对列绑定的Routing_key为:
inform.#.email.#
inform.#.sms.#
发送消息指定的Routing_key为:
inform.email 匹配 inform.#.email.#
inform.sms 匹配 inform.#.sms.#
inform.sms.email 匹配 inform.#.email.# 和 inform.#.sms.# 两个队列都能发送
(即使什么都没有,#也可以匹配个空)
header模式与routing不同的地方在于,header模式取消routingkey,使用header中的 key/value(键值对)匹配队列。
案例:
根据用户的通知设置去通知用户,设置接收Email的用户只接收Email,设置接收sms的用户只接收sms,设置两种通知类型都接收的则两种通知都有效
- Map
headers_email = new Hashtable(); - headers_email.put("inform_type", "email");
- Map
headers_sms = new Hashtable(); - headers_sms.put("inform_type", "sms");
- channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_HEADERS_INFORM,"",headers_email);
- channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_HEADERS_INFORM,"",headers_sms);
通知
- String message = "email inform to user"+i;
- Map
headers = new Hashtable(); - headers.put("inform_type", "email");//匹配email通知消费者绑定的header
- //headers.put("inform_type", "sms");//匹配sms通知消费者绑定的header
- AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
- properties.headers(headers);
- //Email通知
- channel.basicPublish(EXCHANGE_HEADERS_INFORM, "", properties.build(), message.getBytes());
发送邮件消费者
- channel.exchangeDeclare(EXCHANGE_HEADERS_INFORM, BuiltinExchangeType.HEADERS);
- Map
headers_email = new Hashtable(); - headers_email.put("inform_email", "email");
- //交换机和队列绑定
- channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_HEADERS_INFORM,"",headers_email);
- //指定消费队列
- channel.basicConsume(QUEUE_INFORM_EMAIL, true, consumer);

RPC即客户端远程调用服务端的方法 ,使用MQ可以实现RPC的异步调用,基于Direct交换机实现,流程如下:
1、客户端即是生产者就是消费者,向RPC请求队列发送RPC调用消息,同时监听RPC响应队列。
2、服务端监听RPC请求队列的消息,收到消息后执行服务端的方法,得到方法返回的结果
3、服务端将RPC方法 的结果发送到RPC响应队列
4、客户端(RPC调用方)监听RPC响应队列,接收到RPC调用结果。
-
org.springframework.boot -
spring-boot-starter-amqp -
3.3.0
- server:
- port: 44000
- spring:
- application:
- name: test-SpringBoot-RabbitMQ-product
- rabbitmq:
- host: 127.0.0.1
- port: 5672
- username: guest
- password: guest
- virtualHost: /
- @Configuration
- public class RabbitmqConfig {
- public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
- public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
- public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
-
- /**
- * 交换机配置
- * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
- *
- * @return the exchange
- */
- @Bean(EXCHANGE_TOPICS_INFORM)
- public Exchange EXCHANGE_TOPICS_INFORM() {
- //durable(true)持久化,消息队列重启后交换机仍然存在
- return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
- }
-
- //声明队列
- @Bean(QUEUE_INFORM_SMS)
- public Queue QUEUE_INFORM_SMS() {
- Queue queue = new Queue(QUEUE_INFORM_SMS);
- return queue;
- }
-
- //声明队列
- @Bean(QUEUE_INFORM_EMAIL)
- public Queue QUEUE_INFORM_EMAIL() {
- Queue queue = new Queue(QUEUE_INFORM_EMAIL);
- return queue;
- }
-
- /**
- * channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
- * 绑定队列到交换机 .
- *
- * @param queue the queue
- * @param exchange the exchange
- * @return the binding
- */
- @Bean
- public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
- @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
- return BindingBuilder.bind(queue).to(exchange).with("inform.#.sms.#").noargs();
- }
-
- @Bean
- public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
- @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
- return BindingBuilder.bind(queue).to(exchange).with("inform.#.email.#").noargs();
- }
- }
- @Test
- public void testSendByTopics(){
- for (int i=0;i<5;i++){
- String message = "sms email inform to user"+i;
- rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM,"inform.sms.email",message);
- System.out.println("Send Message is:'" + message + "'");
- }
- }

- @Component
- public class ReceiveHandler {
- public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
- public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
- public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
- //监听email队列
- @RabbitListener(queues = {ReceiveHandler.QUEUE_INFORM_EMAIL})
- public void receive_email(String msg, Message message, Channel channel){
- System.out.println(msg);
- }
- //监听sms队列
- @RabbitListener(queues = {ReceiveHandler.QUEUE_INFORM_SMS})
- public void receive_sms(String msg,Message message,Channel channel){
- System.out.println(msg);
- }
- }
1)通过@RabbitListener注解启动消费端也可以声明队列,交换机,路由信息...等等,如下:
2)尽量不要这么写,如果前置的绑定关系已经创建,只需要 @RabbitListener(queues = {ReceiveHandler.QUEUE_INFORM_SMS})用这种写法(推荐)
3)甚至直接在rabbitMQ图形化界面创建
- @Component
- public class ReceiveHandlerV2 {
- public static final String ROUTING_KEY = "order";
- public static final String QUEUE_NAME = "queue.order";
- public static final String EXCHANGE_DIRECT = "exchange.direct.order";
-
-
- //监听email队列
- @RabbitListener(bindings = @QueueBinding(
- value = @Queue(value = QUEUE_NAME, durable = "true"),
- exchange = @Exchange(value = EXCHANGE_DIRECT),
- key = {ROUTING_KEY}
- ))
- public void processMessage(String msg, Message message, Channel channel){
- System.out.println(msg);
- }
- }

1)消息没有发送到消息对列上
2)消息成功存入消息队列,但是消息队列服务器宕机,原本保存在内存中的消息丢失
3)消息成功存入消息队列,但是消费端服务宕机,抛异常等等...
后果:消费者拿不到消息,业务功能缺失,数据错误
1) 消息没有发送到消息队列:
2) 宕机导致内存中消息丢失
3) 但是消费端服务宕机,抛异常
注意:必须加如下两个配置,否则不生效

创建配置类:Why?首先我们需要声明一个回调函数来接受RabbitMQ服务器返回的确认信息
| 方法名 | 方法功能 | 所属接口 | 接口所属类 |
| confirm() | 确认消息是否发送到交换机 | ConfirmCallback | RabbitTemplate |
| returnedMessage() | 确认消息是否发送到对列 | ReturnsCallback | RabbitTemplate |
| 调用组件的方法 | 所需对象类型 |
| setConfirmCallback() | ConfirmCallback接口类型 |
| setReturnsCallback() | ReturnsCallback接口类型 |

配置类信息代码,如下:
- package org.example.testspringbootrabbitmqproduct;
-
- import jakarta.annotation.PostConstruct;
- import org.springframework.amqp.core.ReturnedMessage;
- import org.springframework.amqp.rabbit.connection.CorrelationData;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class RabbitConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback{
-
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- /**
- * 只放在IOC中是不够的,需要设置在RabbitTemplate中才可以
- */
- @PostConstruct
- public void initRabbitTemplate() {
- rabbitTemplate.setConfirmCallback(this);
- rabbitTemplate.setReturnsCallback(this);
- }
-
- /**
- * @param correlationData correlation data for the callback.
- * @param ack true for ack, false for nack
- * @param cause An optional cause, for nack, when available, otherwise null.
- */
- @Override
- public void confirm(CorrelationData correlationData, boolean ack, String cause) {
- // 消息发送到交换机成功或者失败都会回调这个方法
- System.out.println(correlationData);//成功后返回 null
- System.out.println(ack);//成功后返回 true
- System.out.println(cause);//成功后返回 null
- }
-
- /**
- * @param returnedMessage the returned message and metadata.
- */
- @Override
- public void returnedMessage(ReturnedMessage returnedMessage) {
- // 只有发送到队列上失败才会回调该方法
- System.out.println("消息主体: " + new String(returnedMessage.getMessage().getBody()));
- System.out.println("应答码: " + returnedMessage.getReplyCode());
- System.out.println("描述: " + returnedMessage.getReplyText());
- System.out.println("消息使用的交换器是: " + returnedMessage.getExchange());
- System.out.println("消息使用的路由键是: " + returnedMessage.getRoutingKey());
-
- // 消息主体: 11
- // 应答码: 312
- // 描述: NO_ROUTE
- // 消息使用的交换器是: exchange.direct.order
- // 消息使用的路由键是: inform.sms.email1111
- }
- }
原理图

注意:备份交换机的类型必须是fanout(因为是不带路由键的)

注意:备份交换机只能是创建的时候指定

模拟发送一个不存在的routingKey,查看备份队列已经有一条消息


代码中开启机制的支持

思考:更新购物车的微服务消费了消息返回的ACK确认信息,然后Broker删除了信息,进而导致更新库存,更新积分的功能拿不到消息----这种情况会发生吗?

答案:会的,因此需要广播到不同的队列中
- package org.example.testspringbootrabbitmqconsumer;
-
- import com.rabbitmq.client.Channel;
- import org.springframework.amqp.core.Message;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- import java.io.IOException;
-
- @Component
- public class ReceiveHandlerV2 {
- public static final String ROUTING_KEY = "order";
- public static final String QUEUE_NAME = "queue.order";
- public static final String EXCHANGE_DIRECT = "exchange.direct.order";
-
-
- //监听队列
- @RabbitListener(queues = {QUEUE_NAME})
- public void processMessage(String msg, Message message, Channel channel) throws IOException {
- try {
- // 成功了返回ACK
- System.out.println("消费端接收到了消息: " + msg);
- /**
- * deliveryTag: 交付标签(对列中每条消息的唯一标识)是以为64位整数
- * 会从生产者带过来
- * 提问: 如果交换机是Fanout模式,同一个消息广播到不同对列,deliveryTag会重复吗?
- * 不会,deliveryTag在Broker范围内唯一
- * multiple: 为true时 指定某个deliveryTag,为ACK时全部删除,为NACK时全部重新投递
- * 为false时,只做单个的处理
- */
- channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
- } catch (Exception e) {
- // 失败了返回NACK
- // 获取当前消息是否是重复投递
- Boolean redelivered = message.getMessageProperties().getRedelivered();
- if (redelivered) {
- // 就不放回对列了,说明此前重复过了
- channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
- } else {
- /**
- * requeue: 控制消息是否重新放回队列 true:重新放回,进行投递 false:不重新投递, broker会丢弃
- */
- channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
- }
- throw new RuntimeException(e);
- }
- }
- }
发送一条消息未执行basicAck()时,队列中消息还再存在


执行完basicAck()后队列中消息消失


第一次是false

第二次是true

速率显示重新投递

分别向两个队列添加100条信息

强制让一个队列进行NACK
启动消费者 ,可以看到NACK的队列一直处于Runing状态中,什么都不做的队列消费完了

结论: 配置文件配置的 acknowledge-mode: manual #把消息确认模式改为手动确认,只是起一个开启功能的作用,并不是每个队列都需要手动ack,如果不写手动ack的代码会自动进行ack
// 表示拒绝, 和basicNack基本一样,区别是 是否能控制批量操作 channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
三个部分的操作相互配合总体上实现消息的可靠性投递