RabbitMQ集群设置

如果英文还可以,请直接看RabbitMQ的官方文章即可:

https://www.rabbitmq.com/clustering.html

 

环境:

OS: CentOS6.4

JDK版本: 1.7.0_67

RabbitMQ版本:  3.5.1

Erlang版本:17.4

 

1. 同步erlang cookie

首先,选择一台安装了RabbitMQ的机器作为集群主机

erlang cookie位于/var/lib/rabbitmq/.erlang.cookie

将此主机上的erlang cookie拷贝到其他机器上:

 

scp /var/lib/rabbitmq/.erlang.cookie <username>@<host>:/var/lib/rabbitmq/

将<username>和<host>替换成自己相应的参数

 

如果有有多个节点,请重复此步骤将erlang cookie同步到各机器上

假设我有三台主机安装了RabbitMQ,需要组成集群,它们的主机名分别是 rabbit1, rabbit2 和 rabbit3

我需要在 rabbit1 的主机上执行如下命令同步erlang cookie:

 

rabbit1$ scp /var/lib/rabbitmq/.erlang.cookie rabbitmq@rabbit2:/var/lib/rabbitmq/
rabbit1$ scp /var/lib/rabbitmq/.erlang.cookie rabbitmq@rabbit3:/var/lib/rabbitmq/

 

 

2. 分别在主机上启动RabbitMQ

 

rabbit1$ rabbitmq-server -detached
rabbit2$ rabbitmq-server -detached
rabbit3$ rabbitmq-server -detached

 可以在各个主机上执行以下命令查看集群状态:

 

 

rabbitmqctl cluster_status

 此时看到的结果应该是各个RabbitMQ实例都是独自运行的:

 

输出
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1]}]},{running_nodes,[rabbit@rabbit1]}]
...done.

 

 3. 创建集群

我们需要将rabbit2和rabbit3主机上的rabbitmq加入rabbit1主机上运行的RabbitMQ集群,通过在rabbit1主机上执行"rabbitmqctl cluster_status"后我们可以得到rabbit1上运行的集群的名字:

{cluster_name,<<"rabbit@rabbit1">>},

 在rabbit2主机上执行以下shell命令:

 

rabbit2$ rabbitmqctl stop_app
Stopping node rabbit@rabbit2 ...done.
rabbit2$ rabbitmqctl join_cluster rabbit@rabbit1
Clustering node rabbit@rabbit2 with [rabbit@rabbit1] ...done.
rabbit2$ rabbitmqctl start_app
Starting node rabbit@rabbit2 ...done.

 之后,在rabbit1和rabbit2主机上执行以下命令查看集群状态:

 

 

rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]}]},
 {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]
...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]}]},
 {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]
...done.

可以看到节点已经成功加入到了集群

 

在 rabbit3 主机上执行同样的命令加入集群

 

 4. 在各个主机查看集群状态

分别在各个主机执行"rabbitmqctl cluster_status"命令即可查看当前集群的状态,执行情况如下:

 

rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
 {running_nodes,[rabbit@rabbit3,rabbit@rabbit2,rabbit@rabbit1]}]
...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
 {running_nodes,[rabbit@rabbit3,rabbit@rabbit1,rabbit@rabbit2]}]
...done.
rabbit3$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit3 ...
[{nodes,[{disc,[rabbit@rabbit3,rabbit@rabbit2,rabbit@rabbit1]}]},
 {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}]
...done.

 

 

 如何重启集群中的节点:

在需要重启的节点上执行以下命令停止节点,如:

 

rabbit1$ rabbitmqctl stop
Stopping and halting node rabbit@rabbit1 ...done.

 此时,在其他主机上运行"rabbitmqctl cluster_status"命令检查节点是否已经停止:

 

 

rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
 {running_nodes,[rabbit@rabbit3,rabbit@rabbit2]}]
...done.

 从输出结果可以看到,集群中正在运行的节点集合里面已经不包含刚才停止的节点。

 

 

在 rabbit1 主机上执行:

 

rabbitmq-server -detached

 即可启动RabbitMQ实例并自动加入集群

 

 

rabbit1$ rabbitmq-server -detached
rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
 {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]
...done.

 

 

退出集群:

例如,需要将在 rabbit3 主机上运行的RabbitMQ实例退出 rabbit@rabbit1 集群,可以这么做:

 

rabbit3$ rabbitmqctl stop_app
Stopping node rabbit@rabbit3 ...done.
rabbit3$ rabbitmqctl reset
Resetting node rabbit@rabbit3 ...done.
rabbit3$ rabbitmqctl start_app
Starting node rabbit@rabbit3 ...done.

还可以在其他主机上将其他节点从集群中排除;如从rabbit2主机上将rabbit1主机上的node排除出集群:

 

 

rabbit1$ rabbitmqctl stop_app
Stopping node rabbit@rabbit1 ...done.
rabbit2$ rabbitmqctl forget_cluster_node rabbit@rabbit1
Removing node rabbit@rabbit1 from cluster ...
...done.

 此时rabbit1主机上的RabbitMQ并不知道它已经不在集群中了,所以当在rabbit1上启动节点的时候,会出现错误,此时需要执行以下命令进行重置节点:

 

rabbit1$ rabbitmqctl start_app
Starting node rabbit@rabbit1 ...
Error: inconsistent_cluster: Node rabbit@rabbit1 thinks it's clustered with node rabbit@rabbit2, but rabbit@rabbit2 disagrees
rabbit1$ rabbitmqctl reset
Resetting node rabbit@rabbit1 ...done.
rabbit1$ rabbitmqctl start_app
Starting node rabbit@mcnulty ...
...done.

 

你可能感兴趣的:(rabbitmq)