在分布式系统中,一般使用多个术语来标识主要副本和辅助副本。本指南一般使用“主”来引用队列的主要副本,而对于辅助副本则使用“镜像”。可是,你会发现“master”在这里和那里使用。这是由于RabbitMQ CLI工具历史上一直使用术语“master”来指代辅助。所以这两个术语目前能够互换使用,但咱们但愿最终摆脱传统的术语。html
镜像参数是使用策略配置的。策略按名称(使用正则表达式模式)匹配一个或多个队列,并包含添加到匹配队列的整个属性集中的定义(可选参数的映射)。有关策略 的更多信息,请参阅运行时参数和策略。node
参考文档:http://www.rabbitmq.com/parameters.html#policies正则表达式
如上所述,队列经过策略启用镜像。政策能够随时更改; 建立一个非镜像的队列是有效的,而后在稍后的某个点上镜像(反之亦然)。非镜像队列和没有任何镜像的镜像队列之间是有区别的 - 前者缺乏额外的镜像基础架构,可能会提供更高的吞吐量。api
要使队列变成镜像,您须要建立一个与之匹配的策略,并设置策略键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"开头的队列相匹配,并将镜像配置到集群中的全部节点.app
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"开头的队列镜像到集群中的特定节点的策略:htm
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":{"ha-mode":"nodes", "ha-params":["rabbit@nodeA", "rabbit@nodeB"]} |
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. |