rabbitmq集群自问自答

382 阅读5分钟

1. 如何实现rabbitmq高可用

服务高可用实现准则服务尽可能少中断、数据尽可能少丢,因此为了达到这一目标,可以通过集群部署来实现

2. rabbitmq集群搭建完成,队列数据是否就高可用了?

2.1 官方描述

By default, contents of a queue within a RabbitMQ cluster are located on a single node (the node on which the queue was declared). This is in contrast to exchanges and bindings, which can always be considered to be on all nodes. Queues can optionally run mirrors (additional replicas) on other cluster nodes.

2.2 翻译

默认情况下,队列中的数据只在创建队列的节点上存在,并不是集群中所有节点都存在。如果希望队列数据在集群中所有节点都存在,需要配置镜像队列

3. 什么是镜像队列

镜像队列就是队列在集群其它节点存在一个或多个镜像副本

4.集群中leadermirrors是否都可以处理消息?

4.1 官方描述

All operations for a given queue are first applied on the queue's leader node and then propagated to mirrors. This involves enqueueing publishes, delivering messages to consumers, tracking acknowledgements from consumers and so on.

4.2 翻译

队列的所有操作都在leader节点上完成,然后将操作同步给镜像节点,也就是说镜像节点只做数据备份,因此可以得出镜像队列并不能提高rabbitmq性能结论。

5.如何配置镜像队列?

5.1 官方描述

To make the classic queues mirrored, create a policy which matches them and sets policy keys ha-mode and (optionally) ha-params.

5.2 翻译

使用ha-modeha-params创建策略并应用到所有队列

ha-modeha-params其它参数值配置可参考Queue Arguments that Control Mirroring

6.如何检测镜像队列配置成功?

6.1 官方描述

Mirrored queues will have a policy name and the number of additional replicas (mirrors) next to it on the queue page in the management UI.

6.2 翻译

队列界面Node列可以看到镜像节点数量,Features列可以看到使用策略名称,则说明镜像队列配置成功

镜像队列配置成功,队列详情界面会列举出镜像节点

7.如何选择镜像节点数量

在创建镜像策略的时候,是不是应该把所有节点都作为镜像节点?来看看官方说明

7.1 官方描述

Mirroring to all nodes is the most conservative option. It will put additional strain on all cluster nodes, including network I/O, disk I/O and disk space usage. Having a replica on every node is unnecessary in most cases.

For clusters of 3 and more nodes it is recommended to replicate to a quorum (the majority) of nodes, e.g. 2 nodes in a 3 node cluster or 3 nodes in a 5 node cluster.

Since some data can be inherently transient or very time sensitive, it can be perfectly reasonable to use a lower number of mirrors for some queues (or even not use any mirroring).

7.2 翻译

通过官方描述可以了解到:镜像到所有节点是最保守的选择,这将会给集群中所有节点带来网络I/O磁盘I/O磁盘空间使用压力

官方建议:镜像节点数量推荐集群节点半数以上,比如3个节点集群,镜像节点数量推荐2;比如5个节点集群,镜像节点数量推荐3

当然并不是所有场景都对数据可靠性有要求,针对可靠性要求不高的场景可以选择不设置镜像队列

8. 如何平衡集群节点负载

第四章节可以了解到镜像队列中只有leader可以处理消息,这同时也就带来一个问题,如果所有的leader都在一个节点中,那么该节点就将承载所有的压力,有没有一种可以平衡集群节点负载的方法?

8.1 官方描述

Every queue in RabbitMQ has a primary replica. That replica is called queue leader (originally "queue master"). All queue operations go through the leader replica first and then are replicated to followers (mirrors). This is necessary to guarantee FIFO ordering of messages.

To avoid some nodes in a cluster hosting the majority of queue leader replicas and thus handling most of the load, queue leaders should be reasonably evenly distributed across cluster nodes.

Queue leaders can be distributed between nodes using several strategies. Which strategy is used is controlled in three ways, namely, using the x-queue-master-locator optional queue argument, setting the queue-master-locator policy key or by defining the queue_master_locator key in the configuration file. Here are the possible strategies and how to set them:

  • Pick the node hosting the minimum number of leaders: min-masters
  • Pick the node the client that declares the queue is connected to: client-local
  • Pick a random node: random

8.2 翻译

添加名称:queue-master-locator,值:min-masters策略,可以将新增的队列添加到leader数最少的节点上,从而达到平衡集群负载效果

9. 镜像策略失效

9.1 新增平衡集群节点负载策略

9.2 新增队列

新增完平衡集群节点负载策略后,会发现队列没有镜像功能了,既不显示镜像节点数量也不显示镜像策略名称,为什么会出现这种现象呢?难道镜像策略平衡集群节点负载策略只能使用一个,没法同时使用?

9.3 官方描述

At most one policy matches a queue or exchange. Since multiple policies can match a single name, a mechanism is needed to resolve such policy conflicts. This mechanism is called policy priorities. Every policy has a a numeric priority associated with it. This priority can be specified when declaring a policy. If not explicitly provided, the priority of 1 will be used.

Matching policies are then sorted by priority and the one with the highest priority will take effect.

9.4 翻译

队列至多只能使用一种策略,当队列匹配到多种策略时,最终只会匹配优先级最高的策略。

根据官方描述得知队列只能使用一种策略,要么使用镜像策略要么使用平衡节点负载策略,无法同时使用两种策略。

如果相同时拥有这两种功能,似乎是件不可能实现的事。

9.5 一种策略多种定义

9.5.1 官方描述

In some cases we might want to apply more than one policy definition to a resource. For example we might need a queue to be federated and has message TTL. At most one policy will apply to a resource at any given time, but we can apply multiple definitions in that policy.

9.5.2 翻译

队列虽然无法同时使用两种策略,但是可以在一种策略中应用多个definitions

9.5.3 定义多个definitions

9.5.4 新增队列查看效果

新增队列后可以看到队列既具有镜像功能并且还均匀分布在集群的各个节点上