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 rabbit@localhost ...
[{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 rabbit@localhost ...
....略...

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

默認啓動的是rabbit@localhost
如果在啓動過程,你不指定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=rabbit@zhanghuilongdeMacBook-Pro ./rabbitmq-server

這裏寫圖片描述
成功後的web:http://127.0.0.1:15672/#/
這裏寫圖片描述

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

 RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit_1@zhanghuilongdeMacBook-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=rabbit1@zhanghuilongdeMacBook-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 rabbit@localhost ...
Error: unable to connect to node rabbit@localhost: nodedown

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

attempted to contact: [rabbit@localhost]

rabbit@localhost:
  * unable to connect to epmd (port 4369) on localhost: nxdomain (non-existing domain)


current node details:
- node name: 'rabbitmq-cli-01@zhanghuilongdeMacBook-Pro'
- home dir: /Users/zhanghuilong
- cookie hash: bolAngw+/LHA74YduKm11w==

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

zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl status  -n rabbit1
Status of node 'rabbit1@zhanghuilongdeMacBook-Pro' ...
[{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

這樣的話你可以通過:http://127.0.0.1:15672/#/http://127.0.0.1:15673/#/http://127.0.0.1:15674/#/
訪問web查看相關節點是否真的存在。下面就是如何實現集羣操作了。

4.集羣操作

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

a.把節點rabbit1 加入 rabbit中 

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

./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro stop_app

2.重新設置第二個節點的元數據和狀態爲清空狀態。

./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro reset

3.加入第一節點

./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro join_cluster rabbit@zhanghuilongdeMacBook-Pro

4.重新啓動第二節點

 ./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro start_app

以下是具體的過程:

  zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro stop_app
Stopping rabbit application on node 'rabbit1@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro reset
Resetting node 'rabbit1@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro join_cluster rabbit@zhanghuilongdeMacBook-Pro
Clustering node 'rabbit1@zhanghuilongdeMacBook-Pro' with 'rabbit@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit1@zhanghuilongdeMacBook-Pro start_app
Starting node 'rabbit1@zhanghuilongdeMacBook-Pro' ...
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 rabbit2@zhanghuilongdeMacBook-Pro stop_app
Stopping rabbit application on node 'rabbit2@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit2@zhanghuilongdeMacBook-Pro reset
Resetting node 'rabbit2@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit2@zhanghuilongdeMacBook-Pro join_cluster rabbit@zhanghuilongdeMacBook-Pro --ram
Clustering node 'rabbit2@zhanghuilongdeMacBook-Pro' with 'rabbit@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$ ./rabbitmqctl -n rabbit2@zhanghuilongdeMacBook-Pro start_app
Starting node 'rabbit2@zhanghuilongdeMacBook-Pro' ...
zhanghuilongdeMacBook-Pro:sbin zhanghuilong$

查看集羣狀態

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

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

點擊節點名稱可以查看詳細信息,
這裏寫圖片描述
這裏寫圖片描述
節點2 和節點3 分別被設置爲 磁盤 和 內存類型。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章