RabbitMQ 集群搭建

本文記錄RabbitMQ集群搭建過程中遇到的問題

環(huán)境

vmware12 虛擬機(jī),CentOS7
本文以兩臺(tái)CentOS 為例,IP 192.168.32.128,192.168.32.129

磁盤節(jié)點(diǎn)和內(nèi)存節(jié)點(diǎn)

集群中,每一個(gè)RabbitMQ實(shí)例都是一個(gè)節(jié)點(diǎn),而節(jié)點(diǎn)分為磁盤節(jié)點(diǎn)和內(nèi)存節(jié)點(diǎn)

  • 內(nèi)存節(jié)點(diǎn):將Rabbit中的元數(shù)據(jù)(Queue, Exchange, binding, vhost等)存儲(chǔ)在內(nèi)存中,持久化的 Message 依舊保存在磁盤中,內(nèi)存節(jié)點(diǎn)的性能只能體現(xiàn)在資源管理上,消息的發(fā)送和接收和磁盤節(jié)點(diǎn)沒有區(qū)別

  • 磁盤節(jié)點(diǎn):元數(shù)據(jù)存儲(chǔ)在磁盤中,一個(gè)Rabbit集群要求至少有一個(gè)磁盤節(jié)點(diǎn),因?yàn)閮?nèi)存節(jié)點(diǎn)中不存儲(chǔ)元數(shù)據(jù),所以每次內(nèi)存節(jié)點(diǎn)啟動(dòng),都會(huì)從其他節(jié)點(diǎn)中同步元數(shù)據(jù)

另外如果唯一磁盤的磁盤節(jié)點(diǎn)崩潰了,不能進(jìn)行如下操作:

  • 不能創(chuàng)建隊(duì)列
  • 不能創(chuàng)建交換器
  • 不能創(chuàng)建綁定
  • 不能添加用戶
  • 不能更改權(quán)限
  • 不能添加和刪除集群幾點(diǎn)

RabbitMQ集群的幾種類型

  • 單一模式:僅有一個(gè)rabbit實(shí)例

  • 普通模式:默認(rèn)集群模式,每個(gè)節(jié)點(diǎn)各自維護(hù)自己的數(shù)據(jù),兩個(gè)節(jié)點(diǎn)僅存有相同的元數(shù)據(jù)。例如RabbitA 和 RabbitB,A中存在 QueueA,消費(fèi)者可以從RabbitB實(shí)例中,讀取QueueA的消息,這時(shí)RabbitB會(huì)從A中讀取消息,返回給消費(fèi)者。但是如果RabbitA 宕機(jī),這時(shí)就無法獲取QueueA的數(shù)據(jù)了

  • 鏡像模式:Rabbit 會(huì)將數(shù)據(jù)同步到其他節(jié)點(diǎn)中,這固然提高了可用性,但是隨之而來的問題是,系統(tǒng)的性能會(huì)降低。節(jié)點(diǎn)之間消息的傳遞會(huì)占用帶寬,而每個(gè)節(jié)點(diǎn)存儲(chǔ)的數(shù)據(jù)量會(huì)變大

普通模式

我們先來搭建普通模式的集群,在RabbitMQ中,每個(gè)節(jié)點(diǎn)的名必須是唯一的,默認(rèn)以 rabbit@hostname為節(jié)點(diǎn)name

而每臺(tái)虛擬機(jī)中默認(rèn)的host是localhost,這就導(dǎo)致了每個(gè)節(jié)點(diǎn)的name都是 rabbit@localhost

1. 修改hosts

修改兩臺(tái)主機(jī)的hosts文件,以下是129的配置,我們把128定義為F,129定義為G

cat /etc/hosts
127.0.0.1 G
::1       G

192.168.32.129 G
192.168.32.128 F

保證F和G可以ping通

image.png
2. 安裝RabbitMQ

可以參考 https://www.linuxprobe.com/install-rabbitmq-on-centos-7.html

當(dāng)時(shí)安裝完一直發(fā)現(xiàn)無法訪問 Rabbit的web控制臺(tái),除了開啟插件,和添加一個(gè)用戶之外,還需要放開防火墻的端口

firewall-cmd --add-port=4369/tcp --permanent
firewall-cmd --add-port=25672/tcp --permanent
firewall-cmd --add-port=5671-5672/tcp --permanent
firewall-cmd --add-port=15672/tcp --permanent
firewall-cmd --add-port=61613-61614/tcp --permanent
firewall-cmd --add-port=1883/tcp --permanent
firewall-cmd --add-port=8883/tcp --permanent
firewall-cmd --reload # 重載配置
3. 同步Erlang cookie

Erlang VM將嘗試在RabbitMQ服務(wù)器啟動(dòng)時(shí)創(chuàng)建一個(gè)隨機(jī)生成的值(Erlang Cookie)。集群環(huán)境中,所有節(jié)點(diǎn)的Erlang Cookie必須一致。

.erlang.cookie 通常在 $HOME下或者/var/lib/rabbitmq下

# 找到其中一臺(tái)主機(jī)的 .erlang.cookie
# 修改權(quán)限
chmod 777 /var/lib/rabbitmq/.erlang.cookie
# 拷貝到另一臺(tái)主機(jī)
scp /var/lib/rabbitmq/.erlang.cookie G:/var/lib/rabbitmq/
# 再把權(quán)限修改回來
chmod 400 /var/lib/rabbitmq/.erlang.cookie
4. 檢查節(jié)點(diǎn)名
image.png

如果兩個(gè)節(jié)點(diǎn)都是 rabbit@localhost,這是無法建立集群的

我當(dāng)時(shí)就是遇到了這個(gè)問題,需要修改節(jié)點(diǎn)name,參考 https://ubuntuqa.com/article/6619.html

我采取的方法:

vi /etc/rabbitmq/rabbitmq-env.conf

# 添加如下配置
NODENAME=rabbit@G

# 保存后,重啟rabbitmq 生效
service rabbitmq-server restart
5. 組成集群

上述步驟都成功后,我們可以組成集群了

這個(gè)時(shí)候我們先啟動(dòng)rabbitmq@F,然后rabbit@G執(zhí)行以下操作,加入F,組成集群

rabbitmqctl stop_app # 停止rabbitmq服務(wù)
rabbitmqctl reset # 清空節(jié)點(diǎn)狀態(tài)
rabbitmqctl join_cluster rabbit@F # 加入F,組成集群
rabbitmqctl start_app 

集群中,任意節(jié)點(diǎn)停機(jī)后,執(zhí)行 rabbitmqctl start_app 即可再次加入集群

查看集群狀態(tài) rabbitmqctl cluster_status

image.png

鏡像模式

任意rabbit節(jié)點(diǎn)輸入命令,將所有隊(duì)列,同步到所有節(jié)點(diǎn)中

rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
rabbitmqctl set_policy [-p Vhost] Name Pattern Definition [Priority]

-p Vhost: 可選參數(shù),針對(duì)指定vhost下的queue進(jìn)行設(shè)置
Name: policy的名稱
Pattern: queue的匹配模式(正則表達(dá)式)
Definition:鏡像定義,包括三個(gè)部分ha-mode, ha-params, ha-sync-mode
        ha-mode:指明鏡像隊(duì)列的模式,有效值為 all/exactly/nodes
        all:表示在集群中所有的節(jié)點(diǎn)上進(jìn)行鏡像
        exactly:表示在指定個(gè)數(shù)的節(jié)點(diǎn)上進(jìn)行鏡像,節(jié)點(diǎn)的個(gè)數(shù)由ha-params指定
        nodes:表示在指定的節(jié)點(diǎn)上進(jìn)行鏡像,節(jié)點(diǎn)名稱通過ha-params指定
        ha-params:ha-mode模式需要用到的參數(shù)
        ha-sync-mode:進(jìn)行隊(duì)列中消息的同步方式,有效值為automatic和manual
priority:可選參數(shù),policy的優(yōu)先級(jí)

關(guān)于鏡像模式策略的更多請(qǐng)參考官方文檔:https://www.rabbitmq.com/ha.html

集群的負(fù)載均衡

為什么有了集群還需要負(fù)載均衡?

這里我糾結(jié)了好幾天,原因是,在我的理解里 集群 == 負(fù)載均衡,這樣的理解是有問題的。
正解:分布式集群保證的是高可用,而并不是負(fù)載均衡。

rabbitmq文檔中,也建議我們使用TCP負(fù)載均衡器,這樣也不需要在我們應(yīng)用程序里管理集群的地址

Connecting to Clusters from Clients
A client can connect as normal to any node within a cluster. If that node should fail, and the rest of the cluster survives, then the client should notice the closed connection, and should be able to reconnect to some surviving member of the cluster. Generally, it's not advisable to bake in node hostnames or IP addresses into client applications: this introduces inflexibility and will require client applications to be edited, recompiled and redeployed should the configuration of the cluster change or the number of nodes in the cluster change. Instead, we recommend a more abstracted approach: this could be a dynamic DNS service which has a very short TTL configuration, or a plain TCP load balancer, or some sort of mobile IP achieved with pacemaker or similar technologies. In general, this aspect of managing the connection to nodes within a cluster is beyond the scope of RabbitMQ itself, and we recommend the use of other technologies designed specifically to solve these problems.

網(wǎng)上比較多的方案是使用 HAProxy 作為負(fù)載均衡器,這里大家可以參考一下這一篇 HAProxy從零開始到掌握 具體安裝配置的細(xì)節(jié)本文就不說了

這里我又添加了一臺(tái) 130 的虛擬機(jī)來跑 HAProxy

貼一下我的 haproxy.cfg,僅供參考

global
daemon
pidfile /home/ha/haproxy/conf/haproxy.pid
log 127.0.0.1 local2

defaults
mode tcp
maxconn 10000
timeout connect 5s
timeout client 100s
timeout server 100s

frontend http-in
    bind *:5670
    maxconn 30000
    default_backend default_servers

backend default_servers
    balance roundrobin
    server F 192.168.32.128:5672 check inter 2000 rise 2 fall 3 weight 1
    server G 192.168.32.129:5672 check inter 2000 rise 2 fall 3 weight 1

listen stats
    bind *:1936                 
    mode http
    stats refresh 30s             #每30秒更新監(jiān)控?cái)?shù)據(jù)
    stats uri /stats              #訪問監(jiān)控頁(yè)面的uri
    stats realm HAProxy\ Stats    #監(jiān)控頁(yè)面的認(rèn)證提示
    stats auth admin:admin

做完負(fù)載均衡之后,可以跑一下程序,查看一下負(fù)載均衡的效果,這里我的10個(gè) Connection 按照權(quán)重 1:1 分配在了兩臺(tái) Rabbit 上

image.png

參考

https://www.cnblogs.com/knowledgesea/p/6535766.html
https://www.rabbitmq.com/clustering.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容