rabbitMq双节点高可用集群安装(亲测可用)
查询系统版本
cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
rabbitmq v3.9.13 (centos7支持比较大的版本了,后面版本貌似都是centos8以上)
erlang erlang-23.3.4.11-1.el7.x86_64 (需要和rabbitmq版本匹配)
下载rabbitmq
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.9.13/rabbitmq-server-3.9.13-1.el7.noarch.rpm
下载erlang
wget https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.11/erlang-23.3.4.11-1.el7.x86_64.rpm
安装erlang
rpm -ivh erlang-23.3.4.11-1.el7.x86_64.rpm
安装rabbitmq
rpm -ivh rabbitmq-server-3.9.13-1.el7.noarch.rpm
启用WEB 插件
rabbitmq-plugins enable rabbitmq_management
启动mq
systemctl start rabbitmq-server
添加用户
rabbitmqctl add_user admin admin
设置用户角色命令
rabbitmqctl set_user_tags admin administrator
设置用户权限
rabbitmqctl set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
通过地址:http://<IP>:15672 访问管理页面
集群搭建
准备2台机器
192.168.200.100
192.168.200.150
修改 /etc/hosts
192.168.200.100 node01
192.168.200.150 master
把两台机器cookie 修改成一样
vim /var/lib/rabbitmq/.erlang.cookie
重置
sudo rabbitmqctl stop_app
sudo rabbitmqctl reset
sudo rabbitmqctl join_cluster rabbit@master
sudo rabbitmqctl start_app
查看集群状态
rabbitmqctl cluster_status
在其中一台安装HAProxy
yum install -y haproxy
haproxy -v
systemctl start haproxy
systemctl enable haproxy
配置文件位置:
/etc/haproxy/haproxy.cfg
在配置文件末尾增加如下内容:
listen admin_stats
#haproxy监控界面的访问的IP和端口
bind 0.0.0.0:8081
mode http
stats uri /stats
stats realm Global\ statistics
#haproxy登陆帐户信息
stats auth admin:admin
listen rabbitmq_admin
#rabbit的监控页面
bind 0.0.0.0:15673
server rabbit_admin1 192.168.200.100:15672
server rabbit_admin2 192.168.200.150:15672
listen haproxy
#对外提供的端口,spring boot连接mq的端口
bind 0.0.0.0:5673
option tcplog
mode tcp
balance roundrobin
server rabbitmq1 192.168.200.100:5672 check inter 5000 rise 2 fall 2
server rabbitmq2 192.168.200.150:5672 check inter 5000 rise 2 fall 2
重启HAProxy:
systemctl restart haproxy
ip:8081/stats 为HAProxy监控页面
ip:15673 为 rabbitmq管理页面
ip:5673为tcp连接地址
Rabbit镜像功能
使用Rabbit镜像功能,需要基于rabbitmq策略来实现,政策是用来控制和修改群集范围的某个vhost队列行为和Exchange行为
在cluster中任意节点启用策略,策略会自动同步到集群节点
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
高可用测试
关闭master节点,给队列发送消息,发送成功
参考:
rabbitmq下载及安装:https://www.rabbitmq.com/download.html
erlang包下载地址:https://packagecloud.io/rabbitmq/erlang
rababitmq包下载地址:https://packagecloud.io/rabbitmq/rabbitmq-server
样例配置文件下载地址:https://github.com/rabbitmq/rabbitmq-server/blob/master/deps/rabbit/docs/rabbitmq.conf.example
erlang与MQ版本对应矩阵:https://www.rabbitmq.com/which-erlang.html#intro