首先准备好环境,rabbit安装见:
centos7.9 安装rabbitmq_fyihdg的博客-CSDN博客
我是这个成功安装的基础上搭建的rabbitmq集群,
我安装的是普通集群,
192.168.1.61 ? rabbitmq1? ? (主) 192.168.1.62 ? rabbitmq2? ?(从)
1、修改linux配置文件
两台服务器修改如下:
vi /etc/hosts

?
使用命令查询主机是否修改:
hostnamectl?


如果发现hostname名字没修改成host文件中的名字,执行命令修改:
hostnamectl set-hostname ?rabbitmq1
2、复制cookie内容
首先切换到.erlang.cookie,所有的目录,默认是在:/var/lib/rabbimq
但有时候,命令ll -a 居然没有!!!然后怎么办?写了一个程序,连接到rabbitmq,发几条消息,消费几条消息,重启后,就生成了。
cd /var/lib/rabbitmq
把主服务器上的.erlang.cookie文件,复制到从服务器,相同的位置,可以把这个文件下载到本机,再上传到从服务器,或者使用命令复制,在主服务器上操作:
scp .erlang.cookie root@192.168.1.62:/var/lib/rabbitmq/.erlang.cookie
回车会提示输入root密码,输入回车就行

?然后授权,?.erlang.cookie 文件权限只能是600,777我试过,不行的
chmod 600 .erlang.cookie

?
?要注意这里,刚刚复制过来的是root,用户,要修改过来
chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie

主服务器执行如今命令:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
如果报:You cannot reset a node when it is the only disc node in a cluster,执行:
???????rabbitmqctl force_reset
执行rabbitmqctl stop_app命令可能会报错:
[root@rabbitmq01 rabbitmq]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq01 ...
Error: unable to perform an operation on node 'rabbit@rabbitmq01'. Please see diagnostics information and suggestions below.
Most common reasons for this are:
* Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)
* CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)
* Target node is not running
In addition to the diagnostics info below:
* See the CLI, clustering and networking guides on https://rabbitmq.com/documentation.html to learn more
* Consult server logs on node rabbit@rabbitmq01
* If target node is configured to use long node names, don't forget to use --longnames with CLI tools
DIAGNOSTICS
===========
attempted to contact: [rabbit@rabbitmq01]
rabbit@rabbitmq01:
* connected to epmd (port 4369) on rabbitmq01
* epmd reports: node 'rabbit' not running at all
no other nodes on rabbitmq01
* suggestion: start the node
Current node details:
* node name: 'rabbitmqcli-1562-rabbit@rabbitmq01'
* effective user's home directory: /var/lib/rabbitmq
* Erlang cookie hash: KiLCK6kZzMo9nB0K7pdlng==

?解决的办法是,输入命令:
systemctl start rabbitmq-server
就可以解决了


然后从服务器分别执行命令:?
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq1
rabbitmqctl start_app

可能会报以下错误:
[root@rabbitmq02 rabbitmq]# rabbitmqctl join_cluster rabbit@rabbitmq01
Clustering node rabbit@rabbitmq02 with rabbit@rabbitmq01
Error: unable to perform an operation on node 'rabbit@rabbitmq01'. Please see diagnostics information and suggestions below.
Most common reasons for this are:
* Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)
* CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)
* Target node is not running
In addition to the diagnostics info below:
* See the CLI, clustering and networking guides on https://rabbitmq.com/documentation.html to learn more
* Consult server logs on node rabbit@rabbitmq01
* If target node is configured to use long node names, don't forget to use --longnames with CLI tools
DIAGNOSTICS
===========
attempted to contact: [rabbit@rabbitmq01]
rabbit@rabbitmq01:
* unable to connect to epmd (port 4369) on rabbitmq01: address (cannot connect to host/port)
Current node details:
* node name: 'rabbitmqcli-1958-rabbit@rabbitmq02'
* effective user's home directory: /var/lib/rabbitmq
* Erlang cookie hash: KiLCK6kZzMo9nB0K7pdlng==
[root@rabbitmq02 rabbitmq]# systemctl start firewalld
[root@rabbitmq02 rabbitmq]# systemctl stop firewalld
解决的办法是,先开启防火封墙,再关闭
systemctl start firewalld
systemctl stop firewalld
3、创建用户登录查看是否创建成功
两台服务器分别执行命令,其中root是我自己定义,可以随意修改
rabbitmqctl add_user root root
rabbitmqctl set_user_tags root administrator
rabbitmqctl set_permissions -p / root ".*" ".*" ".*"


这样的方式还不够完善,一台挂了,另一台就无法消费消息了,所以, 要设置镜像模式:

?
?
查看集群状态:
rabbitmqctl cluster_status

集群搭建成功了
|