Tomcat session共享配置

准备工作

  1. 192.168.220.123 TomcatA

  2. 192.168.220.126 TomcatB

  3. 192.168.220.126 Nginx

  4. 添加session共享配置(TomcatA和TomcatB 相同)

    <Engine name="Catalina" defaultHost="localhost">

          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
        <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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;.*\.jsp"/>          
          <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>  
  1. 部署测试应用-Tomcat A
    $TOMCAT_HOME/webapps/testapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>Tomcat A</title></head>
<body>
<h1><font color="blue">Tomcat A </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
  1. 部署测试应用-Tomcat B
    $TOMCAT_HOME/webapps/testapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
  1. 在测试应用中的web.xml 加节点
    路径: $TOMCAT_HOME/webapps/testapp/WEB-INF/web.xml
    <distributable />
  2. 配置Nginx
 upstream site { 
  server 192.168.220.126:9099;
  server 192.168.220.123:9099;
  } 
  server {
  listen 9999;
  server_name 192.168.220.126;
  location / {
  root html;
  index index.html index.htm;
  proxy_pass http://site;
  1. 测试
    启动tomcat A、tomcat B、Nginx
    通过Nginx访问index.jsp,不断刷页面,Tomcat 已换,但Session ID没变。


    tomcatA.jpg

    tomcatB.jpg

总结:看文档时一头雾水,觉得很难,实践起来却很简单,真像文档中所有 给出的基本配置满足大多数人的工作要求。
虽花了很多时间,印象深刻,下次继续这种模式。(理解原理-实现-理解原理)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容