二, 基于 tomcat 群集配置 session 会话保持
1, 编辑 vi server.xml , 在 Engine 字段中添加如下代码, 开启群集服务
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> #jvmRoute 表示唯一表示本机, 所以不同主机的是不同的. 这个参数非必须
- <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.10.0.4" port="45564" frequency="500" dropTime="3000"/> #多播地址应该是 224~239, 同一组里的多播地址相同
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="192.168.2.197" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> #address 为本机能够向外通信的地址
- <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"/>
- <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
- </Cluster>
2, 编辑 web.xml
vim /usr/local/tomcat/conf/web.xml 给应用程序加 < distributable/> 标签, 使其能够实现复制. 在 web-app 字段加 <distributable/>.
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <distributable/> #在配置文件的末尾第二行添加即可.
- </web-app>
将 web.xml 放在对应的项目目录下:
- mkdir /usr/local/tomcat/webapps/test/WEB-INF
- cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/test/WEB-INF/
重启服务: catalina.sh stop
catalina.sh start
如果不能启动, 查看日志 logs/catalina.out 信息如下 (不能加入到组播) :blob.png
那么添加到达组播的路由即可: route add -host 228.10.0.4 dev ens33
来源: http://www.bubuko.com/infodetail-2558079.html