会话绑定, 可以使用前端调度器nginx中的 hash 指令, 可以通过 hash $remote_addr consistent; 通过客户端ip 绑定 也可以通过 hash $cookie_JSESSIONID consistent; 通过对应的cookie 进行绑定
Tomcat 官方实现了 Session 的复制集群,将每个Tomcat的Session进行相互的复制同步,从而保证所有 Tomcat都有相同的Session信息
https://tomcat.apache.org/tomcat-10.0-doc/cluster-howto.html
https://tomcat.apache.org/tomcat-9.0-doc/cluster-howto.html
https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html
https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
<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" #45564/UDP
frequency="500" #间隔500ms发送
dropTime="3000"/> #故障阈值3s
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto" #监听地址,此项建议修改为当前主机的IP(不支持0.0.0.0),如果不修改可能会导致服务无法启动
port="4000" #监听端口
autoBind="100" #如果端口冲突,自动绑定其它端口,范围是4000-4100
selectorTimeout="5000" #自动绑定超时时长5s
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.MessageDispatchIntercep
tor"/>
</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.ClusterSessionListener"/>
</Cluster>
#注意:tomcat7的官方文档此处有错误
http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
......
<ClusterListener
className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener">
<ClusterListener
className="org.apache.catalina.ha.session.ClusterSessionListener">
</Cluster>
配置说明
Cluster 集群配置
Manager 会话管理器配置
Channel 信道配置
Membership 成员判定。使用什么多播地址、端口多少、间隔时长ms、超时时长ms。同一个多播地址和端口认为同属一个组。使用时修改这个多播地址,以防冲突
Receiver 接收器,多线程接收多个其他节点的心跳、会话信息。默认会从4000到4100依次尝试可用端口
address="auto",auto可能绑定到127.0.0.1上,所以一定要改为当前主机可用的IP
Sender 多线程发送器,内部使用了tcp连接池。
Interceptor 拦截器
Valve
ReplicationValve 检测哪些请求需要检测Session,Session数据是否有了变化,需要启动复制过程
ClusterListener
ClusterSessionListener 集群session侦听器
使用 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
添加到 <Engine> 所有虚拟主机都可以启用Session复制
添加到 <Host> ,该虚拟主机可以启用Session复制
最后,在应用程序内部启用了才可以使用
修改应用目录下的 WEB-INF/web.xml
注意: 不要修改 conf/web.xml,此文件不起作用
注意:WEB-INF/web.xml文件的如果权限不对,会导致复制失败
</description>
<distributable/> #添加此行
</web-app>
MSM(memcached session manager) 提供将Tomcat的session保持到memcached或Redis的程序,可以实现高可用
https://github.com/magro/memcached-session-manager
支持Tomcat的 6.x、7.x、8.x、9.x
Redisson是Redis官方推荐的Java版的Redis客户端。它提供的功能非常多,也非常强大 Redisson 是基于redis的扩展库,使得redis除了应用于缓存以外,还能做队列等数据结构,直接使用的 分布式锁,以及事务调度器等。此项目更新比较频繁,生产较为推荐
github 地址 https://github.com/redisson/redisson
tomcat 配置Redisson的文档链接
https://github.com/redisson/redisson/tree/master/redisson-tomcat