日期:2014-05-17  浏览次数:20929 次

Apache Tomcat 5.5 Servlet/JSP 容器 怎样配制集群/Session复制

源文:http://www.jaxmao.org/tomcat-docs/cluster-howto.html

Apache Tomcat 5.5 Servlet/JSP 容器

怎 样配制集群/Session复制


快速入门

要在Tomcat 5.5容器里进行session复制,必须完成下列步骤:

  • 你的所有会话属性值必须实现java.io.Serializable
  • 把server.xml文件里的群集(Cluster )元素的注释取消(Uncomment)
  • 把server.xml文件里的Valve(ReplicationValve) 元素注释取消(Uncomment)
  • 如果有多个Tomcat实例在一台机器上运行,确保每个实例的tcpListenPort 属性值是不冲突的。
  • 确定 web.xml 中含有 <distributable/> 元素, 或者设置 <Context distributable="true" />
  • 确定设置好Engine元素的 jvmRoute 的属性值: <Engine name="Catalina" jvmRoute="node01" >
  • 确定所有节点拥有相同的时间, 并且通过网络时间服务(NTP)同步操作系统的时间!
  • 确定负载均衡器已经配置为黏性会话模式.

负载均衡可以有很多种方式实现,请参考负载均衡 章节。

注意:记住你的会话状态是被一个cookie跟踪的,所以你的URL从外面看必须是相同的,否则一个新的session就会被产生。

注意:集群支持目前需要JDK 1.4或其后版本。

概要

要在Tomcat里实现会话session复制,下面三种方法都可以实现相同效果:

  1. 使用session 持久化, 并将 session 保存到一个共享的文件系统(持久管理器 + 文件存储)(PersistenceManager + FileStore)
  2. 使用session 持久化, 并将 session 保存到一个共享的 数据库 (持久管理器 + JDBC存储) (PersistenceManager + JDBCStore)
  3. 使用in-memory-replication,使用和Tomcat 5自带的SimpleTcpCluster(server/lib/catalina-cluster.jar)

In this release of session replication, Tomcat performs an all-to-all replication of session state. This is an algorithm that is only efficient when the clusters are small. For large clusters, the next release will support a primary-secondary session replication where the session will only be stored at one or maybe two backup servers. Currently you can use the domain worker attribute (mod_jk > 1.2.8) to build cluster partitions with the potential of very scaleable cluster solution. In order to keep the network traffic down in an all-to-all environment, you can split your cluster into smaller groups. This can be easily achieved by using different multicast addresses for the different groups. A very simple setup would look like this:

DNS 轮询 
| 
负载均衡器 
/ \ 
集群1  集群2 
/ \ / \ 
Tomcat1 Tomcat2 Tomcat3 Tomcat4

What is important to mention here, is that session replication is only the beginning of clustering. Another popular concept used to implement clusters is farming, ie, you deploy your apps only to one server, and the cluster will distribute the deployments across the entire cluster. This is all capabilities that can go into with the FarmWarDeployer (s. cluster example at server.xml )

下一章将深入讨论sessio