一、叢集搭建概述
摘要:實際生產應用中都會採用訊息佇列的叢集方案,如果選擇RabbitMQ那麼有必要了解下它的叢集方案原理
一般來說,如果只是為了學習RabbitMQ或者驗證業務工程的正確性那麼在本地環境或者測試環境上使用其單例項部署就可以了,但是出於MQ中介軟體本身的可靠性、併發性、吞吐量和訊息堆積能力等問題的考慮,在生產環境上一般都會考慮使用RabbitMQ的叢集方案。
二、叢集方案的原理
RabbitMQ這款訊息佇列中介軟體產品本身是基於Erlang編寫,Erlang語言天生具備分散式特性(透過同步Erlang叢集各節點的magic cookie來實現)。因此,RabbitMQ天然支援Clustering。這使得RabbitMQ本身不需要像ActiveMQ、Kafka那樣透過ZooKeeper分別來實現HA方案和儲存叢集的後設資料。叢集是保證可靠性的一種方式,同時可以透過水平擴充套件以達到增加訊息吞吐量能力的目的。
很多個RabbitMQ節點,節點之間使用映象佇列的方式來同步節點裡的資料。外面用HAProxy進行反向代理。生產者和消費者都訪問HAProxy,然後HAProxy代理訪問各個RabbitMQ節點。
三、單機多例項部署(偽叢集)
由於某些因素的限制,有時候你不得不在一臺機器上去搭建一個rabbitmq叢集,這個有點類似zookeeper的單機版。真實生成環境還是要配成多機叢集的。有關怎麼配置多機叢集的可以參考其他的資料,這裡主要論述如何在單機中配置多個rabbitmq例項。
主要參考官方文件:https://www.rabbitmq.com/clustering.html
首先確保RabbitMQ執行沒有問題
[root@super ~]# rabbitmqctl status Status of node rabbit@super ... [{pid,10232}, {running_applications, [{rabbitmq_management,"RabbitMQ Management Console","3.6.5"}, {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.5"}, {webmachine,"webmachine","1.10.3"}, {mochiweb,"MochiMedia Web Server","2.13.1"}, {rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.5"}, {rabbit,"RabbitMQ","3.6.5"}, {os_mon,"CPO CXC 138 46","2.4"}, {syntax_tools,"Syntax tools","1.7"}, {inets,"INETS CXC 138 49","6.2"}, {amqp_client,"RabbitMQ AMQP Client","3.6.5"}, {rabbit_common,[],"3.6.5"}, {ssl,"Erlang/OTP SSL application","7.3"}, {public_key,"Public key infrastructure","1.1.1"}, {asn1,"The Erlang ASN1 compiler version 4.0.2","4.0.2"}, {ranch,"Socket acceptor pool for TCP protocols.","1.2.1"}, {mnesia,"MNESIA CXC 138 12","4.13.3"}, {compiler,"ERTS CXC 138 10","6.0.3"}, {crypto,"CRYPTO","3.6.3"}, {xmerl,"XML parser","1.3.10"}, {sasl,"SASL CXC 138 11","2.7"}, {stdlib,"ERTS CXC 138 10","2.8"}, {kernel,"ERTS CXC 138 10","4.2"}]}, {os,{unix,linux}}, {erlang_version, "Erlang/OTP 18 [erts-7.3] [source] [64-bit] [async-threads:64] [hipe] [kernel-poll:true]\n"}, {memory, [{total,56066752}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,2680}, {queue_procs,268248}, {queue_slave_procs,0}, {plugins,1131936}, {other_proc,18144280}, {mnesia,125304}, {mgmt_db,921312}, {msg_index,69440}, {other_ets,1413664}, {binary,755736}, {code,27824046}, {atom,1000601}, {other_system,4409505}]}, {alarms,[]}, {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,411294105}, {disk_free_limit,50000000}, {disk_free,13270233088}, {file_descriptors, [{total_limit,924},{total_used,6},{sockets_limit,829},{sockets_used,0}]}, {processes,[{limit,1048576},{used,262}]}, {run_queue,0}, {uptime,43651}, {kernel,{net_ticktime,60}}]
停止rabbitmq服務
[root@super sbin]# service rabbitmq-server stop Stopping rabbitmq-server: rabbitmq-server.
啟動第一個節點:
[root@super sbin]# RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit1 rabbitmq-server start RabbitMQ 3.6.5. Copyright (C) 2007-2016 Pivotal Software, Inc. ## ## Licensed under the MPL. See http://www.rabbitmq.com/ ## ## ########## Logs: /var/log/rabbitmq/rabbit1.log ###### ## /var/log/rabbitmq/rabbit1-sasl.log ########## Starting broker... completed with 6 plugins.
啟動第二個節點:
web管理外掛埠占用,所以還要指定其web外掛佔用的埠號。
[root@super ~]# RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]" RABBITMQ_NODENAME=rabbit2 rabbitmq-server start RabbitMQ 3.6.5. Copyright (C) 2007-2016 Pivotal Software, Inc. ## ## Licensed under the MPL. See http://www.rabbitmq.com/ ## ## ########## Logs: /var/log/rabbitmq/rabbit2.log ###### ## /var/log/rabbitmq/rabbit2-sasl.log ########## Starting broker... completed with 6 plugins.
結束命令:
rabbitmqctl -n rabbit1 stop
rabbitmqctl -n rabbit2 stop
rabbit1操作作為主節點:
[root@super ~]# rabbitmqctl -n rabbit1 stop_app Stopping node rabbit1@super ... [root@super ~]# rabbitmqctl -n rabbit1 reset Resetting node rabbit1@super ... [root@super ~]# rabbitmqctl -n rabbit1 start_app Starting node rabbit1@super ... [root@super ~]#
rabbit2操作為從節點:
[root@super ~]# rabbitmqctl -n rabbit2 stop_app Stopping node rabbit2@super ... [root@super ~]# rabbitmqctl -n rabbit2 reset Resetting node rabbit2@super ... [root@super ~]# rabbitmqctl -n rabbit2 join_cluster rabbit1@'super' ###''內是主機名換成自己的 Clustering node rabbit2@super with rabbit1@super ... [root@super ~]# rabbitmqctl -n rabbit2 start_app Starting node rabbit2@super ...
檢視叢集狀態:
[root@super ~]# rabbitmqctl cluster_status -n rabbit1 Cluster status of node rabbit1@super ... [{nodes,[{disc,[rabbit1@super,rabbit2@super]}]}, {running_nodes,[rabbit2@super,rabbit1@super]}, {cluster_name,<<"rabbit1@super">>}, {partitions,[]}, {alarms,[{rabbit2@super,[]},{rabbit1@super,[]}]}]
web監控:
四、叢集管理
五、