在集群处理中,我们需要一个中间代理进行一个转发。因为程序写好,发布上线后,总不能因为一个节点挂了,然后修改程序的配置指向另一个节点
。所以,我们需要一个控制管理对某个集群进行统一管理,而外部只需要对这个控制管理代理进行操作即可。哪怕内部任意一个节点挂了,提供给外部访问的还是控制管理代理。
如图所示:
-
下载依赖包
yum install gcc vim wget
-
解压上传haproxy源码包
tar -zxvf haproxy-1.6.5.tar.gz -C /usr/local
-
进入目录、进行编译、安装
cd /usr/local/haproxy-1.6.5
make TARGET=linux31 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
-
创建haproxy配置文件
mkdir /etc/haproxy
vim /etc/haproxy/haproxy.cfg
-
编辑配置文件
#logging options
global
log 127.0.0.1 local0 info
maxconn 5120
chroot /usr/local/haproxy
uid 99
gid 99
daemon
quiet
nbproc 20
pidfile /var/run/haproxy.pid
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5s
clitimeout 60s
srvtimeout 15s
#front-end IP for consumers and producters
listen rabbitmq_cluster
# 对外提供的服务端口
bind 0.0.0.0:5672
mode tcp
#balance url_param userid
#balance url_param session_id check_post 64
#balance hdr(User-Agent)
#balance hdr(host)
#balance hdr(Host) use_domain_only
#balance rdp-cookie
#balance leastconn
#balance source //ip
balance roundrobin
# 绑定的节点及重连机制。节点名称随意
server node1 127.0.0.1:5673 check inter 5000 rise 2 fall 2
server node2 127.0.0.1:5674 check inter 5000 rise 2 fall 2
listen stats
# 对外提供的web页面控制台,填自己对应的ip即可。
bind ip:8100
mode http
option httplog
stats enable
# uri资源
stats uri /rabbitmq-stats
stats refresh 5s
-
启动
-
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
-
访问控制台