shangban 发表于 2017-4-18 08:55:19

基于HAproxy的 session同步实践

  首先安装,启动,配置HAproxy的方法网上已经有很多的介绍,此处不再作过多阐述
  推荐2个比较有用的安装配置资料:
  安装说明
  http://blog.chinaunix.net/uid-17291169-id-4744949.html
  配置文件说明
  http://www.iyunv.com/Linux/2012-07/65350.htm
  另附简单操作代码
  重启 关闭 与 启动

service haproxy start/stop/restart
  查询状态

service haproxy status
  OK,前期工作准备完成,下面来描述一下测试环境
  工程1:http://192.168.28.151:1199/TestHa/
  工程2:http://192.168.28.151:2299/TestHa/
  利用HaProxy做负载均衡,并且配置session同步
  以下是HaProsy的配置文件

#---------------------------------------------------------------------
# Example configuration for a possible web application.See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events.This is done
#    by adding the '-r' option to the SYSLOGD_OPTIONS in
#    /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
#   file. A line like the following can be added to
#   /etc/sysconfig/syslog
#
#    local2.*                     /var/log/haproxy.log
#
log         127.0.0.1 local2
chroot      /var/lib/haproxy
pidfile   /var/run/haproxy.pid
maxconn   4000
user      haproxy
group       haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# 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
#---------------------------------------------------------------------
#此处配置HAProxy的监听端口
frontendmain *:5000
acl url_static       path_beg       -i /static /images /javascript /stylesheets
acl url_static       path_end       -i .jpg .gif .png .css .js
use_backend static          if url_static
default_backend             app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance   roundrobin
server      static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
appsession JSESSIONID len 64 timeout 5h request-learn#必配
serverapp1 192.168.28.151:1199 cookie app1 check#必配
serverapp2 192.168.28.151:2299 cookie app2 check#必配
# option httpchk GET /index.html
listen status 192.168.28.151:8899
stats enable
stats uri /stats
stats auth admin:123456
stats admin if TRUE
stats realm (Haproxy\ statistic)

  配置完成后测试一下结果
  访问 http://192.168.28.151:5000/TestHa/





 





 





 
  
可见此过程中的三次跳转,HaProxy已经根据sessionid将本次会话都分配给了同一台机器。


  那么我们再换一个浏览器测试一下负载均衡





 
  
 新的浏览器建立的新的会话被负载到了另外一个端口上。
  实验至此并未结束,实际上HAProxy为我们提供了3种方法来实现session共享


[*]用户IP 识别  
[*]cookie 识别  
[*]session 识别  
  个人觉得前两种比较有局限性,第一种根据ip来分配,在ip变动的时候将会失去这种session一致性。第二种方法将一个cookie头植入客户端,倘若客户端禁用cookie那么也无法实现
  详细的关于这三种方法可以阅读

http://itindex.net/blog/2012/07/24/1343118758531.html
   
 
 
页: [1]
查看完整版本: 基于HAproxy的 session同步实践