搭建 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

转载于:https://www.cnblogs.com/manastudent/p/10190916.html

Tomcat Session Clustering相关推荐

  1. 基于MSM 的tomcat session 共享

    一  ,是基于tomcat cluster 来配置,当然这个配置也十分的简单,但是tomcat 官网建议tomcat cluster 配置,tomcat 不能超过4台,如果超过四台tomcat,不仅容 ...

  2. Memcached + MSM 实现Tomcat Session保持

    Memcached + MSM 实现Tomcat Session保持 tomcat memcached 大纲 前言 Memcached介绍 MSM介绍 实验拓扑 实验环境 实验步骤 安装配置Tomca ...

  3. Nginx负载均衡+tomcat+session共享

    为什么80%的码农都做不了架构师?>>>    本文,是笔者工作之余写的,第一是把之前打系统框架的步骤记录下来.第二是将这个过程,谈不上经验,奉献给正在撘这种框架遇到各种bug,各种 ...

  4. Tomcat源码解析七:Tomcat Session管理机制

    前面几篇我们分析了Tomcat的启动,关闭,请求处理的流程,tomcat的classloader机制,本篇将接着分析Tomcat的session管理方面的内容. 在开始之前,我们先来看一下总体上的结构 ...

  5. Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享

    转载自  Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 一.Session共享使用tomcat-cluster-redis-session-mana ...

  6. tomcat源码分析_CVE-2020-9484 tomcat session反序列化漏洞分析

    作者:N1gh5合天智汇 title: CVE-2020-9484 tomcat session反序列化漏洞分析 tags: CVE,Tomcat,反序列化 grammar_cjkRuby: true ...

  7. 阿里架构师手写Tomcat——Session源码解析

    在 web 开发中,我们经常会用到 Session 来保存会话信息,包括用户信息.权限信息,等等.在这篇文章中,我们将分析 tomcat 容器是如何创建 session.销毁 session,又是如何 ...

  8. idea 配置 tomcat session 无法活化

    idea 配置 tomcat session 无法活化 参考网址: https://blog.csdn.net/weixin_44142032/article/details/89004986?ops ...

  9. Nginx + Tomcat + Session学习 - ﹎敏ō - 博客园

    Nginx + Tomcat + Session学习 - ﹎敏ō - 博客园 Nginx + Tomcat + Session学习 - ﹎敏ō - 博客园 Nginx + Tomcat + Ses ...

  10. tomcat session 共享

    1. nginx+tomcat7+memcached 安装JDK7 sudo apt-get install java7-jdk 安装tomcat7 Tomcat7下载地址 http://mirror ...

最新文章

  1. 每个网站SEO优化人员都要熟知的三大图片优化技巧
  2. Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]
  3. C# partial 关键字的使用
  4. JSON中的JSON.parseArray()、JSON.parseObject()、JSON.tojsonString()
  5. php异步发送邮件,php通过fsockopen异步发送邮件
  6. NO.30 禅道项目管理软件扩展机制简介
  7. 遇见Python.h: No such file or directory的解决方法
  8. 11-实战模拟DRBD项目案例环境准备
  9. JavaScript生成随机颜色的代码
  10. 德标螺纹规格对照表_德标DIN934六角螺母,不锈钢六角螺母DIN934
  11. 傲梅分区助手克隆Linux硬盘,傲梅分区助手复制磁盘或克隆磁盘到另外磁盘
  12. spurious retransmission timeouts理解
  13. php 网页截屏,php怎样实现网页截图
  14. win10 添加打印机页面打不开,闪一下就没有了 怎么办
  15. GBase 8c产品高级特性介绍
  16. mysql 1033_MySQL ERROR 1033 (HY000): Incorrect information in file. 处理一例
  17. 北京 买房 提取 公积金
  18. Java键盘监听器KeyListener
  19. 人脸识别:人脸数据集
  20. MIT计算机科学录取分数线,美国麻省理工学院分数线 美国麻省理工学院录取要求...

热门文章

  1. Laravel5.4 导出Excel表格
  2. 吐槽下CSDN编辑器
  3. 关于opencv设置视频的属性无效问题
  4. Go语言中Path包用法
  5. 如何在Windows XP下安装Windows2000
  6. vue 静态资源文件夹src下的assets 和static的区别
  7. 终于将 SQL Server 成功迁移至 MySQL8.0 啦!!!
  8. GetAsyncKeyState() 0x8000
  9. 洗衣机一边进水一边出水 更换排水阀皮碗
  10. Ubuntu中启用关闭Network-manager网络设置问题! 【Server版本】