1. 程式人生 > >RabbitMQ單機叢集搭建

RabbitMQ單機叢集搭建

搭建前容我先吐槽下,因為第一次接觸rabbitmq,從安裝到啟動到檢視rabbitmq-web頁面,這些都比較簡單,但是關於單機部署多個例項,我按照資料設定,總會遇到各種問題,書本,博文,百度了很多,幾乎千片一律,不過最後還是在谷歌上搜到了蛛絲馬跡, 由於我們使用的版本太新了,所以有些資料自然就過期了!

下面開始正式搭建:

系統:mac
rabbitmq:3.6.9,當前時間最新版。

1.安裝步驟略。

  (建議使用命令brew install rabbitmq, 安裝解除安裝都比較方便)

2.正式啟動時,先了解幾個概念

a. 先啟動預設rabbit
到rabbitmq安裝目錄的sbin目錄下 
$ .
/rabbitmq-server -detached (後臺式啟動,缺點部分錯誤顯示不出來,我就被坑了,可以使用不加【-detached】方式啟動,缺點多開幾個終端視窗。)
b. 檢視狀態
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl status
Status of node [email protected] ...
[{pid,21180},
 {running_applications,
     [{rabbitmq_management_visualiser,"RabbitMQ Visualiser"
,"3.6.9"}, {rabbitmq_management,"RabbitMQ Management Console","3.6.9"}, {rabbitmq_stomp,"RabbitMQ STOMP plugin","3.6.9"}, {rabbitmq_amqp1_0,"AMQP 1.0 support for RabbitMQ","3.6.9"}, {rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.9"}, {rabbitmq_mqtt,"RabbitMQ MQTT Adapter"
,"3.6.9"}, {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.9"}, {rabbit,"RabbitMQ","3.6.9"}, {os_mon,"CPO CXC 138 46","2.4.2"}, {amqp_client,"RabbitMQ AMQP Client","3.6.9"}, {rabbit_common, "Modules shared by rabbitmq-server and rabbitmq-erlang-client", "3.6.9"}, {compiler,"ERTS CXC 138 10","7.0.4"}, {xmerl,"XML parser","1.3.13"}, {cowboy,"Small, fast, modular HTTP server.","1.0.4"}, {ranch,"Socket acceptor pool for TCP protocols.","1.3.0"}, {ssl,"Erlang/OTP SSL application","8.1.1"}, {public_key,"Public key infrastructure","1.4"}, {cowlib,"Support library for manipulating Web protocols.","1.0.2"}, {inets,"INETS CXC 138 49","6.3.6"}, {asn1,"The Erlang ASN1 compiler version 4.0.4","4.0.4"}, {crypto,"CRYPTO","3.7.3"}, {sasl,"SASL CXC 138 11","3.0.3"}, {mnesia,"MNESIA CXC 138 12","4.14.3"}, {syntax_tools,"Syntax tools","2.1.1"}, {stdlib,"ERTS CXC 138 10","3.3"}, {kernel,"ERTS CXC 138 10","5.2"}]}, {os,{unix,darwin}}, {erlang_version, "Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:4:4] [async-threads:64] [hipe] [kernel-poll:true] [dtrace]\n"}, {memory, [{total,64052888}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,2848}, {queue_procs,2848}, {queue_slave_procs,0}, {plugins,2143792}, {other_proc,21306640}, {mnesia,68232}, {metrics,195768}, {mgmt_db,520592}, {msg_index,90240}, {other_ets,2840016}, {binary,149864}, {code,25297221}, {atom,1041593}, {other_system,10586154}]}, {alarms,[]}, **核心點:** {listeners, [{clustering,25672,"::"}, {amqp,5672,"127.0.0.1"}, {mqtt,1883,"::"}, {stomp,61613,"::"}, {http,15672,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,2799457075}, {disk_free_limit,50000000}, {disk_free,175823912960}, {file_descriptors, [{total_limit,7068}, {total_used,2}, {sockets_limit,6359}, {sockets_used,0}]}, {processes,[{limit,1048576},{used,370}]}, {run_queue,0}, {uptime,34586}, {kernel,{net_ticktime,60}}]

一般的文章或者書籍,上來就會讓你執行類似於下面的命令

不過我覺得對於新人來說過於簡陋和不負責任!!!

RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1 rabbitmq-server -detached
RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detached
RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3 rabbitmq-server -detached

如果你按照類似的文章操作,也許你會成功,但我想大多數可能會遇到錯誤提示,而且很棘手,讓人很無語。
官方文件中關於叢集的指導中也是這樣提到
這裡寫圖片描述

我們從剛剛貼出的第一個節點的status 可以看到,這樣的一段記錄

zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl status
Status of node [email protected] ...
....略...

{listeners,
     [{clustering,25672,"::"},
      {amqp,5672,"127.0.0.1"},
      {mqtt,1883,"::"},
      {stomp,61613,"::"},
      {http,15672,"::"}]},

預設啟動的是[email protected]
如果在啟動過程,你不指定hostname,那預設都是localhost,假如你的hosts又沒有配置127.0.0.1 localhost 的話,就會報下面的錯了。

ERROR: epmd error for host localhost: nxdomain (non-existing domain)

3.啟動第一個節點

下面的一張截圖是我配置了hosts,指定了特定hostname啟動的,當然你也可以指定 為localhost。

RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=[email protected]-Pro ./rabbitmq-server

接著上面的,啟動第二個節點,如果我們還是按照

 RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=[email protected]-Pro rabbitmq-server -detached

你可能發現不了什麼,假如你去掉-detached 你會發現,最後你得到了一個這樣的錯誤資訊:崩潰了的資訊。。。

Crash dump is being written to: erl_crash.dump...done

往上翻資訊或者檢視日誌,你會看到這樣的提示:

BOOT FAILED
===========

Error description:
   {could_not_start,rabbitmq_mqtt,
       {{shutdown,
            {failed_to_start_child,'rabbit_mqtt_listener_sup_:::1883',
                {shutdown,
                    {failed_to_start_child,
                        {ranch_listener_sup,{acceptor,{0,0,0,0,0,0,0,0},1883}},
                        {shutdown,
                            {failed_to_start_child,ranch_acceptors_sup,
                                {listen_error,
                                    {acceptor,{0,0,0,0,0,0,0,0},1883},
                                    eaddrinuse}}}}}}},
        {rabbit_mqtt,start,[normal,[]]}}}

那問題很明顯了,埠監聽這個位置 ‘rabbit_mqtt_listener_sup_:::1883出現問題了。
詳細看下:http://www.rabbitmq.com/clustering.html#overview-hostname-requirements
你就會理解了。如何正確啟動第二節點呢?可想而知在埠上要避免衝突,就需要設定一些必要的引數了。

3. 啟動第二個節點

#節點2 
RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt  tcp_listeners [1884]" RABBITMQ_NODENAME=rabbit1 ./rabbitmq-server -detached

如果啟動正常,會顯示下面的提示:

zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt  tcp_listeners [1884]" RABBITMQ_NODENAME=[email protected]-Pro ./rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$

當然,你也可以檢視當前節點狀態,注意不能使用:./rabbitmqctl status,否則會如下所示:

zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl status
Status of node [email protected] ...
Error: unable to connect to node [email protected]: nodedown

DIAGNOSTICS
===========

attempted to contact: [[email protected]]

[email protected]:
  * unable to connect to epmd (port 4369) on localhost: nxdomain (non-existing domain)


current node details:
- node name: '[email protected]'
- home dir: /Users/zhanghuilong
- cookie hash: bolAngw+/LHA74YduKm11w==

這裡需要指定節點名稱:./rabbitmqctl status -n rabbit1

zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl status  -n rabbit1
Status of node '[email protected]' ...
[{pid,41257},
 {running_applications,
     [{rabbitmq_management_visualiser,"RabbitMQ Visualiser","3.6.9"},
     ...

同理你可以啟動節點3,相應的埠號+1即可,只要不衝突就行。


#節點3
RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}] -rabbitmq_stomp tcp_listeners [61615] -rabbitmq_mqtt tcp_listeners [1885]" RABBITMQ_NODENAME=rabbit2 ./rabbitmq-server -detached

4.叢集操作

建議在操作叢集前,先了解叢集架構中的,RabbitMQ元資料的概念,以及叢集中佇列和交換機(器)的行為。—這句屬於廢話,不瞭解誰神經病來搭建叢集。廢話可以略過。

a.把節點rabbit1 加入 rabbit中 

1.停止第二個節點的應用程式

./rabbitmqctl -n [email protected]hanghuilongdeMacBook-Pro stop_app

2.重新設定第二個節點的元資料和狀態為清空狀態。

./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro reset

3.加入第一節點

./rabbitmqctl -n [email protected]-Pro join_cluster [email protected]-Pro

4.重新啟動第二節點

 ./rabbitmqctl -n [email protected]-Pro start_app

以下是具體的過程:

  zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] stop_app
Stopping rabbit application on node '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] reset
Resetting node '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] join_cluster [email protected]
Clustering node '[email protected]' with '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] start_app
Starting node '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$

now,你可以到web端看到叢集已經存在了!!!。。。斯巴拉稀

這裡寫圖片描述

b.把rabbit2加入rabbit

步驟同a的1234,其中注意點是,如果你需要設定第三個叢集節點為記憶體模式,而非磁碟模式,可以參考當前版本的命令提示,在最後加上
–ram 引數

Commands:
    stop [<pid_file>]
    stop_app
    start_app
    wait <pid_file>
    reset
    force_reset
    rotate_logs <suffix>
    hipe_compile <directory>

    join_cluster <clusternode> [--ram]

新增第三節點的完整命令

zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] stop_app
Stopping rabbit application on node '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] reset
Resetting node '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] join_cluster [email protected]nghuilongdeMacBook-Pro --ram
Clustering node '[email protected]' with '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n [email protected] start_app
Starting node '[email protected]' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$

檢視叢集狀態

./rabbitmqctl cluster_status -n [email protected]-Pro
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl cluster_status -n [email protected]
Cluster status of node '[email protected]' ...
[{nodes,[{disc,['[email protected]',
                '[email protected]']},
         {ram,['[email protected]']}]},
 {running_nodes,['[email protected]',
                 '[email protected]',
                 '[email protected]']},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{'[email protected]',[]},
          {'[email protected]',[]},
          {'[email protected]',[]}]}]

到web端看下效果吧:
這裡寫圖片描述

點選節點名稱可以檢視詳細資訊,
這裡寫圖片描述
這裡寫圖片描述
節點2 和節點3 分別被設定為 磁碟 和 記憶體型別。

至此,叢集搭建完畢, 由於我也是初次學習rabbitmq,如果有理解有誤的地方,麻煩給指出來,也好一起交流學習下!