先介绍环境:
3台个人云服务器 A(20) B(37) C(26)
20 这台作为Master 37、26作为Slave
开放端口:5672 15672 25672 4369
如果开启防火墙建议关闭
查看防火墙状态: systemctl status firewalld.service
绿的running表示防火墙开启
执行关闭命令: systemctl stop firewalld.service
再次执行查看防火墙命令:systemctl status firewalld.service
执行开机禁用防火墙自启命令 : systemctl disable firewalld.service
1:三台服务器先各自进行安装 并启动 安装单机教程:https://blog.csdn.net/publicv/article/details/96421936
2:按照教程来,需要将三台的 hostname 名字重新定义一下 ,这边根据 ip 最后一段进行定义 例如:rabbit-xx
vim /etc/hostname
rabbit-xx
3:将三台的 hosts 相互绑定, 互相ping通 三个节点都是一样的操作
vim /etc/hosts
ip rabbit-xx
ip rabbit-xx
ip rabbit-xx
4: 将master 节点的 .erlang.cookie 同步到其他两台服务器
cd /var/lib/rabbitmq/
ll -a
scp /var/lib/rabbitmq/.erlang.cookie ip:/var/lib/rabbitmq/
1:集群步骤开始:将三台节点都进行关闭 这边不能使用单机时的命令 /etc/init.d/rabbitmq-server stop
rabbitmqctl stop
会遇到这个错误,不要慌。他说明的很清楚了,
- 4369要开启
- 25672要开启
- hostname主机名是否和当前主机名保持一致
- home 路径是否正确
- cookie是否一致
因为我写这边博客时是按照我的教程一步一步操作的,希望尽可能避免环境问题。
(1):在这边直接执行 reboot 命令进行重启
reboot
/etc/init.d/rabbitmq-server start
lsof -i:5672
rabbitmqctl stop
2: 这时三台节点都已经进行了关闭, 将三台服务器 执行启动集群命令
rabbitmq-server -detached
3:slave 节点加入集群操作,master 不要执行这步骤 以后重新加入到集群中也是这个命令
37这台slave 执行:
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@你的masterHostname
rabbitmqctl start_app
26这台slave 执行:
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@你的masterHostname
rabbitmqctl start_app
这边 --ram 含义是:使用内存保存而非持久化磁盘。一般来说3台节点 2台持久化1台内存性能会比较好
4:查看集群状态(任意一台节点上都可以)
rabbitmqctl cluster_status
访问任意一个管控台节点:http://ip:15672
如果出现报红:说明你这台节点的 管控台没有开启,在报红那台节点上执行 管控台启动
rabbitmq-plugins enable rabbitmq_management
5:设置镜像队列策略,在任意一台节点上执行一次即可
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
镜像队列成功图:
遇到的一些问题:
rabbitmqctl stop
Stopping and halting node 'rabbit@rabbit-26' ...
Error: unable to connect to node 'rabbit@rabbit-26': nodedown
DIAGNOSTICS
===========
attempted to contact: ['rabbit@rabbit-26']
rabbit@rabbit-26:
* connected to epmd (port 4369) on rabbit-26
* epmd reports node 'rabbit' running on port 25672
* TCP connection succeeded but Erlang distribution failed
* suggestion: hostname mismatch?
* suggestion: is the cookie set correctly?
* suggestion: is the Erlang distribution using TLS?
current node details:
- node name: 'rabbitmq-cli-82@rabbit-26'
- home dir: /var/lib/rabbitmq
- cookie hash: kdWh0HU835FagHiGQNtq0A==
我的解决成功方案:
重启服务器
2:
/etc/init.d/rabbitmq-server start
Starting rabbitmq-server (via systemctl): Job for rabbitmq-server.service failed because the control process exited with error code. erver.service" and "journalctl -xe" for details.
[FAILED]
我的解决成功方案:
hosts 没有配置好
3:
Clustering node 'rabbit@rabbit-37' with 'rabbit@rabbit-20' ...
Error: unable to connect to nodes ['rabbit@rabbit-20']: nodedown
DIAGNOSTICS
===========
attempted to contact: ['rabbit@rabbit-20']
rabbit@rabbit-20:
* unable to connect to epmd (port 4369) on rabbit-20: timeout (timed out)
current node details:
- node name: 'rabbitmq-cli-30@rabbit-37'
- home dir: /var/lib/rabbitmq
- cookie hash: kdWh0HU835FagHiGQNtq0A==
[root@rabbit-37 rabbitmq]# rabbitmqctl join_cluster --ram rabbit@rabbit-20
Clustering node 'rabbit@rabbit-37' with 'rabbit@rabbit-20' ...
我的解决成功方案:
4369 端口没有开放
4:
join_cluster --ram rabbit@rabbit-20
Clustering node 'rabbit@rabbit-37' with 'rabbit@rabbit-20' ...
Error: {inconsistent_cluster,"Node 'rabbit@rabbit-20' thinks it's clustered with node 'rabbit@rabbit-37', but 'rabbit@rabbit-37' disagrees"}
我的解决成功方案:
将所有节点 删除 mnesia ,然后重启 ,重新开始执行集群搭建
rm -rf /var/lib/rabbitmq/mnesia/
5: