码迷,mamicode.com
首页 > 其他好文 > 详细

Rabbitmq 相关介绍之单机镜像模式集群配置

时间:2017-09-19 23:03:12      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:集群   rabbitmq   镜像队列   

一、镜像模式集群简介

    如果RabbitMQ集群只有一个broker节点,那么该节点的失效将导致整个服务临时性的不可用,并且可能会导致message的丢失(尤其是在非持久化message存储于非持久化queue中的时候)。当然可以将所有的publish的message都设置为持久化的,并且使用持久化的queue,但是这样仍然无法避免由于缓存导致的问题:因为message在发送之后和被写入磁盘并执行fsync之间存在一个虽然短暂但是会产生问题的时间窗。通过publisher的confirm机制能够确保客户端知道哪些message已经存入磁盘,尽管如此,一般不希望遇到因单点故障导致的服务不可用。

如果RabbitMQ集群是由多个broker节点构成的,那么从服务的整体可用性上来讲,该集群对于单点失效是有弹性的,但是同时也需要注意:尽管exchange和binding能够在单点失效问题上幸免于难,但是queue和其上持有的message却不行,这是因为queue及其内容仅仅存储于单个节点之上,所以一个节点的失效表现为其对应的queue不可用。

    引入RabbitMQ的镜像队列机制,将queue镜像到cluster中其他节点之上。在该实现下,如果集群中的一个节点失效了,queue能自动地切换到镜像中的另一个节点以保证服务的可用性。在通常的用法中,针对每一个镜像队列都包含一个master和多个slave,分别对应于不同的节点。slave会准确地按照master执行命令的顺序进行命令执行,故slave与master上维护的状态应该是相同的。除了publish外所有动作都只会向master发送,然后由master将命令执行的结果广播给slave们,故看似从镜像队列中的消费操作实际上是在master上执行的。一旦完成了选中的slave被提升为master的动作,发送到镜像队列的message将不会再丢失:publish到镜像队列的所有消息总是被直接publish到master和所有的slave之上。这样一旦master失效了,message仍然可以继续发送到其他slave上。

    RabbitMQ的镜像队列同时支持publisher confirm和事务两种机制。在事务机制中,只有当前事务在全部镜像queue中执行之后,客户端才会收到Tx.CommitOk的消息。同样的,在publisher confirm机制中,向publisher进行当前message确认的前提是该message被全部镜像所接受了。

二、开始配置

镜像队列是基于普通的集群模式的,所以还是得先配置普通集群,然后才能设置镜像队列,这里不再赘述,可以参考我的另一篇文章http://linuxg.blog.51cto.com/4410110/1965369配置普通集群。

1、首先查看策略
#rabbitmqctl -n rabbit1 list_policies    #以节点rabbit2和rabbit3查询结果是一样的
Listing policies
2、设置镜像队列策略
用法:
set_policy [-p vhost] [--priority priority] [--apply-to apply-to] {name} {pattern} {definition}
      Sets a policy.
      name
         The name of the policy.
      pattern
         The regular expression, which when matches on a given resources causes the policy to apply.
      definition
         The definition of the policy, as a JSON term. In most shells you are very likely to need to quote this.
      priority
         The priority of the policy as an integer. Higher numbers indicate greater precedence. The default is 0.
      apply-to
         Which types of object this policy should apply to - "queues", "exchanges" or "all". The default is "all".
      For example:
         rabbitmqctl set_policy federate-me "^amq." ‘{"federation-upstream-set":"all"}‘
         This command sets the policy federate-me in the default virtual host so that built-in exchanges are federated.
 释义:
-p Vhost:      可选参数,针对指定vhost下的queue进行设置
Name:           policy的名称,可以自定义
Pattern:        queue的匹配模式(正则表达式)
Definition:    镜像定义,包括三个部分ha-mode, ha-params, ha-sync-mode
ha-mode:        指明镜像队列的模式,有效值为 all/exactly/nodes
all:           表示在集群中所有的节点上进行镜像
exactly:       表示在指定个数的节点上进行镜像,节点的个数由ha-params指定
nodes:         表示在指定的节点上进行镜像,节点名称通过ha-params指定
ha-params:     ha-mode模式需要用到的参数
ha-sync-mode:  进行队列中消息的同步方式,有效值为automatic和manual
priority:      可选参数,policy的优先级
ha-promote-on-shutdown: 用来控制选主的行为的,有效值为when-synced,always
3、开始设置:
#rabbitmqctl -n rabbit1 set_policy mirror_queue "^" ‘{"ha-mode":"all","ha-sync-mode":"automatic","ha-promote-on-shutdown":"always"}‘
Setting policy "mirror_queue" for pattern "^" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\",\"ha-promote-on-shutdown\":\"always\"}" with priority "0"

4、再次查看策略
#rabbitmqctl -n rabbit1 list_policies
Listing policies
/       mirror_queue    all     ^       {"ha-mode":"all","ha-sync-mode":"automatic","ha-promote-on-shutdown":"always"}  0
#rabbitmqctl -n rabbit2 list_policies
Listing policies
/       mirror_queue    all     ^       {"ha-mode":"all","ha-sync-mode":"automatic","ha-promote-on-shutdown":"always"}  0
#rabbitmqctl -n rabbit3 list_policies
Listing policies
/       mirror_queue    all     ^       {"ha-mode":"all","ha-sync-mode":"automatic","ha-promote-on-shutdown":"always"}  0

可以通过rabbitmq_management界面查看策略,Admin-->Policies, 如下图:

技术分享

同样的,也可以通过此界面添加policies!

本文出自 “知识体系” 博客,请务必保留此出处http://linuxg.blog.51cto.com/4410110/1966896

Rabbitmq 相关介绍之单机镜像模式集群配置

标签:集群   rabbitmq   镜像队列   

原文地址:http://linuxg.blog.51cto.com/4410110/1966896

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!