您的当前位置:首页正文

Rabbitmq主备模式+haproxy

2024-12-01 来源:个人技术集锦

主机准备

172.19.220.172   xm-test_02-172
172.19.220.140   xm-test-172
/etc/hosts信息

172.19.220.172  xm-test_02-172
172.19.220.140  xm-test-172
hostname信息

xm-test_02-172.19.220.172-sh
xm-test-172.19.220.140-sh

一、首先安装rabbitmq-server

1、软件安装

wget https://github.com/rabbitmq/erlang-rpm/releases/download/v21.2.2/erlang-21.2.2-1.el6.x86_64.rpm
rpm -Uvh erlang-21.2.2-1.el6.x86_64.rpm
yum install -y socat
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
rpm -ivh https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.9/rabbitmq-server-3.7.9-1.el7.noarch.rpm
systemctl enable rabbitmq-server
systemctl start rabbitmq-server

2、账户设置

rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmq-plugins enable rabbitmq_management
rabbitmqctl list_users
rabbitmqctl change_password admin xxxxxxxxxx
rabbitmqctl list_users
rabbitmqctl set_user_tags admin administrator

3、主备设置

设置
xm-test_02-172.19.220.172-sh为主节点

拷贝主节点
/var/lib/rabbitmq/.erlang.cookie 的文件内容到从节点替代从节点对应文件的内容/var/lib/rabbitmq/.erlang.cookie

在从节点上操作如下信息

#rabbitmqctl stop_app
#rabbitmqctl reset
#rabbitmqctl join_cluster rabbit@xm-test_02-172
Clustering node rabbit@xm-test-172 with rabbit@xm-test_02-172[root@xm-test-172.19.220.140-sh rabbitmq]$rabbitmqctl start_appStarting node rabbit@xm-test-172 ...completed with 3 plugins.
#rabbitmqctl start_app

这就设置成功,查看cluser信息

在从节点上查看

#rabbitmqctl cluster_status
Cluster status of node rabbit@xm-test-172 ...[{nodes,[{disc,['rabbit@xm-test-172','rabbit@xm-test_02-172']}]},{running_nodes,['rabbit@xm-test_02-172','rabbit@xm-test-172']},{cluster_name,<<"rabbit@xm-test_02-172">>},{partitions,[]},{alarms,[{'rabbit@xm-test_02-172',[]},{'rabbit@xm-test-172',[]}]}]
在主节点上查看

#rabbitmqctl cluster_status
Cluster status of node rabbit@xm-test_02-172 ...[{nodes,[{disc,['rabbit@xm-test-172','rabbit@xm-test_02-172']}]},{running_nodes,['rabbit@xm-test-172','rabbit@xm-test_02-172']},{cluster_name,<<"rabbit@xm-test_02-172">>},{partitions,[]},{alarms,[{'rabbit@xm-test-172',[]},{'rabbit@xm-test_02-172',[]}]}]

这样控制台上就线上两台主机了

 

剔除节点的操作

在需要被剔除的节点上操作

rabbitmq-server -detached
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app


添加集群节点

rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@xm-test_02-172    //内存模式,不加ram是磁盘模式
rabbitmqctl start_app
授权

rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'


haproxy安装配置

yum -y install haproxy配置
haproxy.cfg 内容如下

###########全局配置#########
global
log 127.0.0.1 local0 #local0是设备,对应于 /etc/rsyslog.conf中的配置,默认回收info的日志级chroot /etc/haproxy
pidfile /etc/haproxy/haproxy.pid
maxconn 100000
user haproxy
group haproxy
daemon
nbproc 1
node xm-test_02-172
description xm-test_02-172
###########默认配置#########
defaults
log 127.0.0.1 local0
mode http
option httplog
timeout connect 5000
timeout client 50000
timeout server 50000
option httpclose
option redispatch
timeout connect 10000
maxconn 100000
retries 3
#errorfile 400 /var/log/Haproxy/errorfiles/400.http
#errorfile 403 /var/log/Haproxy/errorfiles/403.http
#errorfile 408 /var/log/Haproxy/errorfiles/408.http
#errorfile 500 /var/log/Haproxy/errorfiles/500.http
#errorfile 502 /var/log/Haproxy/errorfiles/502.http
#errorfile 503 /var/log/Haproxy/errorfiles/503.http
#errorfile 504 /var/log/Haproxy/errorfiles/504.http
####################################################################
listen admin_stats
bind 0.0.0.0:1080 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /haproxy?stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息
#####################我把RabbitMQ的管理界面###############################
listen rabbitmq_admin
bind 0.0.0.0:8004
server xm-test_02-172 172.19.220.172:15672
server xm-test-172 172.19.220.140:15672
####################################################################
listen rabbitmq_cluster
bind 0.0.0.0:5672
option tcplog
mode tcp
timeout client 3h
timeout server 3h
option clitcpka
balance roundrobin #负载均衡算法(#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数ï #balance url_param userid
server xm-test_02-172 172.19.220.172:5672 check inter 5s rise 2 fall 3 weight 10 #check inter 2000 是检测心跳频率,rise #2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用
server xm-test-172 172.19.220.140:5672 check inter 5s rise 2 fall 3 weight 10

 

 

显示全文