由于某些因素的限制,有时候你不得不在一台机器上去搭建一个rabbitmq集群,当然这种集群只适合自己玩玩,验证下结论,这个有点类似zookeeper的单机版。真实生成环境还是要配成多机集群的。有关怎么配置多机集群的可以参考其他的资料,这里主要论述如何在单机中配置多个rabbitmq实例。
主要参考官方文档:https://www.rabbitmq.com/clustering.html
前提
配置的前提是你的rabbitmq可以运行起来,比如”ps aux|grep rabbitmq”你能看到相关进程,又比如运行“rabbitmqctl status”你可以看到类似如下信息,而不报错:
[root@hiddenzhu-8drdc rabbitmq]
# rabbitmqctl status
Status of node
'rabbit@hiddenzhu-8drdc' ...
[{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
[root@hiddenzhu-8drdc rabbitmq]
# rabbitmqctl -n rabbit1 stop_app
Stopping node
'rabbit1@hiddenzhu-8drdc' ...
...done.
[root@hiddenzhu-8drdc rabbitmq]
# rabbitmqctl -n rabbit1 reset
Resetting node
'rabbit1@hiddenzhu-8drdc' ...
...done.
[root@hiddenzhu-8drdc rabbitmq]
# rabbitmqctl -n rabbit1 cluster
Clustering node
'rabbit1@hiddenzhu-8drdc' with []
...
...done.
[root@hiddenzhu-8drdc rabbitmq]
# rabbitmqctl -n rabbit1 start_app
Starting node
'rabbit1@hiddenzhu-8drdc' ...
...done.
TIPS
有些版本(比如3.4.0)在第一个节点(主节点)运行“rabbitmqctl -n rabbit1 cluster” 时会遇到:
Error: could
not recognise
command
的错误,可以不运行“rabbitmqctl -n rabbit1 cluster”这句。然后在从节点运行:
rabbitmqctl
-n rabbit2 join_cluster rabbit1@
`hostname -s`
这一句。
主要原因是:有些版本不识别cluster这个命令