一 问题描述
双节点RabbitMQ集群发生了脑裂,节点日志报错:
[error] <0.6318.0> Mnesia(rabbit@pc2): ** ERROR ** mnesia_event got {inconsistent_database, running_partitioned_network, rabbit@pc1}
Rabbitmq管理界面上也提示报错:
Network partition detected
Mnesia reports that this RabbitMQ cluster has experienced a network partition. There is a risk of losing data. Please read RabbitMQ documentation about network partitions and the possible solutions.
两个RabbitMQ节点互相识别不到对方:
/*
可以这样模拟脑裂故障:
在其中一台服务器上封禁默认的内部通信端口25672:
iptables -A INPUT -p tcp --dport 25672 -j DROP
iptables -A OUTPUT -p tcp --dport 25672 -j DROP
观察两台服务器的日志,会有如下输出:
节点1:
节点2:
注意此时只判定出net_tick_timeout,要等网络恢复之后,即解封25672端口之后才会判定出现网络分区。
解封25672端口:
iptables -D INPUT 1
iptables -D OUTPUT 1
两个节点的日志都会提示发生了脑裂:
2021-04-13 01:55:35.219 [error] <0.6318.0> Mnesia(rabbit@pc2): ** ERROR ** mnesia_event got {inconsistent_database, running_partitioned_network, rabbit@pc1}
*/
二 出错原因
这是由于网络问题导致集群出现了脑裂。
正常情况下,通过rabbitmqctl cluster_status命令查看到的信息中partitions那一项是空的,就像这样:
# rabbitmqctl cluster_status
Cluster status of node rabbit@smacmullen ...
[{nodes,[{disc,[hare@smacmullen,rabbit@smacmullen]}]},
{running_nodes,[rabbit@smacmullen,hare@smacmullen]},
{partitions,[]}]
...done.
然而当网络分区发生时,会变成这样:
# rabbitmqctl cluster_status
Cluster status of node rabbit@smacmullen ...
[{nodes,[{disc,[hare@smacmullen,rabbit@smacmullen]}]},
{running_nodes,[rabbit@smacmullen,hare@smacmullen]},
{partitions,[{rabbit@smacmullen,[hare@smacmullen]},
{hare@smacmullen,[rabbit@smacmullen]}]}]
...done.
三 临时解决办法
在出现问题的节点上执行: rabbitmqctl stop_app
在出现问题的节点上执行: rabbitmqctl start_app
注意:mq集群不能采用kill -9 杀死进程,否则生产者和消费者不能及时识别mq的断连,会影响生产者和消费者正常的业务处理。
四 配置脑裂后自动修复
4.1 修改节点的配置文件
在/etc/rabbitmq下新建rabbitmq.conf,加入:
[
{rabbit,
[{tcp_listeners,[5672]},
{cluster_partition_handling, autoheal}
]}
]
若已有配置文件,则直接添加{cluster_partition_handling, autoheal}配置,示例:
vi /etc/rabbitmq/rabbitmq.config
4.2 重启rabbitmq
systemctl restart rabbitmq-server
再次模拟脑裂,发现会通过重启其中一个节点自动恢复此问题:
--本篇文章主要参考自:
https://blog.csdn.net/u013256816/article/details/74998896,
https://www.cnblogs.com/liyongsan/p/9640361.html