RabbitMQ集群搭建、镜像队列、实现高可用负载均衡、Federation Exchange、Federation Queue、Shovel
1、環(huán)境準(zhǔn)備
| 192.168.56.20 | conch01 |
| 192.168.56.21 | conch02 |
| 192.168.56.22 | conch03 |
2、安裝 rabbitmq集群
1、之前我們已經(jīng)在conch01上面安裝了一臺(tái)rabbitmq,下面我們只需要在另外兩臺(tái)安裝即可
? ? ? ? conch02、conch03
????????
?
?
2、配置.erlang.cookie
??RabbitMQ 的集群是依賴(lài) erlang 集群,而 erlang 集群是通過(guò)這個(gè) cookie 進(jìn)行通信認(rèn)證的,因此,三臺(tái)機(jī)器的配置文件需要一致
我們將conch01的/var/lib/rabbitmq/.erlang.cookie文件拷貝到其他兩臺(tái)機(jī)器下
scp /var/lib/rabbitmq/.erlang.cookie root@conch02:/var/lib/rabbitmq/.erlang.cookie scp /var/lib/rabbitmq/.erlang.cookie root@conch03:/var/lib/rabbitmq/.erlang.cookie3、啟動(dòng)RabbitMQ服務(wù),順帶啟動(dòng)Erlang虛擬機(jī)和RabbitMQ應(yīng)用服務(wù),三臺(tái)節(jié)點(diǎn)下執(zhí)行
rabbitmq-server -detached4、在節(jié)點(diǎn)2執(zhí)行
# rabbitmqctl stop會(huì)將Erlang虛擬機(jī)關(guān)閉 rabbitmqctl stop_app 只關(guān)閉rabbitmq服務(wù) rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl join_cluster rabbit@conch01 # 只啟動(dòng)rabbitmq服務(wù) rabbitmqctl start_app出現(xiàn)問(wèn)題:
[root@conch03 rabbitmq]# rabbitmqctl stop_app Stopping rabbit application on node rabbit@conch03 ... Error: unable to perform an operation on node 'rabbit@conch03'. Please see diagnostics information and suggestions below.Most common reasons for this are:* Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)* CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)* Target node is not runningIn addition to the diagnostics info below:* See the CLI, clustering and networking guides on https://rabbitmq.com/documentation.html to learn more* Consult server logs on node rabbit@conch03* If target node is configured to use long node names, don't forget to use --longnames with CLI toolsDIAGNOSTICS ===========attempted to contact: [rabbit@conch03]rabbit@conch03:* connected to epmd (port 4369) on conch03* epmd reports node 'rabbit' uses port 25672 for inter-node and CLI tool traffic* TCP connection succeeded but Erlang distribution failed* suggestion: check if the Erlang cookie identical for all server nodes and CLI tools* suggestion: check if all server nodes and CLI tools use consistent hostnames when addressing each other* suggestion: check if inter-node connections may be configured to use TLS. If so, all nodes and CLI tools must do that* suggestion: see the CLI, clustering and networking guides on https://rabbitmq.com/documentation.html to learn moreCurrent node details:* node name: 'rabbitmqcli-3-rabbit@conch03'* effective user's home directory: /var/lib/rabbitmq* Erlang cookie hash: vRfqB6mBzPh2BfPPZife/A==[root@conch03 rabbitmq]#原因:
????????RabbitMQ的erlang.cookie和用戶(hù)的cookie沖突了,需要用rabbitmq的cookie去覆蓋用戶(hù)的cookie。
解決:
????????sudo cp /var/lib/rabbitmq/.erlang.cookie ~/.erlang.cookie
重啟即可
5、在節(jié)點(diǎn)3執(zhí)行
rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl join_cluster rabbit@conch02 rabbitmqctl start_app6、查看集群狀態(tài)
rabbitmqctl cluster_status [root@conch01 ~]# rabbitmqctl cluster_status Cluster status of node rabbit@conch01 ... BasicsCluster name: rabbit@conch01Disk Nodesrabbit@conch01 rabbit@conch02 rabbit@conch03Running Nodesrabbit@conch01 rabbit@conch02 rabbit@conch03Versionsrabbit@conch01: RabbitMQ 3.8.12 on Erlang 23.2.6 rabbit@conch02: RabbitMQ 3.8.12 on Erlang 23.2.6 rabbit@conch03: RabbitMQ 3.8.12 on Erlang 23.2.6Maintenance statusNode: rabbit@conch01, status: not under maintenance Node: rabbit@conch02, status: not under maintenance Node: rabbit@conch03, status: not under maintenanceAlarms(none)Network Partitions(none)ListenersNode: rabbit@conch01, interface: [::], port: 15672, protocol: http, purpose: HTTP API Node: rabbit@conch01, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Node: rabbit@conch01, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0 Node: rabbit@conch02, interface: [::], port: 15672, protocol: http, purpose: HTTP API Node: rabbit@conch02, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Node: rabbit@conch02, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0 Node: rabbit@conch03, interface: [::], port: 15672, protocol: http, purpose: HTTP API Node: rabbit@conch03, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Node: rabbit@conch03, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0Feature flagsFlag: drop_unroutable_metric, state: enabled Flag: empty_basic_get_metric, state: enabled Flag: implicit_default_bindings, state: enabled Flag: maintenance_mode_status, state: enabled Flag: quorum_queue, state: enabled Flag: user_limits, state: enabled Flag: virtual_host_metadata, state: enabled [root@conch01 ~]#7、需要重新設(shè)置用戶(hù)(隨便一臺(tái)機(jī)器執(zhí)行即可)
# 創(chuàng)建賬號(hào) rabbitmqctl add_user admin 123456 # 設(shè)置用戶(hù)角色 rabbitmqctl set_user_tags admin administrator # 設(shè)置用戶(hù)權(quán)限 rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"之后在三個(gè)集群節(jié)點(diǎn)的任意一個(gè)可視化界面登錄均可
8、解除集群節(jié)點(diǎn),node2和node3分別執(zhí)行?
rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl start_app rabbitmqctl cluster_status # 此項(xiàng)命令均在node1上執(zhí)行 rabbitmqctl forget_cluster_node rabbit@對(duì)應(yīng)節(jié)點(diǎn)的hostname 例如:rabbitmqctl forget_cluster_node rabbit@conch023、鏡像隊(duì)列
1、使用鏡像的原因
????????如果RabbitMQ集群中只有一個(gè)Broker節(jié)點(diǎn),那么該節(jié)點(diǎn)的失效將導(dǎo)致整體服務(wù)的臨時(shí)性不可用,并且也可能會(huì)導(dǎo)致消息的丟失。可以將所有消息都設(shè)置為持久化,并且對(duì)應(yīng)隊(duì)列的durable屬性也設(shè)置為true,但是這樣仍然無(wú)法避免由于緩存導(dǎo)致的問(wèn)題:因?yàn)橄⒃诎l(fā)送之后和被寫(xiě)入磁盤(pán)井執(zhí)行刷盤(pán)動(dòng)作之間存在一個(gè)短暫卻會(huì)產(chǎn)生問(wèn)題的時(shí)間窗。通過(guò)publisherconfirm機(jī)制能夠確??蛻?hù)端知道哪些消息己經(jīng)存入磁盤(pán),盡管如此,一般不希望遇到因單點(diǎn)故障導(dǎo)致的服務(wù)不可用。
????????引入鏡像隊(duì)列(Mirror Queue)的機(jī)制,可以將隊(duì)列鏡像到集群中的其他Broker節(jié)點(diǎn)之上,如果集群中的一個(gè)節(jié)點(diǎn)失效了,隊(duì)列能自動(dòng)地切換到鏡像中的另一個(gè)節(jié)點(diǎn)上以保證服務(wù)的可用性。
2、搭建步驟
隨便找一個(gè)節(jié)點(diǎn)添加policy,例如我們?cè)赾onch01節(jié)點(diǎn)
?
在conch01上創(chuàng)建一個(gè)隊(duì)列試試
可以發(fā)現(xiàn)出現(xiàn)了一個(gè)加1,表示有一個(gè)備份
?那么一旦conch01出現(xiàn)宕機(jī),那么備份節(jié)點(diǎn)conch02就可以用了,
我們將conch01節(jié)點(diǎn)停掉再看一下:
發(fā)現(xiàn)重新在節(jié)點(diǎn)3上再次備份一下,保證數(shù)據(jù)不丟失。?
4、實(shí)現(xiàn)高可用負(fù)載均衡
????????HAProxy.提供高可用性、負(fù)載均衡及基于TCPHTTP應(yīng)用的代理,支持虛擬主機(jī),它是免費(fèi)、快速并且可靠的一種解決方案,包括Twitter,Reddit,StackOverflow,GitHub.在內(nèi)的多家知名互聯(lián)網(wǎng)公司在使用。HAProxy實(shí)現(xiàn)了一種事件驅(qū)動(dòng)、單一進(jìn)程模型,此模型支持非常大的井發(fā)連接數(shù)。
?5、Federation Exchange
1、使用原因
????????(broker北京),(broker深圳)彼此之間相距甚遠(yuǎn),網(wǎng)絡(luò)延遲是一個(gè)不得不面對(duì)的問(wèn)題。有一個(gè)在北京的業(yè)務(wù)(Client北京)需要連接(broker北京),向其中的交換器exchangeA.發(fā)送消息,此時(shí)的網(wǎng)絡(luò)延遲很小,(Client北京)可以迅速將消息發(fā)送至exchangeA.中,就算在開(kāi)啟了publisherconfirm.機(jī)制或者事務(wù)機(jī)制的情況下,也可以迅速收到確認(rèn)信息。此時(shí)又有個(gè)在深圳的業(yè)務(wù)(Client深圳)需要向exchangeA發(fā)送消息,那么(Client深圳)(broker北京)之間有很大的網(wǎng)絡(luò)延遲,(Client深圳)將發(fā)送消息至exchangeA會(huì)經(jīng)歷一定的延遲,尤其是在開(kāi)啟了publisherconfirm.機(jī)制或者事務(wù)機(jī)制的情況下,(Client深圳)會(huì)等待很長(zhǎng)的延遲時(shí)間來(lái)接收(broker北京)的確認(rèn)信息,進(jìn)而必然造成這條發(fā)送線程的性能降低,甚至造成一定程度上的阻塞。
????????將業(yè)務(wù)(Client深圳)部署到北京的機(jī)房可以解決這個(gè)問(wèn)題,但是如果(Client深圳)調(diào)用的另些服務(wù)都部署在深圳,那么又會(huì)引發(fā)新的時(shí)延問(wèn)題,總不見(jiàn)得將所有業(yè)務(wù)全部部署在一個(gè)機(jī)房,那么容災(zāi)又何以實(shí)現(xiàn)?這里使用Federation插件就可以很好地解決這個(gè)問(wèn)題.
2、搭建步驟?
需要保證每臺(tái)節(jié)點(diǎn)單獨(dú)運(yùn)行
在每臺(tái)機(jī)器上開(kāi)啟federation相關(guān)插件
?
原理圖
?
在下游節(jié)點(diǎn)(node2)配置上游節(jié)點(diǎn)(node1)
?
添加policy
?
成功
?
6、Federation Queue
1、使用原因
????????聯(lián)邦隊(duì)列可以在多個(gè)Broker節(jié)點(diǎn)(或者集群)之間為單個(gè)隊(duì)列提供均衡負(fù)載的功能。一個(gè)聯(lián)邦隊(duì)列可以連接一個(gè)或者多個(gè)上游隊(duì)列(upstream queue),并從這些上游隊(duì)列中獲取消息以滿(mǎn)足本地消費(fèi)者消費(fèi)消息的需求。?
添加上下游配置(同10.4)
添加policy
?
?
7、Shovel
1、簡(jiǎn)介
????????Federation具備的數(shù)據(jù)轉(zhuǎn)發(fā)功能類(lèi)似,Shovel夠可靠、持續(xù)地從一個(gè)Broker中的隊(duì)列(作為源端,即source)拉取數(shù)據(jù)并轉(zhuǎn)發(fā)至另一個(gè)Broker中的交換器(作為目的端,即destination)。作為源端的隊(duì)列和作為目的端的交換器可以同時(shí)位于同一個(gè)Broker,也可以位于不同的Broker上。Shovel可以翻譯為"鏟子",是一種比較形象的比喻,這個(gè)"鏟子"可以將消息從一方"鏟子"另一方。Shovel行為就像優(yōu)秀的客戶(hù)端應(yīng)用程序能夠負(fù)責(zé)連接源和目的地、負(fù)責(zé)消息的讀寫(xiě)及負(fù)責(zé)連接失敗問(wèn)題的處理。
2、開(kāi)啟插件(需要的機(jī)器都開(kāi)啟)?
rabbitmq-plugins enable rabbitmq_shovel rabbitmq-plugins enable rabbitmq_shovel_management3、添加shevel源和目的地
?
總結(jié)
以上是生活随笔為你收集整理的RabbitMQ集群搭建、镜像队列、实现高可用负载均衡、Federation Exchange、Federation Queue、Shovel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 阿里收购抖音?张一鸣不愿意卖,但可像微博
- 下一篇: 基于ZYNQ+AD936X的开源SDR硬