web.xml
在工程中的 web.xml
文件的 <web-app>
节点中新增节点 <distributable/>
,若是使用本文章中的测试 war 包则可忽略此步骤,该 war 包已经配置了此项html
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <distributable/> </web-app>
2.在每一个tomcat 中的server.xml
文件中<Engine>
节点中新增下面内容,而且修改新增内容中Receiver
节点address
属性为当前机器ip前端
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"> <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"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="10.88.44.199" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" /> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" /> </Cluster>
将提供的 war 包或者本身的程序部署到 tomcat 的 webapps 下,每一个 tomcat 都要部署,依次启动tomcat,注意不要一块儿启动,要挨个启动,等待上一个tomcat启动完毕再启动下一个,看到图下 log 则证实 session replication部署成功
ps:
确保 session 中的属性都是都要实现 java.io.Serializable
tomcat 节点不超过 5 个java
配置 nginx 中 conf 文件,以下nginx
upstream tomcat{ ip_hash; server localhost:10010;#tomcat节点一 server localhost:10000;#tomcat节点二 } server { listen 88; server_name 10.88.44.199; location / { root html; index index.html index.htm; proxy_pass http://tomcat; proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
若是使用提供 war 包,则进行以下测试:web
若是本身的项目则按以下原理进行测试:
1.访问 nginx 前端,查看sessionid ,记录下来
2.查看发送到哪一个 tomcat 上,中止tomcat
3.再次访问网站,查看 sessionid 是否改变,若是没有改变则证实 session 复制成功apache