RabbitMQ of message queue

This article is quoted from my blog: https://juejin.im/post/5a67f7836fb9a01cb74e8931

 

Regarding the message queue, I have read some materials on and off since the year before last. I have been wanting to write it for a long time, but I have not made time for it. Recently, I met a few friends talking about the technical selection of this area. It is time to organize and record the knowledge of this area. .

 

There are many message queue products on the market, such as the old ActiveMQ, RabbitMQ, the most popular Kafka I see at present, and ZeroMQ, which Alibaba donated to Apache's RocketMQ at the end of last year, and even NoSQL databases such as redis also support MQ functions. In short, there are more than a dozen well-known products. Based on my own experience and interest, I only intend to talk about RabbitMQ, Kafka and ActiveMQ. This article will first talk about RabbitMQ, and before that, let’s take a look at the related concepts of message queues.

 

What is a message queue

Message (Message) refers to the data transmitted between applications. Messages can be very simple, such as containing only text strings, or more complex, possibly containing embedded objects.

 

Message Queue (Message Queue) is a communication method between applications. After a message is sent, it can be returned immediately, and the message system can ensure the reliable delivery of the message. The message publisher only needs to publish the message to MQ and does not care who gets it, and the message consumer only needs to get the message from MQ regardless of who publishes it. In this way, neither the publisher nor the user has to know the existence of each other.

 

Why use message queues

From the above description, we can see that the message queue is an asynchronous cooperation mechanism between applications. When do we need to use MQ?

 

Taking a common order system as an example, the business logic after a user clicks the [Place Order] button may include: deducting inventory, generating corresponding documents, sending red envelopes, and sending SMS notifications. In the early stage of business development, these logics may be put together and executed synchronously. With the development of the business, the number of orders increases, and the performance of system services needs to be improved. At this time, some operations that do not need to take effect immediately can be split and executed asynchronously, such as issuing red envelopes, Send SMS notifications, etc. In this scenario, MQ can be used to send a message to MQ after the main process of placing an order (such as deducting inventory and generating corresponding documents) to complete the main process quickly, while another separate thread pulls MQ messages (or MQ pushes messages), when it is found that there are messages such as red envelopes or text messages in MQ, the corresponding business logic is executed.

 

The above is the case for business decoupling. Other common scenarios include eventual consistency, broadcast, staggered flow control, and so on.

RabbitMQ Features

RabbitMQ is an open source implementation of AMQP developed in the Erlang language.

 

AMQP: Advanced Message Queue, Advanced Message Queuing Protocol. It is an open standard of the application layer protocol, designed for message-oriented middleware. Clients and message middleware based on this protocol can transfer messages, and are not limited by products, development languages, and other conditions.

 

RabbitMQ originally originated from the financial system and is used to store and forward messages in a distributed system. It performs well in terms of ease of use, scalability, and high availability. Specific features include:

  1. Reliability
    RabbitMQ uses some mechanisms to ensure reliability, such as persistence, transmission confirmation, and publishing confirmation.

  2. Flexible Routing
    routes messages through Exchange before they enter the queue. For typical routing functions, RabbitMQ already provides some built-in Exchange to implement. For more complex routing functions, multiple Exchanges can be bound together, and their own Exchanges can also be implemented through the plug-in mechanism.

  3. Message clustering (Clustering)
    Multiple RabbitMQ servers can form a cluster to form a logical Broker.

  4. High Availability (Highly Available Queues)
    queues can be mirrored on machines in the cluster, so that the queues are still available in the event of some node failure.

  5. Multi-protocol
    RabbitMQ supports a variety of message queue protocols, such as STOMP, MQTT, etc.

  6. Many Clients
    RabbitMQ supports almost all common languages, such as Java, .NET, Ruby, etc.

  7. Management UI
    RabbitMQ provides an easy-to-use user interface that enables users to monitor and manage many aspects of the message broker.

  8. Tracing mechanism (Tracing)
    If the message is abnormal, RabbitMQ provides a message tracking mechanism, the user can find out what happened.

  9. Plugin System (Plugin System)
    RabbitMQ provides many plugins to be extended in many ways, and you can also write your own plugins.

Conceptual model in RabbitMQ

message model

All MQ products have the same process in terms of model abstraction:
a consumer subscribes to a queue. The producer (producer) creates the message, then publishes it to the queue (queue), and finally sends the message to the listening consumers.


 

RabbitMQ basic concepts

The above is just the simplest abstract description, and there are more detailed concepts that need to be explained when it comes to RabbitMQ. As mentioned above, RabbitMQ is an open source implementation of the AMQP protocol, so its internals are actually the basic concepts in AMQP:


  1. Message
    消息,消息是不具名的,它由消息头和消息体组成。消息体是不透明的,而消息头则由一系列的可选属性组成,这些属性包括routing-key(路由键)、priority(相对于其他消息的优先权)、delivery-mode(指出该消息可能需要持久性存储)等。
  2. Publisher
    消息的生产者,也是一个向交换器发布消息的客户端应用程序。
  3. Exchange
    交换器,用来接收生产者发送的消息并将这些消息路由给服务器中的队列。
  4. Binding
    绑定,用于消息队列和交换器之间的关联。一个绑定就是基于路由键将交换器和消息队列连接起来的路由规则,所以可以将交换器理解成一个由绑定构成的路由表。
  5. Queue
    消息队列,用来保存消息直到发送给消费者。它是消息的容器,也是消息的终点。一个消息可投入一个或多个队列。消息一直在队列里面,等待消费者连接到这个队列将其取走。
  6. Connection
    网络连接,比如一个TCP连接。
  7. Channel
    信道,多路复用连接中的一条独立的双向数据流通道。信道是建立在真实的TCP连接内地虚拟连接,AMQP 命令都是通过信道发出去的,不管是发布消息、订阅队列还是接收消息,这些动作都是通过信道完成。因为对于操作系统来说建立和销毁 TCP 都是非常昂贵的开销,所以引入了信道的概念,以复用一条 TCP 连接。
  8. Consumer
    消息的消费者,表示一个从消息队列中取得消息的客户端应用程序。
  9. Virtual Host
    虚拟主机,表示一批交换器、消息队列和相关对象。虚拟主机是共享相同的身份认证和加密环境的独立服务器域。每个 vhost 本质上就是一个 mini 版的 RabbitMQ 服务器,拥有自己的队列、交换器、绑定和权限机制。vhost 是 AMQP 概念的基础,必须在连接时指定,RabbitMQ 默认的 vhost 是 / 。
  10. Broker
    表示消息队列服务器实体。
AMQP 中的消息路由

AMQP 中消息的路由过程和 Java 开发者熟悉的 JMS 存在一些差别,AMQP 中增加了 Exchange 和 Binding 的角色。生产者把消息发布到 Exchange 上,消息最终到达队列并被消费者接收,而 Binding 决定交换器的消息应该发送到那个队列。

Exchange 类型

Exchange分发消息时根据类型的不同分发策略有区别,目前共四种类型:direct、fanout、topic、headers 。headers 匹配 AMQP 消息的 header 而不是路由键,此外 headers 交换器和 direct 交换器完全一致,但性能差很多,目前几乎用不到了,所以直接看另外三种类型:

 

1.direct


消息中的路由键(routing key)如果和 Binding 中的 binding key 一致, 交换器就将消息发到对应的队列中。路由键与队列名完全匹配,如果一个队列绑定到交换机要求路由键为“dog”,则只转发 routing key 标记为“dog”的消息,不会转发“dog.puppy”,也不会转发“dog.guard”等等。它是完全匹配、单播的模式。

 

2.fanout


每个发到 fanout 类型交换器的消息都会分到所有绑定的队列上去。fanout 交换器不处理路由键,只是简单的将队列绑定到交换器上,每个发送到交换器的消息都会被转发到与该交换器绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。fanout 类型转发消息是最快的。

 

3.topic


topic 交换器通过模式匹配分配消息的路由键属性,将路由键和某个模式进行匹配,此时队列需要绑定到一个模式上。它将路由键和绑定键的字符串切分成单词,这些单词之间用点隔开。它同样也会识别两个通配符:符号“#”和符号“”。#匹配0个或多个单词,匹配不多不少一个单词。

 

RabbitMQ 安装

一般来说安装 RabbitMQ 之前要安装 Erlang ,可以去Erlang官网下载。接着去RabbitMQ官网下载安装包,之后解压缩即可。根据操作系统不同官网提供了相应的安装说明:WindowsDebian / UbuntuRPM-based LinuxMac

如果是Mac 用户,个人推荐使用 HomeBrew 来安装,安装前要先更新 brew:

 

brew update

接着安装 rabbitmq 服务器:

 

 

brew install rabbitmq

 

这样 RabbitMQ 就安装好了,安装过程中会自动其所依赖的 Erlang 。

 

RabbitMQ 运行和管理

1.启动

启动很简单,找到安装后的 RabbitMQ 所在目录下的 sbin 目录,可以看到该目录下有6个以 rabbitmq 开头的可执行文件,直接执行 rabbitmq-server 即可,下面将 RabbitMQ 的安装位置以 . 代替,启动命令就是:

./sbin/rabbitmq-server

启动正常的话会看到一些启动过程信息和最后的 completed with 7 plugins,这也说明启动的时候默认加载了7个插件。



2.后台启动

如果想让 RabbitMQ 以守护程序的方式在后台运行,可以在启动的时候加上 -detached 参数:

./sbin/rabbitmq-server -detached

3.查询服务器状态

sbin 目录下有个特别重要的文件叫 rabbitmqctl ,它提供了 RabbitMQ 管理需要的几乎一站式解决方案,绝大部分的运维命令它都可以提供。

 

查询 RabbitMQ 服务器的状态信息可以用参数 status :

./sbin/rabbitmqctl status

该命令将输出服务器的很多信息,比如 RabbitMQ 和 Erlang 的版本、OS 名称、内存等等

 

4.关闭 RabbitMQ 节点

我们知道 RabbitMQ 是用 Erlang 语言写的,在Erlang 中有两个概念:节点和应用程序。节点就是 Erlang 虚拟机的每个实例,而多个 Erlang 应用程序可以运行在同一个节点之上。节点之间可以进行本地通信(不管他们是不是运行在同一台服务器之上)。比如一个运行在节点A上的应用程序可以调用节点B上应用程序的方法,就好像调用本地函数一样。如果应用程序由于某些原因奔溃,Erlang 节点会自动尝试重启应用程序。

 

如果要关闭整个 RabbitMQ 节点可以用参数 stop :

./sbin/rabbitmqctl stop

它会和本地节点通信并指示其干净的关闭,也可以指定关闭不同的节点,包括远程节点,只需要传入参数 -n :

./sbin/rabbitmqctl -n [email protected] stop

-n node 默认 node 名称是 rabbit@server ,如果你的主机名是 server.example.com ,那么 node 名称就是 [email protected]

 

5.关闭 RabbitMQ 应用程序

如果只想关闭应用程序,同时保持 Erlang 节点运行则可以用 stop_app:

./sbin/rabbitmqctl stop_app

这个命令在后面要讲的集群模式中将会很有用。

 

6.启动 RabbitMQ 应用程序

./sbin/rabbitmqctl start_app

7.重置 RabbitMQ 节点

./sbin/rabbitmqctl reset

该命令将清除所有的队列。

 

8.查看已声明的队列

./sbin/rabbitmqctl list_queues

9.查看交换器

./sbin/rabbitmqctl list_exchanges

该命令还可以附加参数,比如列出交换器的名称、类型、是否持久化、是否自动删除:

./sbin/rabbitmqctl list_exchanges name type durable auto_delete

10.查看绑定

./sbin/rabbitmqctl list_bindings

 

Java 客户端访问

RabbitMQ 支持多种语言访问,以 Java 为例看下一般使用 RabbitMQ 的步骤。

 

1.maven工程的pom文件中添加依赖

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

2.消息生产者

package org.study.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Producer {

 public static void main(String[] args) throws IOException, TimeoutException {
     //创建连接工厂
     ConnectionFactory factory = new ConnectionFactory();
     factory.setUsername("guest");
     factory.setPassword("guest");
     //设置 RabbitMQ 地址
     factory.setHost("localhost");
     //建立到代理服务器到连接
     Connection conn = factory.newConnection();
     //获得信道
     Channel channel = conn.createChannel();
     //声明交换器
     String exchangeName = "hello-exchange";
     channel.exchangeDeclare(exchangeName, "direct", true);

     String routingKey = "hola";
     //发布消息
     byte[] messageBodyBytes = "quit".getBytes();
     channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes);

     channel.close();
     conn.close();
 }
}

3.消息消费者

package org.study.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer {

 public static void main(String[] args) throws IOException, TimeoutException {
     ConnectionFactory factory = new ConnectionFactory();
     factory.setUsername("guest");
     factory.setPassword("guest");
     factory.setHost("localhost");
     //建立到代理服务器到连接
     Connection conn = factory.newConnection();
     //获得信道
     final Channel channel = conn.createChannel();
     //声明交换器
     String exchangeName = "hello-exchange";
     channel.exchangeDeclare(exchangeName, "direct", true);
     //声明队列
     String queueName = channel.queueDeclare().getQueue();
     String routingKey = "hola";
     //绑定队列,通过键 hola 将队列和交换器绑定起来
     channel.queueBind(queueName, exchangeName, routingKey);
while(true) {
         //消费消息
         boolean autoAck = false;
         String consumerTag = "";
         channel.basicConsume(queueName, autoAck, consumerTag, new DefaultConsumer(channel) {
             @Override
             public void handleDelivery(String consumerTag,
                                        Envelope envelope,
                                        AMQP.BasicProperties properties,
                                        byte[] body) throws IOException {
                 String routingKey = envelope.getRoutingKey();
                 String contentType = properties.getContentType();
                 System.out.println("消费的路由键:" + routingKey);
                 System.out.println("消费的内容类型:" + contentType);
                 long deliveryTag = envelope.getDeliveryTag();
                 //确认消息
                 channel.basicAck(deliveryTag, false);
                 System.out.println("消费的消息体内容:");
                 String bodyStr = new String(body, "UTF-8");
                 System.out.println(bodyStr);

             }
         });
     }
 }
}

4.启动 RabbitMQ 服务器

./sbin/rabbitmq-server

5.运行 Consumer

先运行 Consumer ,这样当生产者发送消息的时候能在消费者后端看到消息记录。

 

6.运行 Producer

接着运行 Producer ,发布一条消息,在 Consumer 的控制台能看到接收的消息



RabbitMQ 集群

RabbitMQ 最优秀的功能之一就是内建集群,这个功能设计的目的是允许消费者和生产者在节点崩溃的情况下继续运行,以及通过添加更多的节点来线性扩展消息通信吞吐量。RabbitMQ 内部利用 Erlang 提供的分布式通信框架 OTP 来满足上述需求,使客户端在失去一个 RabbitMQ 节点连接的情况下,还是能够重新连接到集群中的任何其他节点继续生产、消费消息。

 

RabbitMQ 集群中的一些概念

RabbitMQ 会始终记录以下四种类型的内部元数据:

  1. 队列元数据
    包括队列名称和它们的属性,比如是否可持久化,是否自动删除
  2. 交换器元数据
    交换器名称、类型、属性
  3. 绑定元数据
    内部是一张表格记录如何将消息路由到队列
  4. vhost 元数据
    为 vhost 内部的队列、交换器、绑定提供命名空间和安全属性

在单一节点中,RabbitMQ 会将所有这些信息存储在内存中,同时将标记为可持久化的队列、交换器、绑定存储到硬盘上。存到硬盘上可以确保队列和交换器在节点重启后能够重建。而在集群模式下同样也提供两种选择:存到硬盘上(独立节点的默认设置),存在内存中。

 

如果在集群中创建队列,集群只会在单个节点而不是所有节点上创建完整的队列信息(元数据、状态、内容)。结果是只有队列的所有者节点知道有关队列的所有信息,因此当集群节点崩溃时,该节点的队列和绑定就消失了,并且任何匹配该队列的绑定的新消息也丢失了。还好RabbitMQ 2.6.0之后提供了镜像队列以避免集群节点故障导致的队列内容不可用。

 

RabbitMQ 集群中可以共享 user、vhost、exchange等,所有的数据和状态都是必须在所有节点上复制的,例外就是上面所说的消息队列。RabbitMQ 节点可以动态的加入到集群中。

 

当在集群中声明队列、交换器、绑定的时候,这些操作会直到所有集群节点都成功提交元数据变更后才返回。集群中有内存节点和磁盘节点两种类型,内存节点虽然不写入磁盘,但是它的执行比磁盘节点要好。内存节点可以提供出色的性能,磁盘节点能保障配置信息在节点重启后仍然可用,那集群中如何平衡这两者呢?

 

RabbitMQ 只要求集群中至少有一个磁盘节点,所有其他节点可以是内存节点,当节点加入火离开集群时,它们必须要将该变更通知到至少一个磁盘节点。如果只有一个磁盘节点,刚好又是该节点崩溃了,那么集群可以继续路由消息,但不能创建队列、创建交换器、创建绑定、添加用户、更改权限、添加或删除集群节点。换句话说集群中的唯一磁盘节点崩溃的话,集群仍然可以运行,但知道该节点恢复,否则无法更改任何东西。

 

RabbitMQ 集群配置和启动

如果是在一台机器上同时启动多个 RabbitMQ 节点来组建集群的话,只用上面介绍的方式启动第二、第三个节点将会因为节点名称和端口冲突导致启动失败。所以在每次调用 rabbitmq-server 命令前,设置环境变量 RABBITMQ_NODENAME 和 RABBITMQ_NODE_PORT 来明确指定唯一的节点名称和端口。下面的例子端口号从5672开始,每个新启动的节点都加1,节点也分别命名为test_rabbit_1、test_rabbit_2、test_rabbit_3。

 

启动第1个节点:

RABBITMQ_NODENAME=test_rabbit_1 RABBITMQ_NODE_PORT=5672 ./sbin/rabbitmq-server -detached

启动第2个节点:

RABBITMQ_NODENAME=test_rabbit_2 RABBITMQ_NODE_PORT=5673 ./sbin/rabbitmq-server -detached

启动第2个节点前建议将 RabbitMQ 默认激活的插件关掉,否则会存在使用了某个插件的端口号冲突,导致节点启动不成功。

 

现在第2个节点和第1个节点都是独立节点,它们并不知道其他节点的存在。集群中除第一个节点外后加入的节点需要获取集群中的元数据,所以要先停止 Erlang 节点上运行的 RabbitMQ 应用程序,并重置该节点元数据,再加入并且获取集群的元数据,最后重新启动 RabbitMQ 应用程序。

 

停止第2个节点的应用程序:

./sbin/rabbitmqctl -n test_rabbit_2 stop_app

重置第2个节点元数据:

./sbin/rabbitmqctl -n test_rabbit_2 reset

第2节点加入第1个节点组成的集群:

./sbin/rabbitmqctl -n test_rabbit_2 join_cluster test_rabbit_1@localhost

 

启动第2个节点的应用程序:

./sbin/rabbitmqctl -n test_rabbit_2 start_app

第3个节点的配置过程和第2个节点类似:

RABBITMQ_NODENAME=test_rabbit_3 RABBITMQ_NODE_PORT=5674 ./sbin/rabbitmq-server -detached

./sbin/rabbitmqctl -n test_rabbit_3 stop_app

./sbin/rabbitmqctl -n test_rabbit_3 reset

./sbin/rabbitmqctl -n test_rabbit_3 join_cluster test_rabbit_1@localhost

./sbin/rabbitmqctl -n test_rabbit_3 start_app
RabbitMQ 集群运维

停止某个指定的节点,比如停止第2个节点:

RABBITMQ_NODENAME=test_rabbit_2 ./sbin/rabbitmqctl stop

查看节点3的集群状态:

./sbin/rabbitmqctl -n test_rabbit_3 cluster_status 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326467837&siteId=291194637