安装rabbitmq cluster:
设置 Erlang Cookie
安装完RabbitMQ之后,在第一台机器上面启动RabbitMQ,然后在停止.复制node1上的/var/lib/rabbitmq/.erlang.cookie文件到node2, node3.
这里将 node1 的该文件复制到 node2、node3,由于这个文件权限是 400,所以需要先修改 node2、node3 中的该文件权限为 777,否则不能复制:
cat /var/lib/rabbitmq/.erlang.cookie chmod 777 /var/lib/rabbitmq/.erlang.cookie ll /var/lib/rabbitmq/.erlang.cookie
然后将 node1 中的该文件拷贝到 node2、node3,最后将权限和所属用户/组修改回来:
chmod 400 /var/lib/rabbitmq/.erlang.cookie chgrp rabbitmq /var/lib/rabbitmq/.erlang.cookie chgrp rabbitmq /var/lib/rabbitmq/.erlang.cookie
设置 Erlang Cookie
如果启动,需要停止:
rabbitmqctl stop或service rabbitmq-server stop
使用 -detached 参数运行各节点
node1$ rabbitmq-server -detached node2$ rabbitmq-server -detached node3$ rabbitmq-server -detached
这样就创建了 3 个独立的 RabbitMQ broker ,每一个 node 上一个,可以通过 cluster_status 命令来确认:
node1$ rabbitmqctl cluster_status
node2$ rabbitmqctl cluster_status
node3$ rabbitmqctl cluster_status
组成集群将 node2、node3 与 node1 组成集群:
node1/2/3:
--node1,node2,node3 node1/2/3$ iptables -I INPUT -p tcp --dport 4369 -j ACCEPT node1/2/3$ iptables -I INPUT -p tcp --dport 25672 -j ACCEPT node1/2/3$ service iptables save node1/2/3$ service iptables restart
node2/3:
--node2 node2$ rabbitmqctl stop_app node2$ rabbitmqctl join_cluster --ram rabbit@node1 node2$ rabbitmqctl start_app --node3 node2$ rabbitmqctl stop_app node2$ rabbitmqctl join_cluster --ram rabbit@node1 node2$ rabbitmqctl start_app
集群配置好后,可以在 RabbitMQ 任意节点上执行 rabbitmqctl cluster_status 来查看是否集群配置成功。
改变 node 的类型
我们可以改变 node 的类型,如磁盘 node 到内存 node ,或者相反。比如将 rabbit@rabbit2 和 rabbit@rabbit3 的 node 类型都变成和之前不同的种类。我们可以使用命令 change_cluster_node_type 来进行转换,但是首先需要将 node 停止。
node2$ rabbitmqctl stop_app
Stopping node rabbit@node2 ...done.
node2$ rabbitmqctl change_cluster_node_type disc
Turning rabbit@node2 into a disc node ...
...done.
node2$ rabbitmqctl start_app
Starting node rabbit@node2 ...done.
node3$ rabbitmqctl stop_app
Stopping node rabbit@node3 ...done.
node3$ rabbitmqctl change_cluster_node_type ram
Turning rabbit@node3 into a ram node ...
node3$ rabbitmqctl start_app
Starting node rabbit@node3 ...done.
设置镜像队列策略在任意一个节点上执行:
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
![](https://images2015.cnblogs.com/blog/105584/201606/105584-20160601113954602-1607209522.png)
重新启动 cluster node
cluster 中的 node 在任何时候都可以被停止。 同样地如果他们崩溃了也是没有任何问题的。在上述两种情况中,cluster 中的其他 node 都可以不受任何影响的继续运行,这些“非正常” node 重新启动后会自动地与 cluster 中的其他 node 取得联系。
rabbit1$ rabbitmqctl stop
Stopping and halting node rabbit@rabbit1 ...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]},{ram,[rabbit@rabbit3]}]},
{running_nodes,[rabbit@rabbit3,rabbit@rabbit2]}]
...done.
rabbit3$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit3 ...
[{nodes,[{disc,[rabbit@rabbit2,rabbit@rabbit1]},{ram,[rabbit@rabbit3]}]},
{running_nodes,[rabbit@rabbit2,rabbit@rabbit3]}]
...done.
rabbit3$ rabbitmqctl stop
Stopping and halting node rabbit@rabbit3 ...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]},{ram,[rabbit@rabbit3]}]},
{running_nodes,[rabbit@rabbit2]}]
...done.
现在我们重新启动 node ,并查看 cluster 的状态:
rabbit1$ rabbitmq-server -detached rabbit1$ rabbitmqctl cluster_status Cluster status of node rabbit@rabbit1 ... [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]},{ram,[rabbit@rabbit3]}]}, {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}] ...done. rabbit2$ rabbitmqctl cluster_status Cluster status of node rabbit@rabbit2 ... [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]},{ram,[rabbit@rabbit3]}]}, {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}] ...done. rabbit3$ rabbitmq-server -detached rabbit1$ rabbitmqctl cluster_status Cluster status of node rabbit@rabbit1 ... [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]},{ram,[rabbit@rabbit3]}]}, {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}] ...done. rabbit2$ rabbitmqctl cluster_status Cluster status of node rabbit@rabbit2 ... [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]},{ram,[rabbit@rabbit3]}]}, {running_nodes,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}] ...done. rabbit3$ rabbitmqctl cluster_status Cluster status of node rabbit@rabbit3 ... [{nodes,[{disc,[rabbit@rabbit2,rabbit@rabbit1]},{ram,[rabbit@rabbit3]}]}, {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}] ...done.
listen rabbitmq_cluster 0.0.0.0:5672 mode tcp balance roundrobin server node1 192.168.1.1:5672 check inter 2000 rise 2 fall 3 server node2 192.168.1.2:5672 check inter 2000 rise 2 fall 3 server node2 192.168.1.3:5672 check inter 2000 rise 2 fall 3
转:http://88250.b3log.org/rabbitmq-clustering-ha(参考)
http://my.oschina.net/moooofly/blog/93548(翻译)
http://www.rabbitmq.com/clustering.html(官网)
http://yupengyan.com/quick-installation-of-haproxy-on-centos6-2.html(Happroxy)