首先建立了两个tomcat服务器
173.16.80.104:3020
173.16.80.70:8090
下载haproxy
sudo yum install haproxy
一般在/etc/haproxy
编辑配置文件 sudo vi haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user root
group root
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
log global
mode http
option http-keep-alive
option httplog
timeout connect 5000ms
timeout client 10000ms
timeout server 50000ms
timeout http-request 20000ms
balance roundrobin
listen stats
mode http
log 127.0.0.1 local0 err
bind 0.0.0.0:1088
stats enable
stats hide-version
stats uri /stats
stats refresh 30s
stats auth admin:admin
stats admin if TRUE
listen tomcat
bind 0.0.0.0:5222
mode http
maxconn 300
log 127.0.0.1 local0 debug
balance roundrobin
server static1 173.16.80.104:3020 check inter 2000 fall 3 weight 30
server static2 173.16.80.70:8090 check inter 2000 fall 3 weight 30
配置文件解释
###########全局配置#########
global
log 127.0.0.1 local2
(log 127.0.0.1 local0 #[日志输出配置,所有日志都记录在本机,通过local0输出]
log 127.0.0.1 local1 notice #定义haproxy 日志级别[error warringinfo debug])
chroot /var/lib/haproxy #chroot运行路径
pidfile /var/run/haproxy.pid #haproxy 进程PID文件
maxconn 4000 #默认最大连接数,需考虑ulimit-n限制
user root #运行haproxy的用户
group root #运行haproxy的用户所在的组
daemon #以后台形式运行harpoxy
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
########默认配置############
defaults
log global
mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
option http-keep-alive #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option httplog #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
timeout connect 5000ms #连接超时
timeout client 10000ms #客户端超时
timeout server 50000ms #服务器超时
#timeout http-keep-alive10s #默认持久连接超时时间
#timeout queue 1m #默认队列超时时间
timeout http-request 20000ms #默认http请求超时时间
balance roundrobin #设置默认负载均衡方式,轮询方式
#balance source #设置默认负载均衡方式,类似于nginx的ip_hash
#balnace leastconn #设置默认负载均衡方式,最小连接数
########统计页面配置########
listen stats
mode http #http的7层模式
log 127.0.0.1 local0 err #错误日志记录
bind 0.0.0.0:1088 #设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称
stats enable
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats uri /stats #统计页面url
stats refresh 30s #统计页面自动刷新时间
stats auth admin:admin #设置监控页面的用户和密码:admin,可以设置多个用户名
stats admin if TRUE #设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本)
########backend后端配置##############
listen tomcat
bind 0.0.0.0:5222
mode http
maxconn 300
log 127.0.0.1 local0 debug
balance roundrobin
server static1 173.16.80.104:3020 check inter 2000 fall 3 weight 30 #设服务器信息
server static2 173.16.80.70:8090 check inter 2000 fall 3 weight 30 #设服务器信息
重新启动[chenwb@hadoop03 haproxy]$ sudo systemctl restart haproxy
运行 [chenwb@hadoop03 haproxy]$ sudo haproxy -f /etc/haproxy/haproxy.cfg
检查两个tomcat是否成功
浏览器地址栏输入配置的ip+端口 出现以下页面
然后在浏览器地址栏输入
http://173.16.80.70:1088/stats
得到一下的页面表示成功配置
如果出现一下的错误
[WARNING] 079/184258 (56658) : config : log format ignored for proxy 'stats' since it has no log address.
[WARNING] 079/184258 (56658) : config : log format ignored for proxy 'tomcat' since it has no log address.
只是没配置日志
[WARNING] 079/184258 (56658) : [haproxy.main()] Cannot raise FD limit to 8034.
[ALERT] 079/184258 (56658) : Starting frontend GLOBAL: error when trying to preserve previous UNIX socket [/var/lib/haproxy/stats]
[ALERT] 079/184258 (56658) : Starting proxy stats: cannot listen to socket [127.0.0.1:8066]
[ALERT] 079/184258 (56658) : Starting proxy tomcat: cannot listen to socket [0.0.0.0:5111]
修改配置为上面的配置文件内容 然后使用sudo启动就可以了
附录:2个jboss的配置
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
#replace the user and group
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
tune.bufsize 65536
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend ft_web
bind 0.0.0.0:18080
default_backend jboss
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend jboss
balance leastconn
cookie SERVERID insert indirect nocache
#replace your ip for ip1 and ip2
server ps1 ip1:8080 check cookie ps1
server ps2 ip2:8080 check cookie ps2