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