天天看點

rabbitmq實戰(五)——RabbitMQ叢集(鏡像叢集)

可檢視 rabbitmq官方叢集方案

架構圖

This guide covers mirroring (queue contents replication) of classic queues --摘自官網

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 be made mirrored across multiple nodes. --摘自官網

鏡像隊列機制就是将隊列在三個節點之間設定主從關系,消息會在三個節點之間進行自動同步,且如果其中一個節點不可用,并不會導緻消息丢失或服務不可用的情況,提升MQ叢集的整體高可用性。

rabbitmq實戰(五)——RabbitMQ叢集(鏡像叢集)

叢集類型:

  1. 主從副本叢集
  2. 鏡像叢集(常用)

上一章已經介紹主從副本叢集

點選可檢視: rabbitmq實戰(十)——RabbitMQ叢集(主從副本叢集)

本章介紹一下常用的鏡像叢集

鏡像叢集是

基于主從副本叢集的

# 0.政策說明
	rabbitmqctl set_policy [-p <vhost>] [--priority <priority>] [--apply-to <apply-to>] <name> <pattern>  <definition>
	-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的優先級
                
                 
# 1.檢視目前政策
	rabbitmqctl list_policies

# 2.添加政策
	rabbitmqctl set_policy ha-all '^hello' '{"ha-mode":"all","ha-sync-mode":"automatic"}' 
	說明:政策正規表達式為 “^” 表示所有比對所有隊列名稱  ^hello:比對hello開頭隊列

# 3.删除政策
	rabbitmqctl clear_policy ha-all

# 4.測試叢集
           

我們在node1上用指令執行,可以看到node2上同步了政策

rabbitmq實戰(五)——RabbitMQ叢集(鏡像叢集)

繼續閱讀