您的位置:首页 > 理论基础 > 计算机网络

windows下 Haproxy 负载均衡(HTTP+TCP)--- 建议

2016-09-22 15:56 1691 查看
1、下载haproxy

2、HTTP和TCP混合模式配置

##########全局配置#########  

global  

daemon  #以后台形式运行haproxy   

nbproc 1  #进程数量(可以设置多个进程提高性能)   

pidfile /haproxy/run/haproxy.pid   #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件   

ulimit-n 82000 #设置每个进程的可用的最大文件描述符  

log 127.0.0.1 local0 #日志输出配置,所有日志都记录在本机,通过local0输出   

#log 127.0.0.1 local1 notice   

#log loghost local0 info   

#maxconn 4096 #最大连接数   

#chroot /var/haproxy #chroot运行的路径   

#uid 99 #所属运行的用户uid   

#gid 99 #所属运行的用户组   

#debug #调试模式,输出启动信息到标准输出   

#quiet #安静模式,启动时无输出  

########默认配置############

defaults  

#mode http #混合模式时关闭 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK

#maxconn 1000 #默认的最大连接数    

#option dontlognull #不记录健康检查的日志信息   

#option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户ip

#stats refresh 30 #统计页面刷新间隔   

#balance leastconn #默认的负载均衡的方式,最小连接 

retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置

option httplog #日志类别http日志格式   

option tcplog  #日志类别tcp日志格式

option httpclose #每次请求完毕后主动关闭http通道 

option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接

option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器  

balance roundrobin #默认的负载均衡的方式,轮询方式

balance source #默认的负载均衡的方式,类似nginx的ip_hash    

timeout connect 5000ms #连接超时  

timeout client 30000ms #客户端超时  

timeout server 30000ms #服务器超时  

timeout queue   5000ms  #在队列等待连接槽释放的超时时间

timeout client-fin 30000ms #半关闭状态连接,server端非活动超时时间

timeout server-fin 30000ms #半关闭状态连接,client端非活动超时时间

timeout tunnel  1h  #客户端和服务器端通道非活动超时时间

log 127.0.0.1 local0 err #[err warning info debug]  

########统计页面配置########

listen admin_stats  #Frontend和Backend的组合体,监控组的名称,按需自定义名称   

bind 0.0.0.0:8088 #监听端口  

mode http #http的7层模式  

option httplog #采用http日志格式  

log 127.0.0.1 local0 err     

maxconn 10  

stats refresh 30s #统计页面自动刷新时间  

stats uri /stats #统计页面url,监控页面的url   

stats realm XingCloud\ Haproxy #统计页面密码框上提示文本,监控页面的提示信息   

stats auth admin:admin #统计页面用户名和密码设置,监控页面的用户和密码admin,可以设置多个用户名  

stats auth admin1:admin1 #监控页面的用户和密码admin1   

stats hide-version #隐藏统计页面上HAProxy的版本信息  

stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)   

#errorfile 403 /etc/haproxy/errorfiles/403.http   

#errorfile 500 /etc/haproxy/errorfiles/500.http   

#errorfile 502 /etc/haproxy/errorfiles/502.http   

#errorfile 503 /etc/haproxy/errorfiles/503.http   

#errorfile 504 /etc/haproxy/errorfiles/504.http 

########test1配置#################启动项目必须重启haproxy才能在一个项目挂了之后,跳转到另一个项目,设置check inter 2s将会不停的想应用发送检测  

listen test1  

bind 0.0.0.0:1080  

mode http  

maxconn 1024  

server web1 127.0.0.1:8080 weight 1 rise 2 fall 3

server web2 127.0.0.1:8081 weight 1 rise 2 fall 3

########frontend配置##############  

   

########test2配置#################设置check inter 2s将会不停的想应用发送检测

listen test2  

bind 0.0.0.0:1051  

mode tcp  

server tcp1 127.0.0.1:10519 maxconn 1024 weight 1 rise 2 fall 3  

server tcp2 127.0.0.1:10518 maxconn 1024 weight 1 rise 2 fall 3

########frontend配置##############

3、然后DOS下使用cmd命令:
cd XX:\YYYYYYY\haproxy1.5
在解压的路径下输入
haproxy.exe -f haproxy.cfg -D 
如图,无视Warning信息:

4、查看进程中是否已经存在

5、统计页面配置 
http://localhost:8088/stats
6、应用访问根据自己设置的端口 

我的HTTP端口:1080,TCP端口:1051
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: