分享

OpenFire源码学习之三十:haproxy负载均衡

 WindySky 2017-11-03

安装

使用Haproxy做负载均衡。

下载地址:http://www./downloads/trial-versions/

下载后解压:

tar zcvf haproxy-1.3.20.tar.gz

cd haproxy-1.3.20

make TARGET=linux26 PREFIX=/usr/local/haprpxy

make install PREFIX=/usr/local/haproxy

本人安装在/usr/local/haproxy

解下来编辑haproxy.cfg

vi haproxy.cfg

  1. global    
  2.     maxconn 51200    
  3.     chroot /usr/local/haproxy    
  4.     uid 99    
  5.     gid 99    
  6.     daemon    
  7.     #quiet    
  8.     nbproc 2 #进程数    
  9.     pidfile /usr/local/haproxy/haproxy.pid    
  10.     
  11. defaults    
  12.         mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK    
  13.         #retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置    
  14.         option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器    
  15.         option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接    
  16.         timeout connect 5000ms #连接超时    
  17.         timeout client 30000ms #客户端超时    
  18.         timeout server 30000ms #服务器超时    
  19.         #timeout check 2000 #=心跳检测超时    
  20.         log 127.0.0.1 local0 err #[err warning info debug]    
  21.         balance roundrobin #负载均衡算法    
  22.     #option  httplog #日志类别,采用httplog    
  23.     #option  httpclose   #每次请求完毕后主动关闭http通道,ha-proxy不支持keep-alive,只能模拟这种模式的实现    
  24.     #option  dontlognull    
  25.     #option  forwardfor  #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip    
  26.     
  27. listen admin_stats    
  28.         bind 0.0.0.0:8888 #监听端口    
  29.         option httplog #采用http日志格式    
  30.         stats refresh 30s #统计页面自动刷新时间    
  31.         stats uri /haproxy-stats #统计页面url    
  32.         stats realm Haproxy Manager #统计页面密码框上提示文本    
  33.         stats auth admin:admin #统计页面用户名和密码设置    
  34.         #stats hide-version #隐藏统计页面上HAProxy的版本信息    
  35.     
  36. listen test1    
  37.         bind :5222    
  38.         mode tcp    
  39.         server t1 192.169.1.120:5222    
  40.         server t2 192.169.1.240:5222    
  41.         server t3 192.169.1.250:5222    
  42.     
  43. listen test2 192.169.1.245:80    
  44.        option httpclose    
  45.        option forwardfor    
  46.         server s1 192.169.1.120:9090 check weight 1 minconn 1 maxconn 3 check inter 40000          
  47.         server s2 192.169.1.240:9090 check weight 1 minconn 1 maxconn 3 check inter 40000   
  48.         server s3 192.169.1.250:9090 check weight 1 minconn 1 maxconn 3 check inter 40000   

启动

开启:/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg 

查看进程:ps -e|grep haproxy

杀死进程:kill -s 9 9883

另外还需要设置下linux环境下的打开连接数

修改linux tcp参数

ulimit -n 102400


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多