RabbitMQ 基础

一、MQ 介绍

1.1、概述

MQ 全称 Message Queue(消息队列),是在消息的传输过程中保存消息的容器。多用于分布式系统之间进行通信。

分布式系统通信两种方式:直接远程调用 和 借助第三方 完成间接通信。发送方称为生产者,接收方称为消费者。
mq-1mq-2

1.2、优势

应用解耦: 提高系统容错性和可维护性。
应用解耦-1
系统的耦合性越高,容错性就越低,可维护性就越低。
应用解耦-2
使用 MQ 让应用间解耦,提升容错性和可维护性。

异步提速: 提升用户体验和系统吞吐量。
异步提速1
一个下单操作耗时:20+300+300+300=920ms,用户点击完下单按钮后需要等待 920ms 才能得到下单响应,太慢!
异步提速-2.png
用户点击完下单按钮后,只需等待 20+5=25ms 就能得到下单响应。提升用户体验和系统吞吐量(单位时间内处理请求的数目)。

削峰填谷: 提高系统稳定性。
在这里插入图片描述
削峰填谷-2
削峰填谷-3使用 MQ 之后,限制消费消息的速度为 1000 ,这样一来,高峰期产生的数据势必会被积压在 MQ 中,高峰就被 “” 掉了,但是因为消息积压,在高峰期过后的一段时间内,消费消息的速度还是会维持在 1000 ,直到消费完积压的消息,这就叫做 “填谷”。可以提高系统稳定性。

1.3、劣势

在这里插入图片描述

  • 系统可用性降低
    系统引入的外部依赖越多,系统稳定性越差。一旦 MQ 宕机,就会对业务造成影响。如何保证 MQ 的高可用?

  • 系统复杂度提高
    MQ 的加入大大增加了系统的复杂度,以前系统间是同步的远程调用,现在是通过 MQ 进行异步调用。
    如何保证消息没有被重复消费?怎么处理消息丢失情况?怎么保证消息传递的顺序性?

  • 一致性问题
    A 系统处理完业务,通过 MQ 给 B、C 系统发消息数据,如果 B 系统处理成功,C 系统处理失败。如何保证消息数据处理的一致性?

1.4、小结

既然 MQ 有优势也有劣势,那么使用 MQ 需要满足什么条件呢?

  • 生产者不需要从消费者处获得反馈。引入消息队列之前的直接调用,其接口的返回值应该为空,这才让明明下层的动作还没做,上层却当成动作做完了继续往后走,即所谓异步成为了可能。
  • 容许短暂的不一致性。
  • 确实是用了有效果。即解耦、提速、削峰这些方面的收益,超过加入 MQ ,管理 MQ 这些成本。

1.5、产品

目前业界有很多的 MQ 产品,例如 RabbitMQ 、 RocketMQ 、 ActiveMQ 、 Kafka 、 ZeroMQ 、 MetaMq 等,也有直接使用 R edis 充当消息队列的案例。而这些消息队列产品,各有侧重,在实际选型时,需要结合自身需求及 MQ 产品特征,综合考虑。

RabbitMQActiveMQRocketMQKafka
公司/社区RabbitApache阿里巴巴Apache
开发语言ErlangJavaJavaScala & Java
协议支持AMQP、XMPP、SMTP、STOMPOpenWire、STOMP、REST、XMPP、AMQP自定义自定义协议,社区封装了 http 协议支持
客户端支持语言官方支持 Erlang、Java、Ruby 等,社区产出多种 API 几乎支持所有语言Java、C、C++、Python、PHP、Perl、.net 等Java、C++(不成熟)官方支持 Java, 社区产出多种 API,如 PHP、Python 等
单机吞吐量万级(其次)万级(最差)十万级(最好)十万级(次之)
消息延迟微妙级毫秒级毫秒级毫秒以内
功能特性并发能力强,性能极其好,延时低,社区活跃,管理界面丰富老牌产品,成熟度高,文档较多MQ 功能比较完备,扩展性佳只支持主要的 MQ 功能,毕竟是为大数据领域准备的。

二、RabbitMQ 简介

官方网址:https://www.rabbitmq.com/
官方文档:https://www.rabbitmq.com/getstarted.html
官方下载:https://www.rabbitmq.com/download.html

AMQP 即 Advanced Message Queuing Protocol (高级消息队列协议),是一个网络协议,是应用层协议的一个开放标准,为面向消息的中间件设计。基于此协议的客户端与消息中间件可传递消息,并不受客户端中间件不同产品,不同的开发语言等条件的限制。 2006年,AMQP 规范发布。类比 HTTP 。

2007年, Rabbit 技术公司基于 AMQP 标准开发的 RabbitMQ 1.0 发布。 RabbitMQ 采用 Erlang 语言开发。Erlang 语言由 Ericson 设计,专门为开发高并发和分布式系统的一种语言,在电信领域使用广泛。

2.1、架构

rabbitmq-架构

2.2、概念

  • Message Broker(消息代理)
    接收和分发消息的应用,RabbitMQ Server 就是 Message Broker 。
  • Virtual host(虚拟主机)
    出于多租户和安全因素设计的,把 AMQP 的基本组件划分到一个虚拟的分组中,类似于网络中的 namespace 概念。当多个不同的用户使用同一个 RabbitMQ server 提供的服务时,可以划分出多个 Virtual host ,每个用户在自己的 Virtual host 创建 exchange、queue 等。
  • Connection(连接)
    Producer 和 Consumer 与 broker 之间的 TCP 连接。
  • Channel(管道)
    如果每一次访问 RabbitMQ 都建立一个 Connection ,在消息量大的时候建立 TCP Connection的开销将是巨大的,效率也较低。 Channel 是在 connection 内部建立的逻辑连接,如果应用程序支持多线程,通常每个 thread 创建单独的 channel 进行通讯,AMQP method 包含了 channel id 帮助客户端和 message broker 识别 channel ,所以 channel 之间是完全隔离的。 Channel 作为轻量级的 Connection 极大减少了操作系统建立 TCP connection 的开销。
  • Exchange(交换机)
    message 到达 broker 的第一站,根据分发规则,匹配查询表中的 routing key ,分发消息到 queue 中去。常用的类型有: direct (点对点)、topic (发布/订阅)、fanout (广播)。
  • Queue(队列)
    消息最终被送到这里等待消费者取走
  • Binding(绑定)
    exchange 和 queue 之间的虚拟连接, binding 中可以包含 routing key 。 Binding 信息被保存到 exchange 中的查询表中,用于 message 的分发依据。
  • 交换机类型:
    Default Exchange:默认交换机,发送消息时 RoutingKey 要与队列名一致。
    Direct Exchange:定向交换机,将消息交给符合指定 RoutingKey 的队列。
    Fanout Exchange:广播交换机,将消息交给所有绑定到该交换机的队列,RoutingKey 为 “”。
    Topic Exchange:主题交换机,把消息交给符合 RoutingPattern 的队列。“*”:星号代表一个单词。“#”:井号代表零个或多个单词。
    交换机只负责转发消息,不具备存储消息的能力。如果没有任何队列与交换机绑定,或者没有符合路由规则的队列,那么消息会丢失!

2.3、安装

略。

三、RabbitMQ 工作模式

创建 rabbitmq-producer(Maven生产者工程)、rabbitmq-consumer(Maven消费者工程)。

导入依赖:

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.6.0</version>
</dependency>

3.1、简单模式(默认交换机)

简单模式(Hello Word):最简单的事情。使用默认交换机(""),发送消息时 RoutingKey 要与队列名一致。
在这里插入图片描述
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。

3.1.1、生产者

public class Producer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("hello_world_queue", true, false, false, null);
        // 6、发送消息,参数:交换机名称、路由键、配置信息、消息内容
        channel.basicPublish("", "hello_world_queue", null, "hello rabbitmq ~".getBytes());
        // 7、释放资源
        channel.close();
        connection.close();
    }
}

3.1.2、消费者

public class Consumer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("hello_world_queue", true, false, false, null);
        // 6、创建生产者
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("(生产者标识)consumerTag = " + consumerTag);
                System.out.println("(相关信息)envelope = " + envelope.getRoutingKey());
                System.out.println("(配置信息)properties = " + properties);
                System.out.println("(消息内容)body = " + new String(body));
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("hello_world_queue", true, consumer);
    }
}

先运行生产者,再运行消费者。消费者控制台输出:

(生产者标识)consumerTag = amq.ctag-uySvr2z0LeNYxexAeGzvRw
(相关信息)envelope = hello_world_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 = hello rabbitmq ~

3.2、工作队列(默认交换机)

工作队列(Work Queues):在工人之间分配任务(竞争的消费者模式)。多个消费者监听同一个队列,只能有一个消费者收到。使用默认交换机(""),发送消息时 RoutingKey 要与队列名一致。
工作队列
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。

3.2.1、生产者

public class Producer_WorkQueues {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列,(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("work_queues_queue", true, false, false, null);
        // 6、发送多条消息
        for (int i = 0; i < 10; i++) {
            String body = i + " hello rabbitmq ~";
            // 参数:交换机名称、路由键、配置信息、消息内容
            channel.basicPublish("", "work_queues_queue", null, body.getBytes());
        }
        // 7、释放资源
        channel.close();
        connection.close();
    }
}

3.2.2、消费者1

public class Consumer_WorkQueues_1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("work_queues_queue", true, false, false, null);
        // 6、创建生产者
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer1 body = " + new String(body));
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("work_queues_queue", true, consumer);
    }
}

3.2.3、消费者2

public class Consumer_WorkQueues_2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("work_queues_queue", true, false, false, null);
        // 6、创建生产者
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer2 body = " + new String(body));
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("work_queues_queue", true, consumer);
    }
}

先运行消费者1、消费者2,再运行生产者。消费者1、消费者2控制台输出:

消费者1:
consumer1 body = 0 hello rabbitmq ~
consumer1 body = 2 hello rabbitmq ~
consumer1 body = 4 hello rabbitmq ~
consumer1 body = 6 hello rabbitmq ~
consumer1 body = 8 hello rabbitmq ~

消费者2:
consumer2 body = 1 hello rabbitmq ~
consumer2 body = 3 hello rabbitmq ~
consumer2 body = 5 hello rabbitmq ~
consumer2 body = 7 hello rabbitmq ~
consumer2 body = 9 hello rabbitmq ~

3.3、发布订阅(广播交换机)

发布订阅(Publish / Subscribe):同时向多个消费者发送消息。多个消费者监听自己的队列,每个消费者都能接收到消息。使用广播交换机将消息传给所有绑定到该交换机的队列,RoutingKey 设为 ""。
发布订阅
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。
X:交换机,接收生产者发送的消息、根据交换机类型处理消息。

3.1.1、生产者

public class Producer_PublishSubscribe {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建广播交换机,参数:交换机名称、交换机类型、是否持久化、自动删除、内部使用、参数
        String exchange = "fanoutExchange";
        channel.exchangeDeclare(exchange, BuiltinExchangeType.FANOUT, true, false, false, null);
        // 6、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        String queue1 = "fanoutExchange_queue_1";
        String queue2 = "fanoutExchange_queue_2";
        channel.queueDeclare(queue1, true, false, false, null);
        channel.queueDeclare(queue2, true, false, false, null);
        // 7、绑定队列到交换机并指定路由键
        channel.queueBind(queue1, exchange, "");
        channel.queueBind(queue2, exchange, "");
        String body = "日志级别:info... 日志信息:正常信息...";
        // 8、发送消息,参数:交换机名称、路由键、配置信息、消息内容
        channel.basicPublish(exchange, "", null, body.getBytes());
        // 9、释放资源
        channel.close();
        connection.close();
    }
}

3.1.2、消费者1

public class Consumer_PublishSubscribe_1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("fanoutExchange_queue_1", true, false, false, null);
        // 6、创建消费者
        Consumer consumer1 = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer1 body = " + new String(body));
                System.out.println("日志信息输出到控制台...");
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("fanoutExchange_queue_1", true, consumer1);
    }
}

3.1.3、消费者2

public class Consumer_PublishSubscribe_2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("fanoutExchange_queue_2", true, false, false, null);
        // 6、创建消费者
        Consumer consumer2 = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer2 body = " + new String(body));
                System.out.println("日志信息保存到数据库...");
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("fanoutExchange_queue_2", true, consumer2);
    }
}

先运行生产者,再运行消费者1、消费者2。消费者1、消费者2控制台输出:

消费者1:
consumer1 body = 日志级别:info... 日志信息:正常信息...
日志信息输出到控制台...

消费者2:
consumer2 body = 日志级别:info... 日志信息:正常信息...
日志信息保存到数据库...

3.4、路由模式(定向交换机)

路由模式(Routing):有选择地接收消息。使用定向交换机将消息传给符合指定 RoutingKey 的队列,队列在绑定交换机时要指定 RoutingKey 。
路由模式
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。
X:交换机,接收生产者发送的消息、根据交换机类型处理消息。

3.4.1、生产者

public class Producer_Routing {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建定向交换机,参数:交换机名称、交换机类型、是否持久化、自动删除、内部使用、参数
        String exchange = "directExchange";
        channel.exchangeDeclare(exchange, BuiltinExchangeType.DIRECT, true, false, false, null);
        // 6、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        String queue1 = "directExchange_queue_1";
        String queue2 = "directExchange_queue_2";
        channel.queueDeclare(queue1, true, false, false, null);
        channel.queueDeclare(queue2, true, false, false, null);
        // 7、绑定队列到交换机并指定路由键
        // 7.1、队列1绑定路由键error
        channel.queueBind(queue1, exchange, "error");
        // 7.2、队列2绑定路由键info、error、warning
        channel.queueBind(queue2, exchange, "info");
        channel.queueBind(queue2, exchange, "error");
        channel.queueBind(queue2, exchange, "warning");
        // 8、发送消息,参数:交换机名称、路由键、配置信息、消息内容
        String info = "日志级别:info... 日志信息:正常信息...";
        channel.basicPublish(exchange, "info", null, info.getBytes());
        String error = "日志级别:error... 日志信息:错误信息...";
        channel.basicPublish(exchange, "error", null, error.getBytes());
        String warning = "日志级别:warning... 日志信息:警告信息...";
        channel.basicPublish(exchange, "warning", null, warning.getBytes());
        // 9、释放资源
        channel.close();
        connection.close();
    }
}

3.4.2、消费者1

public class Consumer_Routing_1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("directExchange_queue_1", true, false, false, null);
        // 6、创建消费者
        Consumer consumer1 = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer1 body = " + new String(body));
                System.out.println("日志信息保存到数据库...");
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("directExchange_queue_1", true, consumer1);
    }
}

3.4.3、消费者2

public class Consumer_Routing_2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列
        channel.queueDeclare("directExchange_queue_2", true, false, false, null);
        // 6、创建消费者
        Consumer consumer2 = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer2 body = " + new String(body));
                System.out.println("日志信息输出到控制台...");
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("directExchange_queue_2", true, consumer2);
    }
}

先运行生产者,再运行消费者1、消费者2。消费者1、消费者2控制台输出:

消费者1:
consumer1 body = 日志级别:error... 日志信息:错误信息...
日志信息保存到数据库...

消费者2:
consumer2 body = 日志级别:info... 日志信息:正常信息...
日志信息输出到控制台...
consumer2 body = 日志级别:error... 日志信息:错误信息...
日志信息输出到控制台...
consumer2 body = 日志级别:warning... 日志信息:警告信息...
日志信息输出到控制台...

3.5、主题模式(主题交换机)

主题模式(Topics):基于通配符模式接收消息。就是 RoutingKey 可以使用通配符。使用主题交换机将消息传给符合指定 RoutingKey 规则的队列。
主题模式
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。
X:交换机,接收生产者发送的消息、根据交换机类型处理消息。
*:星号代表一个单词。
#:井号代表零个或多个单词。

3.5.1、生产者

public class Producer_Topics {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建主题交换机,参数:交换机名称、交换机类型、是否持久化、自动删除、内部使用、参数
        String exchange = "topicExchange";
        channel.exchangeDeclare(exchange, BuiltinExchangeType.TOPIC, true, false, false, null);
        // 6、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        String queue1 = "topicExchange_queue_1";
        String queue2 = "topicExchange_queue_2";
        channel.queueDeclare(queue1, true, false, false, null);
        channel.queueDeclare(queue2, true, false, false, null);
        // 7、绑定队列到交换机并指定路由键
        channel.queueBind(queue1, exchange, "#.error");
        channel.queueBind(queue2, exchange, "order.*");
        channel.queueBind(queue2, exchange, "*.*");
        // 8、发送消息
        String msg1 = "日志级别:info... 日志信息:订单信息...";
        channel.basicPublish(exchange, "order.info", null, msg1.getBytes());
        String msg2 = "日志级别:error... 日志信息:订单错误...";
        channel.basicPublish(exchange, "order.order.error", null, msg2.getBytes());
        String msg3 = "日志级别:error... 日志信息:用户错误...";
        channel.basicPublish(exchange, "user.error", null, msg3.getBytes());
        // 9、释放资源
        channel.close();
        connection.close();
    }
}

3.5.2、消费者1

public class Consumer_Topics_1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("topicExchange_queue_1", true, false, false, null);
        // 6、创建消费者
        Consumer consumer1 = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer1 body = " + new String(body));
                System.out.println("日志信息保存到数据库...");
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("topicExchange_queue_1", true, consumer1);
    }
}

3.5.3、消费者2

public class Consumer_Topics_2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置参数
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        // 3、创建连接
        Connection connection = factory.newConnection();
        // 4、创建管道
        Channel channel = connection.createChannel();
        // 5、创建队列(不存在则创建,存在则不创建),参数:队列名称、是否持久化、是否独占、是否自动删除、参数
        channel.queueDeclare("topicExchange_queue_2", true, false, false, null);
        // 6、创建消费者
        Consumer consumer2 = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumer2 body = " + new String(body));
                System.out.println("日志信息保存到数据库...");
            }
        };
        // 7、接收消息,参数:队列名称、是否自动确认、回调对象
        channel.basicConsume("topicExchange_queue_2", true, consumer2);
    }
}

先运行生产者,再运行消费者1、消费者2。消费者1、消费者2控制台输出:

消费者1:
consumer1 body = 日志级别:error... 日志信息:订单错误...
日志信息保存到数据库...
consumer1 body = 日志级别:error... 日志信息:用户错误...
日志信息保存到数据库...

消费者2:
consumer2 body = 日志级别:info... 日志信息:订单信息...
日志信息保存到数据库...
consumer2 body = 日志级别:error... 日志信息:订单错误...
日志信息保存到数据库...
consumer2 body = 日志级别:error... 日志信息:用户错误...
日志信息保存到数据库...

四、Spring Boot 整合

创建 springboot-rabbitmq-producer(SpringBoot生产者工程)、spingboot-rabbitmq-consumer(SpringBoot消费者工程)。

导入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

yml配置:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: /

4.1、简单模式(默认交换机)

简单模式(Hello Word):最简单的事情。使用默认交换机(""),发送消息时 RoutingKey 要与队列名一致。在这里插入图片描述
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。

4.1.1、生产者

@Configuration
public class ProducerSimpleConfig {
    @Bean
    public Queue simple_queue() {
        // 创建队列,参数:队列名称
        return new Queue("simple_queue");
    }
}

发送消息:

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void test_Producer_Simple() {
        // 转换和发送,参数:交换机、路由键、消息内容
        rabbitTemplate.convertAndSend("", "simple_queue", "hello rabbitmq ~");
    }

4.1.2、消费者

@Component
public class ConsumerSimpleListen {
    @RabbitListener(queues = "simple_queue")
    public void consumer(Message message) {
        System.out.println("consumer message = " + new String(message.getBody()));
    }
}

启动主程序接收消息,控制台输出:

consumer message = hello rabbitmq ~

4.2、工作队列(默认交换机)

工作队列(Work Queues):在工人之间分配任务(竞争的消费者模式)。多个消费者监听同一个队列,只能有一个消费者收到。使用默认交换机(""),发送消息时 RoutingKey 要与队列名一致。
工作队列
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。`

4.2.1、生产者

@Configuration
public class ProducerWorkQueuesConfig {
    @Bean
    public Queue work_queues_queue() {
        // 创建队列,参数:队列名称
        return new Queue("work_queues_queue");
    }
}

发送消息:

    @Autowired
    private RabbitTemplate rabbitTemplate;

	@Test
    void test_Producer_WorkQueues() {
        for (int i = 0; i < 10; i++) {
            // 转换和发送,参数:交换机、路由键、消息内容
            rabbitTemplate.convertAndSend("", "work_queues_queue", i + " hello rabbitmq ~");
        }
    }

4.2.2、消费者

@Component
public class ConsumerWorkQueuesListen {
    @RabbitListener(queues = "work_queues_queue")
    public void consumer1(Message message) {
        System.out.println("consumer1 message = " + new String(message.getBody()));
    }

    @RabbitListener(queues = "work_queues_queue")
    public void consumer2(Message message) {
        System.out.println("consumer2 message = " + new String(message.getBody()));
    }
}

启动主程序接收消息,控制台输出:

consumer2 message = 0 hello rabbitmq ~
consumer2 message = 2 hello rabbitmq ~
consumer1 message = 1 hello rabbitmq ~
consumer2 message = 4 hello rabbitmq ~
consumer1 message = 3 hello rabbitmq ~
consumer2 message = 6 hello rabbitmq ~
consumer1 message = 5 hello rabbitmq ~
consumer2 message = 8 hello rabbitmq ~
consumer1 message = 7 hello rabbitmq ~
consumer1 message = 9 hello rabbitmq ~

4.3、发布订阅(广播交换机)

发布订阅(Publish / Subscribe):同时向多个消费者发送消息。多个消费者监听自己的队列,每个消费者都能接收到消息。使用广播交换机将消息传给所有绑定到该交换机的队列,RoutingKey 设为 ""。
发布订阅
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。
X:交换机,接收生产者发送的消息、根据交换机类型处理消息。

4.3.1、生产者

@Configuration
public class ProducerPublishSubscribeConfig {
    @Bean
    public FanoutExchange fanoutExchange() {
        // 创建广播交换机,参数:交换机名称
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    public Queue fanoutExchange_queue_1() {
        // 创建队列,参数:队列名称
        return new Queue("fanoutExchange_queue_1");
    }
    @Bean
    public Queue fanoutExchange_queue_2() {
        return new Queue("fanoutExchange_queue_2");
    }

    @Bean
    public Binding binding1(Queue fanoutExchange_queue_1, FanoutExchange fanoutExchange) {
        // 绑定队列到交换机
        return BindingBuilder.bind(fanoutExchange_queue_1).to(fanoutExchange);
    }
    @Bean
    public Binding binding2(Queue fanoutExchange_queue_2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutExchange_queue_2).to(fanoutExchange);
    }
}

发送消息:

    @Autowired
    private RabbitTemplate rabbitTemplate;

	@Test
    void test_Producer_Publish_Subscribe() {
        // 转换和发送,参数:交换机、路由键、消息内容
        rabbitTemplate.convertAndSend("fanoutExchange", "", "日志级别:info... 日志信息:正常信息...");
    }

4.3.2、消费者

@Component
public class ConsumerPublishSubscribeListen {
    @RabbitListener(queues = "fanoutExchange_queue_1")
    public void consumer3(Message message) {
        System.out.println("consumer3 message = " + new String(message.getBody()));
    }

    @RabbitListener(queues = "fanoutExchange_queue_2")
    public void consumer4(Message message) {
        System.out.println("consumer4 message = " + new String(message.getBody()));
    }
}

启动主程序接收消息,控制台输出:

consumer3 message = hello rabbitmq ~
consumer4 message = hello rabbitmq ~

4.4、路由模式(定向交换机)

路由模式(Routing):有选择地接收消息。使用定向交换机将消息传给符合指定 RoutingKey 的队列,队列在绑定交换机时要指定 RoutingKey 。
在路由模式
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。
X:交换机,接收生产者发送的消息、根据交换机类型处理消息。

4.4.1、生产者

@Configuration
public class ProducerRoutingConfig {
    @Bean
    public DirectExchange directExchange() {
        // 创建定向交换机,参数:交换机名称
        return new DirectExchange("directExchange");
    }

    @Bean
    public Queue directExchange_queue_1() {
        // 创建队列,参数:队列名称
        return new Queue("directExchange_queue_1");
    }
    @Bean
    public Queue directExchange_queue_2() {
        return new Queue("directExchange_queue_2");
    }

    @Bean
    public Binding binding3(Queue directExchange_queue_1, DirectExchange directExchange) {
        // 绑定队列到交换机并指定路由键
        return BindingBuilder.bind(directExchange_queue_1).to(directExchange).with("error");
    }
    @Bean
    public Binding binding4(Queue directExchange_queue_2, DirectExchange directExchange) {
        return BindingBuilder.bind(directExchange_queue_2).to(directExchange).with("info");
    }
    @Bean
    public Binding binding5(Queue directExchange_queue_2, DirectExchange directExchange) {
        return BindingBuilder.bind(directExchange_queue_2).to(directExchange).with("error");
    }
    @Bean
    public Binding binding6(Queue directExchange_queue_2, DirectExchange directExchange) {
        return BindingBuilder.bind(directExchange_queue_2).to(directExchange).with("warning");
    }
}

发送消息:

    @Autowired
    private RabbitTemplate rabbitTemplate;

	@Test
    void test_Producer_Routing() {
        // 转换和发送,参数:交换机、路由键、消息内容
        rabbitTemplate.convertAndSend("directExchange", "info", "日志级别:info... 日志信息:正常信息...");
        rabbitTemplate.convertAndSend("directExchange", "error", "日志级别:error... 日志信息:错误信息...");
        rabbitTemplate.convertAndSend("directExchange", "warning", "日志级别:warning... 日志信息:警告信息...");
    }

4.4.2、消费者

@Component
public class ConsumerRoutingListen {
    @RabbitListener(queues = "directExchange_queue_1")
    public void consumer5(Message message) {
        System.out.println("consumer5 message = " + new String(message.getBody()));
    }

    @RabbitListener(queues = "directExchange_queue_2")
    public void consumer6(Message message) {
        System.out.println("consumer6 message = " + new String(message.getBody()));
    }
}

启动主程序接收消息,控制台输出:

consumer5 message = 日志级别:error... 日志信息:错误信息...
consumer6 message = 日志级别:info... 日志信息:正常信息...
consumer6 message = 日志级别:error... 日志信息:错误信息...
consumer6 message = 日志级别:warning... 日志信息:警告信息...

4.5、主题模式(主题交换机)

主题模式(Topics):基于通配符模式接收消息。就是 RoutingKey 可以使用通配符。使用主题交换机将消息传给符合指定 RoutingKey 规则的队列。
主题模式
P:生产者,发送消息的程序。
C:消费者,接收消息的程序。
Q:消息队列,红色部分。
X:交换机,接收生产者发送的消息、根据交换机类型处理消息。
*:星号代表一个单词。
#:井号代表零个或多个单词。

4.5.1、生产者

@Configuration
public class ProducerTopicConfig {
    @Bean
    public TopicExchange topicExchange() {
        // 创建主题交换机,参数:交换机名称
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Queue topicExchange_queue_1() {
        // 创建队列,参数:队列名称
        return new Queue("topicExchange_queue_1");
    }
    @Bean
    public Queue topicExchange_queue_2() {
        return new Queue("topicExchange_queue_2");
    }
    @Bean
    public Queue topicExchange_queue_3() {
        return new Queue("topicExchange_queue_3");
    }

    @Bean
    public Binding binding7(Queue topicExchange_queue_1, TopicExchange topicExchange) {
        // 队列绑定交换机并指定路由键
        return BindingBuilder.bind(topicExchange_queue_1).to(topicExchange).with("#.error");
    }
    @Bean
    public Binding binding8(Queue topicExchange_queue_2, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicExchange_queue_2).to(topicExchange).with("order.*");
    }
    @Bean
    public Binding binding9(Queue topicExchange_queue_3, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicExchange_queue_3).to(topicExchange).with("*.*");
    }
}

发送消息:

    @Autowired
    private RabbitTemplate rabbitTemplate;

	@Test
    void test_Producer_Topic() {
        // 转换和发送,参数:交换机、路由键、消息内容
        rabbitTemplate.convertAndSend("topicExchange", "order.info", "日志级别:info... 日志信息:订单信息...");
        rabbitTemplate.convertAndSend("topicExchange", "order.order.error", "日志级别:error... 日志信息:订单错误...");
        rabbitTemplate.convertAndSend("topicExchange", "user.error", "日志级别:error... 日志信息:用户错误...");
    }

4.5.2、消费者

@Component
public class ConsumerTopicListen {
    @RabbitListener(queues = "topicExchange_queue_1")
    public void consumer7(Message message) {
        System.out.println("consumer7 message = " + new String(message.getBody()));
    }

    @RabbitListener(queues = "topicExchange_queue_2")
    public void consumer8(Message message) {
        System.out.println("consumer8 message = " + new String(message.getBody()));
    }
}

启动主程序接收消息,控制台输出:

consumer7 message = 日志级别:error... 日志信息:用户错误...
consumer8 message = 日志级别:info... 日志信息:订单信息...
consumer8 message = 日志级别:error... 日志信息:用户错误...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值