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

负载均衡采用的是使用nginx进行反向代理,进行分配流量,nginx的安装和使用:
1.下载Nginx的安装包,http://download.csdn.net/download/qq_34125349/10115583

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

upstream localhost {
	server localhost:8080 weight=1;
	server localhost:18080 weight=1;
}

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

server {
	listen 80;
}
4.启动、重启和关闭 nginx

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

start nginx //启动 nginx
nginx -s reload //重启 nginx
nginx -s stop //关闭 nginx
这样配置后访问 http://localhost 后 nginx 会将请求分发给 http://localhost:8080 和 http://localhost:18080
# Tomcat 配置
1.将 Tomcat 文件夹复制成多份
2.修改端口号,修改 conf 文件夹下面的 server.xml 配置文件
<Server port="18005" shutdown="SHUTDOWN">
<Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />

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

方案一、Tomcat 广播方案(Tomcat Session Clustering)
1.修改 conf 文件夹下面的 server.xml 配置文件,在 Engine (<Engine name="Catalina" defaultHost="localhost">)模块下添加如下代码

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
	<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>
	<Channel className="org.apache.catalina.tribes.group.GroupChannel">
		<Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/>
		<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
		<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
			<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
		</Sender>
		<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
		<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
	</Channel>
	<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>
	<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
	<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
	<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
	<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</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 服务,右键启动
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值