1. 程式人生 > >RabbitMQ單機多例項配置

RabbitMQ單機多例項配置

由於某些因素的限制,有時候你不得不在一臺機器上去搭建一個rabbitmq叢集,當然這種叢集只適合自己玩玩,驗證下結論,這個有點類似zookeeper的單機版。真實生成環境還是要配成多機叢集的。有關怎麼配置多機叢集的可以參考其他的資料,這裡主要論述如何在單機中配置多個rabbitmq例項。

前提
配置的前提是你的rabbitmq可以執行起來,比如”ps aux|grep rabbitmq”你能看到相關程序,又比如執行“rabbitmqctl status”你可以看到類似如下資訊,而不報錯:

[[email protected] rabbitmq]# rabbitmqctl status
Status of node '[email protected]' ... [{pid,13014}, {running_applications,[{rabbit,"RabbitMQ","3.4.0"}, {mnesia,"MNESIA CXC 138 12","4.14.1"}, {os_mon,"CPO CXC 138 46","2.4.1"}, {xmerl,"XML parser","1.3.12"}, {sasl,"SASL CXC 138 11"
,"3.0.1"}, {stdlib,"ERTS CXC 138 10","3.1"}, {kernel,"ERTS CXC 138 10","5.1"}]}, {os,{unix,linux}}, {erlang_version,"Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:30] [hipe] [kernel-poll:true]\n"}, {memory,[{total,37276536}, {connection_readers,0
}, {connection_writers,0}, {connection_channels,0}, {connection_other,2832}, {queue_procs,2832}, {queue_slave_procs,0}, {plugins,0}, {other_proc,13331064}, {mnesia,66248}, {mgmt_db,0}, {msg_index,41184}, {other_ets,778528}, {binary,23152}, {code,14637029}, {atom,654241}, {other_system,7739426}]}, {alarms,[]}, {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,3301929779}, {disk_free_limit,50000000}, {disk_free,37108654080}, {file_descriptors,[{total_limit,924}, {total_used,3}, {sockets_limit,829}, {sockets_used,1}]}, {processes,[{limit,1048576},{used,126}]}, {run_queue,0}, {uptime,2143}]

為了簡單化,這裡也要保證rabbitmq的plugin沒有開啟(因為開啟之後要佔用一些埠,多例項配置起來會更加複雜,這裡簡單問題簡化說明)。

情景
假設有三個rabbitmq節點,分別為rabbit1, rabbit2和rabbit3
主要開啟命令如下:

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

結束命令如下:

rabbitmqctl -n rabbit1 stop
rabbitmqctl -n rabbit2 stop
rabbitmqctl -n rabbit3 stop

rabbit1

[[email protected] rabbitmq]# rabbitmqctl -n rabbit1 stop_app
Stopping node '[email protected]' ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit1 reset
Resetting node '[email protected]' ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit1 cluster 
Clustering node '[email protected]' with [] ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit1 start_app
Starting node '[email protected]' ...
...done.

TIPS
有些版本(比如3.4.0)在第一個節點(主節點)執行“rabbitmqctl -n rabbit1 cluster” 時會遇到:

Error: could not recognise command

的錯誤,可以不執行“rabbitmqctl -n rabbit1 cluster”這句。然後在從節點執行:

rabbitmqctl -n rabbit2 join_cluster [email protected]`hostname -s`

這一句。
主要原因是:有些版本不識別cluster這個命令

rabbit2

[[email protected] rabbitmq]# rabbitmqctl -n rabbit2 stop_app
Stopping node '[email protected]' ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit2 reset
Resetting node '[email protected]' ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit2 cluster [email protected]`hostname -s`
Clustering node '[email protected]' with ['[email protected]'] ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit2 start_app
Starting node '[email protected]' ...
...done.

TIPS
如果略去“rabbitmqctl -n rabbit2 reset”這一句,可能會報錯:

Error: {ok,already_member}

檢視cluster資訊

[[email protected] rabbitmq]# rabbitmqctl -n rabbit1 cluster_status
Cluster status of node '[email protected]' ...
[{nodes,[{disc,['[email protected]']},
         {ram,['[email protected]']}]},
 {running_nodes,['[email protected]','[email protected]']}]
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit2 cluster_status
Cluster status of node '[email protected]' ...
[{nodes,[{disc,['[email protected]']},
         {ram,['[email protected]']}]},
 {running_nodes,['[email protected]','[email protected]']}]
...done.

加入節點rabbit3

[[email protected] rabbitmq]# RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3 rabbitmq-server -detached
Activating RabbitMQ plugins ...
0 plugins activated:

[[email protected] rabbitmq]# rabbitmqctl -n rabbit3 stop_app
Stopping node '[email protected]' ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit3 reset
Resetting node '[email protected]' ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit3 cluster [email protected]`hostname -s`
Clustering node '[email protected]' with ['[email protected]'] ...
...done.
[[email protected] rabbitmq]# rabbitmqctl -n rabbit3 start_app
Starting node '[email protected]' ...
...done.

最終檢視叢集狀態

[[email protected] rabbitmq]# rabbitmqctl -n rabbit3 cluster_status
Cluster status of node '[email protected]' ...
[{nodes,[{disc,['[email protected]']},
         {ram,['[email protected]','[email protected]']}]},
 {running_nodes,['[email protected]','[email protected]',
                 '[email protected]']}]
...done.

TIPS
有可能遇到這樣的情況:

[[email protected] rabbitmq]# rabbitmqctl status -n rabbit2
Status of node '[email protected]' ...
Error: {badarith,[{rabbit_vm,bytes,1,[]},
                  {rabbit_vm,'-mnesia_memory/0-lc$^0/1-0-',1,[]},
                  {rabbit_vm,mnesia_memory,0,[]},
                  {rabbit_vm,memory,0,[]},
                  {rabbit,status,0,[]},
                  {rpc,'-handle_call_call/6-fun-0-',5,
                       [{file,"rpc.erl"},{line,187}]}]}

有一種解決版本是將“/var/lib/rabbitmq/mnesia/”目錄下的所有內容刪掉(rm -rf *),然後重新啟動再配置。

And….
很多時候也會遇到這樣的情況:

[[email protected] rabbitmq]# rabbitmqctl -n rabbit2 status
Status of node '[email protected]' ...
Error: unable to connect to node '[email protected]': nodedown
diagnostics:
- nodes and their ports on hiddenzhu-8drdc: [{rabbit1,44494},
                                             {rabbitmqctl2271,60458}]
- current node: '[email protected]'
- current node home dir: /root
- current node cookie hash: VCwbL3S9/ydrGgVsrLjVkA==

這說明rabbitmq節點並未啟動起來,需要進一步檢視排除異常。

End…

歡迎支援《RabbitMQ實戰指南》以及關注微信公眾號:朱小廝的部落格。