RabbitMQ概念及环境搭建RabbitMQ cluster

Posted hao987

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ概念及环境搭建RabbitMQ cluster相关的知识,希望对你有一定的参考价值。

测试环境:VMS00781 VMS00782 VMS00386 (centos5.8)
1.先在三台机器上分别安装RabbitMQ Server

2.读取其中一个节点的cookie,并复制到其他节点(节点间通过cookie确定相互是否可通信)
两者之一均可:
sudo vim /var/lib/rabbitmq/.erlang.cookie
sudo vim $HOME/.erlang.cookie

3.逐个启动节点
sudo service rabbitmq-server start

4.查看各节点中的RabbitMQ brokers
sudo rabbitmqctl cluster_status

5.建集群
分别在VMS00386、VMS00782 上执行
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster --ram [email protected]
sudo rabbitmqctl start_app
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster [email protected]
sudo rabbitmqctl start_app

6.排错
建集群过程中碰到如下错误:
sudo rabbitmqctl join_cluster --ram [email protected]
Clustering node [email protected] with [email protected] ...
Error: unable to connect to nodes [[email protected]]: nodedown
DIAGNOSTICS
===========
attempted to contact: [[email protected]]
[email protected]:
  * unable to connect to epmd (port 4369) on VMS00386: nxdomain (non-existing domain)
current node details:
- node name: ‘[email protected]
- home dir: /var/lib/rabbitmq
- cookie hash: 50YO3zK+HJHos0tab1vHjg==
解决之道:
集群节点间需能互相访问,故每个集群节点的hosts文件应包含集群内所有节点的信息以保证互相解析
vim /etc/hosts
781‘s IP   VMS00781
782‘s IP   VMS00782
386‘s IP   vms00386
之后重启各节点中的rabbitmq

7.其他问题

cluster搭建起来后若在web管理工具中rabbitmq_management的Overview的Nodes部分看到“Node statistics not available”的信息,说明在该节点上web管理插件还未启用。

直接在显示提示信息的节点上运行sudo rabbitmq-plugins enable rabbitmq_management即可。

 

Error: mnesia_unexpectedly_running
原因:忘记先停止stop_app
解决:sudo rabbitmqctl stop_app

若rabbitmq-server第一次启动后hostname不能被解析或者发生了更改则会导致启动失败
需执行如下操作
sudo rm -rf /var/lib/rabbitmq/mnesia(因为相关信息会记录在此数据库)
重装RabbitMQ Server

#####################################################
RabbitMQ cluster 管理
#####################################################
1.查看集群状态
可分别在集群中各个节点执行
sudo rabbitmqctl cluster_status

2.更改节点类型(内存型或磁盘型)
sudo rabbitmqctl stop_app
sudo rabbitmqctl change_cluster_node_type disc

sudo rabbitmqctl change_cluster_node_type ram
sudo rabbitmqctl start_app

3.重启cluster中的节点
停止某个节点或者节点down掉剩余节点不受影响
[[email protected] ~]$ sudo rabbitmqctl stop
Stopping and halting node [email protected] ...

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo rabbitmqctl stop
Stopping and halting node [email protected] ...

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

待节点重启后自动追上其他节点
[[email protected] ~]$ sudo service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

几点注意:
保证集群中至少有一个磁盘类型的节点以防数据丢失,在更改节点类型时尤其要注意。
若整个集群被停掉了,应保证最后一个down掉的节点被最先启动,若不能则要使用forget_cluster_node命令将其移出集群
若集群中节点几乎同时以不可控的方式down了此时在其中一个节点使用force_boot命令重启节点

4.从集群移除节点
[[email protected] ~]$ sudo rabbitmqctl stop_app
Stopping node [email protected] ...
[[email protected] ~]$ sudo rabbitmqctl reset
Resetting node [email protected] ...
[[email protected] ~]$ sudo rabbitmqctl start_app
Starting node [email protected] ...

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected]]}]},
 {running_nodes,[[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected],[email protected]]}]},
 {running_nodes,[[email protected],[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]
可见[email protected]成为了独立的节点,原集群只剩[email protected],[email protected]

也可在某个节点移除集群中其他节点
如继续在[email protected]上移除[email protected]
[[email protected] ~]$ sudo rabbitmqctl forget_cluster_node [email protected]
Removing node [email protected] from cluster ...

[[email protected] ~]$ sudo rabbitmqctl cluster_status
Cluster status of node [email protected] ...
[{nodes,[{disc,[[email protected]]}]},
 {running_nodes,[[email protected]]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

可见集群只剩[email protected]一个节点了

这里有个问题,在远程其他节点中被移除的节点会自认为仍属于集群

[[email protected] ~]$ sudo rabbitmqctl start_app
Starting node [email protected] ...
BOOT FAILED
===========
Error description:
   {error,{inconsistent_cluster,"Node [email protected] thinks it‘s clustered with node [email protected], but [email protected] disagrees"}}
Log files (may contain more information):
   /var/log/rabbitmq/[email protected]
   /var/log/rabbitmq/[email protected]
Stack trace:
   [{rabbit_mnesia,check_cluster_consistency,0},
    {rabbit,‘-start/0-fun-0-‘,0},
    {rabbit,start_it,1},
    {rpc,‘-handle_call_call/6-fun-0-‘,5}]
Error: {rabbit,failure_during_boot,
           {error,
               {inconsistent_cluster,
                   "Node [email protected] thinks it‘s clustered with node [email protected], but [email protected] disagrees"}}}
需要重置一下
[[email protected] ~]$ sudo rabbitmqctl reset
Resetting node [email protected] ...
[[email protected] ~]$ sudo rabbitmqctl start_app
Starting node [email protected] ...

此时三个节点均已成为独立的节点
其中[email protected][email protected]均被重置为了新的RabbitMQ broker而[email protected]还保留着原cluster的残留状态可通过如下步骤重置
[[email protected] ~]$ sudo rabbitmqctl stop_app
Stopping node [email protected] ...
[[email protected] ~]$ sudo rabbitmqctl reset
Resetting node [email protected] ...
[[email protected] ~]$ sudo rabbitmqctl start_app
Starting node [email protected] ...

5.自动配置cluster
显然,这是通过配置文件而非命令行工具进行的
首先重置各节点
[[email protected] ~]$ sudo rabbitmqctl stop_app
Stopping node [email protected] ...
[[email protected] ~]$ sudo rabbitmqctl reset
Resetting node [email protected] ...
...
其次调整配置文件
[{rabbit,
  [{cluster_nodes, {[‘[email protected]‘, ‘[email protected]‘, ‘rabbit[email protected]‘], disc}}]}].
...
之后启动各节点
[[email protected] ~]$ sudo service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.

查看集群状态
[[email protected] ~]$ sudo rabbitmqctl cluster_status

几点注意:
无论通过命令行还是通过配置文件配置,请确保各节点上Erlang和RabbitMQ版本一致
配置文件仅对新鲜节点有效,也即被reset或者第一次启动的节点。因此在重启节点后自动化集群过程并不会发生。也以为这通过rabbitmq进行的改变优先于自动化集群配置。

在一台机器上部署集群,一般用户测试集群特性
这里的关键是已不同的端口可节点名称启动多个rabbitmq-server实例,其余过程同多机器上部署集群类似

其他注意事项:
如防火墙策略等

参考:
http://www.rabbitmq.com/clustering.html

 

转载:  https://blog.csdn.net/zyz511919766/article/details/41896747






















































































































































































































































以上是关于RabbitMQ概念及环境搭建RabbitMQ cluster的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMQ的相关概念及队列模式

RabbitMQ的相关概念及队列模式

RabbitMQ的相关概念及队列模式

RabbitMQ cluster

RabbitMQ 核心概念及与 Spring Boot 2 的整合

rabbitMQ基本概念及基础使用