Tomcat6+nginx集群,达到负载均衡和session复制

时间:2022-09-03 17:56:32

nginx+tomcat做web项目集群,达到负载均衡、故障转移、session复制功能。

1、nginx配置文件见上一篇“nginx配置文件(反向代理+集群+动静分离)

2、tomcat集群,修改配置内容如下:

修改conf/server.xml

将<Engine name="Catalina" defaultHost="localhost">改为:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

再在改代码下添加以下内容

<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="192.168.1.105" #服务器IP
port="4002"#监听端口,不同tomcat用不通端口
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>

3、session复制

修改项目中web.xml,在最后添加<distributable/>属性,添加后如下:

<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<distributable/>
</web-app>