RabbitMQ 集群搭建

2016-07-08|来源:

准备机器:
192.168.56.101 huangyineng
192.168.56.102 slave2
192.168.56.103 slave1


参考《RabbitMQ 环境配置-基于linux》安装好三台RabbitMQ server,然后开启 RabbitMQ 监控插件

[hadoop@huangyineng rabbitmq]$ sbin/rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
 mochiweb
 webmachine
 rabbitmq_web_dispatch
 amqp_client
 rabbitmq_management_agent
 rabbitmq_management


1、了解.erlang.cookie

RabbitMQ nodes and CLI tools (e.g. rabbitmqctl) use a cookie to determine whether they are allowed to communicate with each other. For two nodes to be able to communicate they must have the same shared secret called the Erlang cookie. The cookie is just a string of alphanumeric characters. It can be as long or short as you like. Every cluster node must have the same cookie.

Erlang VM will automatically create a random cookie file when the RabbitMQ server starts up. The easiest way to proceed is to allow one node to create the file, and then copy it to all the other nodes in the cluster.

On Unix systems, the cookie will be typically located in /var/lib/rabbitmq/.erlang.cookie or$HOME/.erlang.cookie.

On Windows, the locations are C:\Users\Current User\.erlang.cookie(%HOMEDRIVE% + %HOMEPATH%\.erlang.cookie) orC:\Documents and Settings\Current User\.erlang.cookie, and C:\Windows\.erlang.cookie for RabbitMQ Windows service. If Windows service is used, the cookie should be placed in both places.

As an alternative, you can insert the option "-setcookie cookie" in the erl call in the rabbitmq-serverand rabbitmqctl scripts.

When the cookie is misconfigured (for example, not identical), RabbitMQ will log errors such as "Connection attempt from disallowed node" and "Could not auto-cluster".


Rabbitmq的集群是依附于erlang的集群来工作的。Erlang的集群中各节点是经由过程一个magic cookie来实现的,这个cookie存放在 $home/.erlang.cookie 中(如本人测试是在 /home/hadoop/.erlang.cookie ),文件是400的权限。所以必须包管各节点cookie对峙一致,不然节点之间就无法通信。


2、同步.erlang.cookie

由于.erlang.cookie的权限是400,不能修改,首先对其他两台机器的.erlang.cookie更改为可修改的权限,如:
[hadoop@slave2 sbin]$ chmod 777 ~/.erlang.cookie
[hadoop@slave1 rabbitmq]$ chmod 777 ~/.erlang.cookie

把另外一台机器的.erlang.cookie 拷贝到上面两台机器,然后把权限改回来
[hadoop@huangyineng rabbitmq]$ scp ~/.erlang.cookie slave1:/home/hadoop/
[hadoop@huangyineng rabbitmq]$ scp ~/.erlang.cookie slave2:/home/hadoop/

[hadoop@slave1 rabbitmq]$ chmod 400 ~/.erlang.cookie
[hadoop@slave2 sbin]$ chmod 400 ~/.erlang.cookie


3、使用 -detached 参数运行各节点

如果rabbitmq server在运行,先停掉
[hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl stop
[hadoop@huangyineng rabbitmq]$ sbin/rabbitmq-server -detached
Warning: PID file not written; -detached was passed.


[hadoop@huangyineng rabbitmq]$ ps -ef | grep rabbit

hadoop    3373     1  5 10:24 ?        00:00:02 /data/dn1/erlang/lib/erlang/erts-8.0/bin/beam -W w -A 64 -P 1048576 -K true -- -root /data/dn1/erlang/lib/erlang -progname erl -- -home /home/hadoop -- -pa /data/dn1/rabbitmq/ebin -noshell -noinput -s rabbit boot -sname rabbit@huangyineng -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/data/dn1/rabbitmq/var/log/rabbitmq/rabbit@huangyineng.log"} -rabbit sasl_error_logger {file,"/data/dn1/rabbitmq/var/log/rabbitmq/rabbit@huangyineng-sasl.log"} -rabbit enabled_plugins_file "/data/dn1/rabbitmq/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/data/dn1/rabbitmq/plugins" -rabbit plugins_expand_dir "/data/dn1/rabbitmq/var/lib/rabbitmq/mnesia/rabbit@huangyineng-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/data/dn1/rabbitmq/var/lib/rabbitmq/mnesia/rabbit@huangyineng" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672 -noshell -noinput

在另外两个机器上同样使用上面的命令操作

使用 rabbitmqctl cluster_status查看每台机器的状态
[ hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@huangyineng ...
[{nodes,[{disc,[ rabbit@huangyineng]}]},
{running_nodes,[ rabbit@huangyineng]},
{cluster_name,<<" rabbit@huangyineng">>},
{partitions,[]},
{alarms,[{ rabbit@huangyineng,[]}]}]
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@slave1 ...
[{nodes,[{disc,[ rabbit@slave1]}]},
{running_nodes,[ rabbit@slave1]},
{cluster_name,<<" rabbit@slave1">>},
{partitions,[]},
{alarms,[{ rabbit@slave1,[]}]}]
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@slave2 ...
[{nodes,[{disc,[ rabbit@slave2]}]},
{running_nodes,[ rabbit@slave2]},
{cluster_name,<<" rabbit@slave2">>},
{partitions,[]},
{alarms,[{ rabbit@slave2,[]}]}]

4、创建集群
把slave1、slave2与huangyineng组成集群
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl stop_app
Stopping node rabbit@slave1 ...
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl join_cluster rabbit@huangyineng
Clustering node rabbit@slave1 with rabbit@huangyineng ...
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl start_app
Starting node rabbit@slave1 ...


在slave2上进行同样操作

[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl stop_app
Stopping node rabbit@slave2 ...
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl join_cluster rabbit@huangyineng
Clustering node rabbit@slave2 with rabbit@huangyineng ...
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl start_app
Starting node rabbit@slave2 ...


使用rabbitmqctl cluster_status在任意一台机器上看到的状态都是一样的

[ hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@huangyineng ...
{cluster_name,<<" rabbit@huangyineng">>},
{partitions,[]},
{alarms,[{ rabbit@slave2,[]},{ rabbit@slave1,[]},{ rabbit@huangyineng,[]}]}]


注:如果要使用内存节点,则可以使用--ram参数加入集群,如:

[hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl join_cluster --ram rabbit@huangyineng


5、重启集群节点

停掉huangyineng节点
[hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl stop
Stopping and halting node rabbit@huangyineng ...

查看集群状态,可以看出运行的节点有 rabbit@slave1,rabbit@slave2
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@slave2 ...
{running_nodes,[ rabbit@slave1, rabbit@slave2]},
{cluster_name,<<" rabbit@huangyineng">>},
{partitions,[]},
{alarms,[{ rabbit@slave1,[]},{ rabbit@slave2,[]}]}]


重启huangyineng节点

[ hadoop@huangyineng rabbitmq]$ sbin/rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[ hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@huangyineng ...
{cluster_name,<<" rabbit@huangyineng">>},
{partitions,[]},
{alarms,[{ rabbit@slave1,[]},{ rabbit@slave2,[]},{ rabbit@huangyineng,[]}]}]


There are some important caveats:

   When the entire cluster is brought down, the last node to go down must be the first node to be brought online. If this doesn't happen, the nodes will wait 30 seconds for the last disc node to come back online, and fail afterwards. If the last node to go offline cannot be brought back up, it can be removed from the cluster using the forget_cluster_node command - consult the rabbitmqctl manpage for more information.
   If all cluster nodes stop in a simultaneous and uncontrolled manner (for example with a power cut) you can be left with a situation in which all nodes think that some other node stopped after them. In this case you can use the force_boot command on one node to make it bootable again - consult the rabbitmqctl manpage for more information.


6、从集群中移除节点

6.1、主动从集群中退出
把slave2从集群中移除
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl stop_app
Stopping node rabbit@slave2 ...
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl reset
Resetting node rabbit@slave2 ...
[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl start_app
Starting node rabbit@slave2 ...


在slave2中查看集群状态,只有他自己了

[ hadoop@slave2 rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@slave2 ...
[{nodes,[{disc,[ rabbit@slave2]}]},
{running_nodes,[ rabbit@slave2]},
{cluster_name,<<" rabbit@slave2">>},
{partitions,[]},
{alarms,[{ rabbit@slave2,[]}]}]


在其他节点查看集群状态,可以看出该slave2已经不在集群中了

[ hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@huangyineng ...
[{nodes,[{disc,[ rabbit@huangyineng, rabbit@slave1]}]},
{running_nodes,[ rabbit@slave1, rabbit@huangyineng]},
{cluster_name,<<" rabbit@huangyineng">>},
{partitions,[]},
{alarms,[{ rabbit@slave1,[]},{ rabbit@huangyineng,[]}]}]


6.2、使用forget_cluster_node被动退出

停掉slave1,相当他挂了
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl stop_app
Stopping node rabbit@slave1 ...


在huangyineng这个节点上移除他

[ hadoop@huangyineng rabbitmq]$ sbin/rabbitmqctl forget_cluster_node rabbit@slave1
Removing node rabbit@slave1 from cluster ...


启动slave1时,他以为自己还在集群中,但集群已经把它移除了,所以会出现启动报错,可以执行rabbitmqctl reset重置后再启动

[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl start_app
Starting node rabbit@slave1 ...
BOOT FAILED
===========
Error description:
  {error,{inconsistent_cluster,"Node rabbit@slave1 thinks it's clustered with node rabbit@huangyineng, but rabbit@huangyineng disagrees"}}
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl reset
Resetting node rabbit@slave1 ...
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl start_app
Starting node rabbit@slave1 ...
[ hadoop@slave1 rabbitmq]$ sbin/rabbitmqctl cluster_status
Cluster status of node rabbit@slave1 ...
[{nodes,[{disc,[ rabbit@slave1]}]},
{running_nodes,[ rabbit@slave1]},
{cluster_name,<<" rabbit@slave1">>},
{partitions,[]},
{alarms,[{ rabbit@slave1,[]}]}]


相关问答

更多

net redis 和rabbitmq 有什么区别

RabbitMQ RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。 Redis 是一个Key-Value的NoSQL数据库,开发维护很活跃,虽然它是一个Key-Value数据库存储系统,但它本身支持MQ功能,所以完全可以当做一个轻量级的队列服务来使用。 具体对比 可靠消费 Redis:没有相应的机制保证 ...

如何在windows上搭建rabbitmq

下方两个网址有详细的教程,望采纳,还是不行可以追问我。http://www.cnblogs.com/shanyou/p/4067250.html http://www.myexception.cn/windows/1596759.html

netty和rabbitmq的区别

netty和rabbitmq层次的问题: 我知道netty是tcp通信框架,rabbitmq是基于tcp通信封装的一种消息队列。如果包含套节字的话他们之间的关系层次是 socket/nio ---> netty ---> rabbitmq 这种,不知道我理解的有没有错误。 netty和rabbitmq 替换关系: 在不考虑数据解析序列化的前提下,单对单的,不涉及延时:能用netty的地方是不是可以用rabbitmq?能用rabbitmq的地方是不是可以使用netty+protobuf替换。

Windows10安装RabbitMQ ,安装完成所有命令都报“系统找不到指定文件”

rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统。它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rabbit MQ 是建立在Erlang OTP平台上。

如何监控RabbitMQ的性能

测试 rabbitmq 的性能方法如下: 1、声明7个具有不同属性的queue,分别和名为test_exchage的exchange进行绑定(因为exchange为fanout类型,所以测试代码中的routing_key其实是不起作用的); 2、向exchange发送具有persistent属性的消息(delivery_mode=2); 3、创建7个消费者分别从上述7个queue中获取消息;

专题教程

JAVA概述
第一部分:java入门基础
第二部分:java常用类
第三部分:jdbc系列教程
第四部分:java高级特征
快速入门
Gson教程
快速了解 jdk8 新特征

相关文章

更多

最近更新

更多