RabbitMQ高可用镜像队列

RabbitMQ高可用镜像队列


在分布式系统中,通常使用多个术语来标识主要副本和辅助副本。本指南通常使用“主”来引用队列的主要副本,而对于辅助副本则使用“镜像”。但是,你会发现“master”在这里和那里使用。这是因为RabbitMQ CLI工具历史上一直使用术语“master”来指代辅助。因此这两个术语目前可以互换使用,但我们希望最终摆脱传统的术语。

如何配置镜像

镜像参数是使用策略配置的。策略按名称(使用正则表达式模式)匹配一个或多个队列,并包含添加到匹配队列的整个属性集中的定义(可选参数的映射)。有关策略 的更多信息,请参阅运行时参数和策略。

参考文档:http://www.rabbitmq.com/parameters.html#policies

控制镜像的队列参数

如上所述,队列通过策略启用镜像。政策可以随时更改; 创建一个非镜像的队列是有效的,然后在稍后的某个点上镜像(反之亦然)。非镜像队列和没有任何镜像的镜像队列之间是有区别的 - 前者缺少额外的镜像基础架构,可能会提供更高的吞吐量。

要使队列变成镜像,您需要创建一个与之匹配的策略,并设置策略键ha-mode和(可选)ha-params。

ha-mode ha-params Result
all (absent) 队列镜像在群集中的所有节点上。当新节点添加到群集时,队列将镜像到该节点。.
exactly count 集群中的队列实例数。甲计的1值意味着只是队列主,没有镜子。如果运行队列主节点的节点变为不可用,则队列将变为不可用。甲计的2值表示1个队列主站和1个队列镜。如果运行队列主节点的节点不可用,队列镜像将自动升级为主节点。总之:NumberOfQueueMirrors = NumberOfNodes - 1。如果群集中的计数节点少于该计数节点,则该队列将镜像到所有节点。如果有超过 计数的集群中的节点,以及包含反射镜一个节点出现故障,那么一个新的反射镜将被另一节点上创建。确切的使用模式与“ha-promote-on-shutdown”:“always”可能是危险的,因为队列可以在群集中迁移,并在被关闭时变得不同步。 .
nodes node names 队列镜像到节点名称中列出的节点。节点名称是在出现在rabbitmqctl cluster_status中的Erlang节点名称; 他们通常有“ 兔子@主机名 ” 的形式。如果这些节点名称中的任何一个不是集群的一部分,则这不构成错误。如果在声明队列时列表中没有任何节点在线,则会在声明客户端所连接的节点上创建队列。

例子

以下示例声明名为ha-all的策略,它与名称以"ha"开头的队列相匹配,并将镜像配置到集群中的所有节点.

rabbitmqctl rabbitmqctl set_policy ha-all "^ha." '{"ha-mode":"all"}'
rabbitmqctl (Windows) rabbitmqctl set_policy ha-all "^ha." "{""ha-mode"":""all""}"
HTTP API PUT /api/policies/%2f/ha-all {"pattern":"^ha.", "definition":{"ha-mode":"all"}}
Web UI Navigate to Admin > Policies > Add / update a policy.Enter "ha-all" next to Name, "^ha." next to Pattern, and "ha-mode" = "all" in the first line next to Policy.
Click Add policy.

策略的名称以"two"开始的队列镜像到群集中的任意两个节点,并进行自动同步:

rabbitmqctl rabbitmqctl set_policy ha-two "^two." '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'
rabbitmqctl (Windows) rabbitmqctl set_policy ha-two "^two." ^ "{""ha-mode"":""exactly"",""ha-params"":2,"ha-sync-mode":"automatic"}"
HTTP API PUT /api/policies/%2f/ha-two{"pattern":"^two.", "definition":{"ha-mode":"exactly", "ha-params":2,"ha-sync-mode":"automatic"}}
Web UI Navigate to Admin > Policies > Add / update a policy.Enter "ha-two" next to Name and "^two." next to Pattern.Enter "ha-mode" = "exactly" in the first line next to Policy, then "ha-params" = 2 in the second line, then "ha-sync-mode" = "automatic" in the third, and set the type on the second line to "Number".Click Add policy.

以"node"开头的队列镜像到集群中的特定节点的策略:

rabbitmqctl rabbitmqctl set_policy ha-nodes "^nodes." '{"ha-mode":"nodes","ha-params":["rabbit@nodeA", "rabbit@nodeB"]}'
rabbitmqctl (Windows) rabbitmqctl set_policy ha-nodes "^nodes." ^ "{""ha-mode"":""nodes"",""ha-params"":[""rabbit@nodeA"", ""rabbit@nodeB""]}"
HTTP API PUT /api/policies/%2f/ha-nodes{"pattern":"^nodes.", "definition":
Web UI Navigate to Admin > Policies > Add / update a policy.Enter "ha-nodes" next to Name and "^nodes." next to Pattern.Enter "ha-mode" = "nodes" in the first line next to Policy, then "ha-params" in the second line, set the second line's type to "List", and then enter "rabbit@nodeA" and "rabbit@nodeB" in the sublist which appears.Click Add policy.
posted @ 2017-12-15 09:46  全能程序猿  阅读(3154)  评论(0编辑  收藏  举报