6.1.Classic Queue

予早 2025-02-21 01:08:23
Categories: Tags:

Mirrored Classic Queue

默认情况下,RabbitMQ 集群中队列的内容位于单个节点(声明队列的节点)上。

可以对Classic Queue设置镜像以实现队列高可用,对镜像经典队列的操作会被路由到queue leader执行,queue mirror会与queue leader进行同步,queue leader不可用时,存在最久的queue mirror会被提升为queue leader。

If the node that hosts queue leader fails, the oldest mirror will be promoted to the new leader as long as it’s synchronised. Unsynchronised mirrors can be promoted, too, depending on queue mirroring parameters.

一个节点最多只会有同一个队列的一个副本。

使用ha-mode和ha-params配置镜像,ha-mode指定镜像模式,ha-params指定镜像模式的参数,ha-mode取值如下:

exactly:精确指定队列副本的数量,包括queue leader和queue mirror

all:所有节点有一个队列副本

nodes:指定节点有队列副本

# 使用exactly,majority (N/2+1)

rabbitmqctl set_policy ha-two "^two\." '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'

rabbitmqctl set_policy ha-all "^ha\." '{"ha-mode":"all"}'

rabbitmqctl set_policy ha-nodes "^nodes\." '{"ha-mode":"nodes","ha-params":["rabbit@nodeA", "rabbit@nodeB"]}'

Mirroring of Exclusive Queues

Exclusive queues will be deleted when the connection that declared them is closed. For this reason, it is not useful for an exclusive queue to be mirrored (or a non-durable queue, for that matter) since when the node hosting it goes down, the connection will close and the queue will need to be deleted anyway.

For this reason, exclusive queues are never mirrored (even if they match a policy stating that they should be). They are also never durable (even if declared as such).

Classic Queue Mode

Classic queues operated in lazy mode until RabbitMQ 3.12. It is important to know that from RabbitMQ 3.12 and onwards, this lazy queue mode is ignored. By default, in RabbitMQ 3.12, classic queues work in a similar manner to how classic queues worked with lazy mode running on them. Classic queues in RabbitMQ 3.12 may however keep a small number of messages in memory (up to 2048 at the time of writing) based on the consumption rate.

设置lazy模式

动态设置策略

rabbitmqctl set_policy Lazy "^lazy-queue$" '{"queue-mode":"lazy"}' --apply-to queues

声明队列时设置参数

Map<String, Object> args = new HashMap<String, Object>();
args.put("x-queue-mode", "lazy");
channel.queueDeclare("myqueue", false, false, false, args);

lazy模式性能分析详见https://www.rabbitmq.com/docs/lazy-queues#performance