為了實現高可用,我采用LVS+雙節點RabbitMq , 架構圖如下:
在RabbitMQ之前放了LVS, LVS 采用 rr 輪詢算法 , 目的是將請求平均分配到兩個真實節點,並配置5672端口監控,異常時轉移到另外的節點。
在 ,做完之后測試發現,用上LVS之后,測試會報錯,服務器端的隊列名稱都是一致的,但是隊列內容卻並不是一致,看來需要做同步。
Rabbit提供鏡像功能,需要基於rabbitmq策略來實現,政策是用來控制和修改群集范圍的某個vhost隊列行為和Exchange行為 :
set_policy [-p vhostpath] {name} {pattern} {definition} [priority]
eg.
1 rabbitmqctl set_policy ha-allqueue "^" '{"ha-mode":"all"}'
pattern 是匹配隊列名稱的正則表達式 , 進行區分哪些隊列使用哪些策略
definition 其實就是一些arguments, 支持如下參數:
ha-mode:
One ofall
,exactly
ornodes
(the latter currently not supported by web UI).ha-params:
Absent ifha-mode
isall
, a number ifha-mode
isexactly
, or an array of strings ifha-mode
isnodes
.ha-sync-mode:
One ofmanual
orautomatic
. //如果不指定該參數默認為manual,這個在高可用集群測試的時候詳細分析federation-upstream-set:
A string; only if the federation plugin is enabled.
ha-mode 的參數:
ha-mode | ha-params | Result |
---|---|---|
all | (absent) | Queue is mirrored across all nodes in the cluster. When a new node is added to the cluster, the queue will be mirrored to that node. |
exactly | count | Queue is mirrored to count nodes in the cluster. If there are less than count nodes in the cluster, the queue is mirrored to all nodes. If there are more than countnodes in the cluster, and a node containing a mirror goes down, then a new mirror will not be created on another node. (This is to prevent queues migrating across a cluster as it is brought down.) |
nodes | node names | Queue is mirrored to the nodes listed in node names. If any of those node names are not a part of the cluster, this does not constitute an error. If none of the nodes in the list are online at the time when the queue is declared then the queue will be created on the node that the declaring client is connected to. |
在管理policy的時候WebUI是非常不錯:
Definition加入兩項:
ha-mode:all
ha-sync-mode:automatic
到這里配置已經完成,接下來進行測試。
兩個節點之間就會開始同步消息了。
這時借助前面的LVS / HA 就可以使用高可用了 。