负载均衡之session共享的三种处理方法

负载均衡采用的是使用nginx进行反向代理,进行分配流量,nginx的安装和使用:

1.下载Nginx的安装包,http://download.csdn.net/download/qq_34125349/10115583

2.打开conf打开nginx.conf进行配置负载均衡


  1. upstream localhost {
  2. server localhost:8080 weight=1;
  3. server localhost:18080 weight=1;
  4. }

3.修改 Nginx 端口号不与 Tomcat 冲突


  1. server {
  2. listen 80;
  3. }

4.启动、重启和关闭 nginx

启动 DOS 环境进入 nginx 目录,输入


  1. start nginx //启动 nginx
  2. nginx -s reload //重启 nginx
  3. nginx -s stop //关闭 nginx

这样配置后访问 http://localhost 后 nginx 会将请求分发给 http://localhost:8080 和 http://localhost:18080

# Tomcat 配置

1.将 Tomcat 文件夹复制成多份

2.修改端口号,修改 conf 文件夹下面的 server.xml 配置文件


  1. <Server port="18005" shutdown="SHUTDOWN">
  2. <Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
  3. <Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />

负载均衡Session共享的三种方案:

方案一、Tomcat 广播方案(Tomcat Session Clustering)

1.修改 conf 文件夹下面的 server.xml 配置文件,在 Engine (<Engine name="Catalina" defaultHost="localhost">)模块下添加如下代码


  1. <Cluster classname="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
  2. <Manager classname="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>
  3. <Channel classname="org.apache.catalina.tribes.group.GroupChannel">
  4. <Membership classname="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/>
  5. <Receiver classname="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
  6. <Sender classname="org.apache.catalina.tribes.transport.ReplicationTransmitter">
  7. <Transport classname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
  8. </Sender>
  9. <Interceptor classname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
  10. <Interceptor classname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
  11. </Channel>
  12. <Valve classname="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>
  13. <Valve classname="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
  14. <Deployer classname="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
  15. <ClusterListener classname="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
  16. <ClusterListener classname="org.apache.catalina.ha.session.ClusterSessionListener"/>
  17. </Cluster>

2.在 web 项目的 web.xml 文件中添加

<distributable/>

方案二、Tomcat-redis-session-manager 插件方案,将sessionId放在redis进行存储

1.添加 redis session 集群依赖的jar包到 tomcat/lib 目录下

tomcat-redis-session-manager-2.0.0.jar

jedis-2.5.2.jar

commons-pool2-2.2.jar

2.修改 conf 目录下的 context.xml 文件

<Valve classname="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

<Manager classname="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />

3.启动 DOS 环境进入 redis 目录,启动 redis

redis-server.exe redis.windows.conf

方案三、tomcat-memcached-session-manmger 插件方案

1.添加 memcached session 集群依赖的 jar 包到 tomcat/lib 目录下

couchbase-client-1.4.4.jar

javolution-5.4.3.1.jar

memcached-session-manager-1.6.5.jar

memcached-session-manager-tc7-1.6.5.jar

msm-javolution-serializer-1.6.5.jar

msm-kryo-serializer-1.6.5.jar

msm-xstream-serializer-1.6.5.jar

spymemcached-2.11.4.jar

2.修改 conf 目录下的 context.xml 文件

<Manager classname="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211" sticky="false" requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryclass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false"/>

3.安装 memcached

windows:

解压 memcached 安装包,启动 DOS 环境,进入 memcached 的目录,输入 memcached.exe -d install 命令安装 memcached

安装后的 memcached 安装为系统服务,进入任务管理器,在服务中找到 memcached 服务,右键启动

负载均衡之session共享的三种处理方法

相关推荐