所需安装包:
[root@m1 rabbitmq]# tree . ├── haproxy-1.6.5.tar.gz └── rabbitmq ├── erlang-18.3-1.el7.centos.x86_64.rpm ├── rabbitmq-server-3.6.5-1.noarch.rpm └── socat-1.7.3.2-1.1.el7.x86_64.rpm
一、环境准备:
1、修改两台机器的hostname:
hostnamectl set-hostname m1 hostnamectl set-hostname m2
2、修改两台机器的hosts文件:
[root@m1 rabbitmq]# vim /etc/hosts ... 139.198.191.246 m1 139.198.157.32 m2
3、重启机器
二、完成单台RabbitMQ的安装
1、安装erlang环境:
rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm
2、安装socat环境:
rpm -ivh socat-1.7.3.2-1.1.el7.x86_64.rpm # 或者直接执行: yum inatll -y socat
3、安装rabbitMQ:
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
4、开启管理界面并修改初始配置:
# 开启管理界面:http://ip:15672 rabbitmq-plugins enable rabbitmq_management # 修改默认配置信息 vim /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app ## 比如修改密码、配置等等,例如:loopback_users 中的 <<"guest">>,只保留guest
5、指定配置文件:
[root@m1 rabbitmq]# cd /usr/share/doc/rabbitmq-server-3.6.5/ [root@m1 rabbitmq-server-3.6.5]# cp rabbitmq.config.example rabbitmq.config
6、启停RabbitMQ服务:
service rabbitmq-server start # 启动服务 service rabbitmq-server stop # 停止服务 service rabbitmq-server restart # 重启服务
三、搭建集群
以 m2 加入 m1 的集群为例
1、同步 .erlang.coolie 文件:
scp /var/lib/rabbitmq/.erlang.cookie m2:/var/lib/rabbitmq/
2、停止 m2 应用:
rabbitmqctl stop_app # stop_app 只代表让rabbitMQ不对外提供服务,而不是kill这个进程
3、将 m2 节点加入到 m1 的集群:
rabbitmqctl join_cluster rabbit@m1
4、重新启动 m2 节点服务:
rabbitmqctl start_app
5、再任意节点都可以查看集群状态:
[root@m2 rabbitmq-server-3.6.5]# rabbitmqctl cluster_status Cluster status of node rabbit@m2 ... [{nodes,[{disc,[rabbit@m1,rabbit@m2]}]}, {running_nodes,[rabbit@m1,rabbit@m2]}, {cluster_name,<<"rabbit@m1">>}, {partitions,[]}, {alarms,[{rabbit@m1,[]},{rabbit@m2,[]}]}]
6、通过管理界面查看集群状态:
四、通过HaProxy对两个节点实现负载均衡
1、安装HaProxy:
yum install -y haproxy # 检查安装是否成功 [root@m1 rabbitmq-server-3.6.5]# haproxy HA-Proxy version 1.8.27-493ce0b 2020/11/06 Copyright 2000-2020 Willy Tarreau <willy@haproxy.org>
2、修改配置文件:
vim /etc/haproxy/haproxy.cfg # 前面的都不用修改: ...... defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 #-----下面的原内容删除,改为以下内容-------- #对MQ集群进行监听 listen rabbitmq_cluster bind 0.0.0.0:5672 option tcplog mode tcp option clitcpka timeout connect 1s timeout client 10s timeout server 10s balance roundrobin server m1 139.198.191.246:5672 check inter 5s rise 2 fall 3 server m2 139.198.157.32:5672 check inter 5s rise 2 fall 3 #开启haproxy监控服务 listen http_front bind 0.0.0.0:1080 stats refresh 30s stats uri /haproxy_stats stats auth admin:admin
3、修改配置后启动Haproxy:
service haproxy {start|stop|reload|restart|status} # 查看状态: [root@i-zgn6som8 ~]# systemctl status haproxy.service ● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2022-08-11 11:41:03 CST; 1min 25s ago Main PID: 20250 (haproxy-systemd) CGroup: /system.slice/haproxy.service ├─20250 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ├─20251 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds └─20252 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
4、通过Haproxy的控制台,输入账号密码后,即可访问界面:
5、之后,如果我们想连接RabbitMQ集群,则直接通过Haproxy代理出来的5672端口即可:
192.198.183.162:5672