Tomcat Session Clustering
搭建 Tomcat 集群需要解决很多的问题,其中之一就是要解决 Session 共享问题。小规模集群可以使用 Tomcat 提供的 Session Clustering 来解决。
For the impatient
Simply add
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
to your <Engine> or your <Host> element to enable clustering.
Using the above configuration will enable all-to-all session replication using the DeltaManager to replicate session deltas. By all-to-all we mean that the session gets replicated to all the other nodes in the cluster. This works great for smaller cluster but we don't recommend it for larger clusters(a lot of Tomcat nodes). Also when using the delta manager it will replicate to all nodes, even nodes that don't have the application deployed.
To get around this problem, you'll want to use the BackupManager. This manager only replicates the session data to one backup node, and only to nodes that have the application deployed. Downside of the BackupManager: not quite as battle tested as the delta manager.
Here are some of the important default values:
Multicast address is 228.0.0.4
Multicast port is 45564 (the port and the address together determine cluster membership.
The IP broadcasted is java.net.InetAddress.getLocalHost().getHostAddress() (make sure you don't broadcast 127.0.0.1, this is a common error)
The TCP port listening for replication messages is the first available server socket in range 4000-4100
Listener is configured ClusterSessionListener
Two interceptors are configured TcpFailureDetector and MessageDispatch15Interceptor
The following is the default cluster configuration:
<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="auto"
port="4000"
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>
Cluster Basics
To run session replication in your Tomcat 7.0 container, the following steps should be completed:
All your session attributes must implement java.io.Serializable
Uncomment the Cluster element in server.xml
If you have defined custom cluster valves, make sure you have the ReplicationValve defined as well under the Cluster element in server.xml
If your Tomcat instances are running on the same machine, make sure the Receiver.port attribute is unique for each instance, in most cases Tomcat is smart enough to resolve this on it's own by autodetecting available ports in the range 4000-4100
Make sure your web.xml has the <distributable/> element
If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine <Engine name="Catalina" jvmRoute="node01" > and that the jvmRoute attribute value matches your worker name in workers.properties
Make sure that all nodes have the same time and sync with NTP service!
Make sure that your loadbalancer is configured for sticky session mode.
Load balancing can be achieved through many techniques, as seen in the Load Balancing chapter.
Note: Remember that your session state is tracked by a cookie, so your URL must look the same from the out side otherwise, a new session will be created.
Note: Clustering support currently requires the JDK version 1.5 or later.
The Cluster module uses the Tomcat JULI logging framework, so you can configure logging through the regular logging.properties file. To track messages, you can enable logging on the key: org.apache.catalina.tribes.MESSAGES
more see http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
管中窥豹
我们打开conf下的server.xml文件,找到下面这一行:
<Engine name="Catalina" defaultHost="localhost">
在这一行的下面加入如下代码:
<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="auto"
port="4000"
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>
然后再在 web 项目的 web.xml 中添加 <distributable/> 后即可完成 Tomcat Session 共享。
本文由个人 hexo 博客 co2fe.com 迁移
date: 2017-10-11 20:53:02
Tomcat Session Clustering的更多相关文章
- Nginx+Tomcat+Session 高性能群集搭建
随着IT行业的发展,linux服务器在企业中应用广泛,人们对linux上的应用服务要求也越来越高,早先的apache服务器.apache有优点也 有不足,apache渐渐不能满足人们的要求,目前ngi ...
- tomcat session cluster
Session的生命周期 以前在学习的时候没怎么注意,今天又回过头来仔细研究研究了一下Session的生命周期. Session存储在服务器端,一般为了防止在服务器的内存中(为了高速存取),Sessi ...
- 【Tomcat】Tomcat Session在Redis共享
参考的优秀文章 Redis-backed non-sticky session store for Apache Tomcat 简单地配置Tomcat Session在Redis共享 我使用的是现有的 ...
- Tomcat session集群
author:JevonWei 版权声明:原创作品 环境 tomcatA 172.16.253.108 tomcatB 172.16.253.105 代理服务器 172.16.253.191 Tomc ...
- Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享
Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...
- tomcat session Memcache 共享
背景 这次做的这个项目并发还真是挺高,单表一天产生百万条记录不在话下.结果导致运行过程中经常丢失数据,卡.慢等.开来终于要搞一次负载均衡了,之前实验学习了不少,但是没有在项目中实际用过,因为并发量不大 ...
- Session会话保持机制的原理与Tomcat Session共享的几种实现方式(Session Cluster、memcached+MSM)
一.Session的定义 在计算机科学中,特别是在网络中,session是两个或更多个通信设备之间或计算机和用户之间的临时和交互式信息交换.session在某个时间点建立,然后在之后的某一时间点拆除. ...
- 关于tomcat session机制梳理
一道题目引起的思考:"tomcat里怎样禁止服务端自己主动创建session". 1背景知识: 要说tomcat的机制.先从session说起. http是无状态协议(http详 ...
- nginx tomcat session丢失的问题
nginx反向代理tomcat,出现seesion获取不到的问题. 网上搜索到的解决方案大多是集群tomcat共享session共享的问题,但我这个只有一台tomcat服务器,不涉及到服务器集群问题. ...
随机推荐
- AC日记——[SDOI2011]染色 洛谷 P2486
题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例#1: 6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C ...
- L1-5. A除以B【一种输出格式错了,务必看清楚输入输出】
L1-5. A除以B 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 真的是简单题哈 —— 给定两个绝对值不超过100的整数A和 ...
- 【APIO2015】Palembang Bridges
题目描述 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 $A$ 和区域 $B$. 每一块区域沿着河岸都建了恰好 $1000000001$ 栋的建筑,每条岸边的建筑都从 $0$ 编号到 $100 ...
- Java文件夹操作,判断多级路径是否存在,不存在就创建(包括windows和linux下的路径字符分析),兼容Windows和Linux
兼容windows和linux. 分析: 在windows下路径有以下表示方式: (标准)D:\test\1.txt (不标准,参考linux)D:/test/1.txt 然后在java中,尤其使用F ...
- 转:android studio入门合集
http://blog.csdn.net/column/details/zsl-androidstudio.html
- visual c++ 2013进行MySQL编程(ODBC) -- (一) 套装安装
最近写了有些技术类文章了,只因为最近研究多了些东西,有一些项目用到了,所以自己记录一下,怕自己忘记,如果有哪位同学有自己的见解,可以邮件或者回复,技术类的探讨,不管对否,都是欢迎的. 操作之前,必须安 ...
- python正则方法
通过正则替换字符串 res=re.sub(正则,newString,srcString)//返回替换后的字符串 res,m=res.subn(正则,newString,srcString)//返回替换 ...
- selenium之 文件上传方法
文件上传是所有UI自动化测试都要面对的一个头疼问题 首先,我们要区分出上传按钮的种类,大体上可以分为两种,一种是input框,另外一种就比较复杂,通过js.flash等实现,标签非input 我们分别 ...
- 《Python核心编程》数字类型
1.数字类型简单介绍 Python中数字类型包含:整型.长整型.布尔型.双精度浮点型.十进制浮点型.复数.这些数字类型都是不可变类型.也就是说,改变了数字的值会生成新的对象. 在Python中删除数字 ...
- 如何成为一个Linux内核开发者
你想知道如何成为一个Linux内核开发者么?或者你的老板告诉你,“去为这个设备写一个Linux驱动.“这篇文档的目的,就是通过描述你需要 经历的过程和提示你如何和社区一起工作,来教给你为达到这些目的所 ...