防火墙开启的话,需要开放3个指定端口:5672、15672、25672
下载epel
wget https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm
下载socat
wget http://www.rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/socat-1.7.3.2-2.el7.x86_64.rpm
下载erlang
wget https://github.com/rabbitmq/erlang-rpm/releases/download/v23.2.1/erlang-23.2.1-1.el7.x86_64.rpm
下载rabbitmq-server
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm
yum install -y epel-release-7-14.noarch.rpm
yum install -y socat-1.7.3.2-2.el7.x86_64.rpm
yum install -y erlang-23.2.1-1.el7.x86_64.rpm
rpm -ivh rabbitmq-server-3.8.9-1.el7.noarch.rpm
启动rabbitmq-server
systemctl start rabbitmq-server
查看状态
systemctl status rabbitmq-server
停止服务命令
systemctl stop rabbitmq-server
同步集群节点中的cookie
ls -a /var/lib/rabbitmq
注意要rabbitmq-server启动过才会显示.erlang.cookie
把第二台、第三台上门的.erlang.cookie删除掉,然后使用source copy命令把第一台的.erlang.cookie拷贝到二三上面来,保持一致
修改hosts文件
vi /etc/hosts
192.168.170.1 name1
192.168.170.2 name2
192.168.170.3 name3
启动rabbitmq-server
不出意外应该会报下面的错误
Job for rabbitmq-server.service failed because the control process exited with error code. See "systemctl status rabbitmq-server.service" and "journalctl -xe" for details.
.erlang.cookie 默认账号权限是rabbitmq,我们copy过来之后变成了root会导致启动不了,修改一下用户权限
chown rabbitmq:rabbitmq .erlang.cookie
再来启动rabbitmq-server
集群构建
先把3台服务器的rabbitmq-server服务重启
systemctl restart rabbitmq-server
然后在服务器二、三上面执行,@hostname1改成你的服务器1的hostname
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@hostname1
rabbitmqctl start_app
在3台服务器上开启管理插件
rabbitmq-plugins enable rabbitmq_management
rabbitmqctl add_user admin 密码
rabbitmqctl set_permissions -p / admin "." "." ".*"
rabbitmqctl set_user_tags admin administrator #将admin加入administrator超级管理员组
登录web页面
http://ip:15672
镜像策略配置
rabbitmqctl set_policy all ".*" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
所有队列设置为镜像队列,即队列会被同步到各个节点保持状态一致,RabbitMQ高可用集群就已经搭建好了。
最后设置开机自启
systemctl enable rabbitmq-server.service