[Tomcat] Session sharing between N tomcats

        For high-traffic, high-concurrency websites or web applications, the most common solution at present should be to use load balancing for server clustering, such as the popular nginx+memcache+tomcat. After the cluster, for example, we have N Tomcats. When users visit our website, the first request may be distributed to tomcat1, and the second request is distributed to tomcat2. Friends who have experience in web development know that session What are the consequences of inconsistency, so we need to solve the problem of session sharing between multiple tomcats.

 

       Create a new directory called tomcat_cluster (meaning tomcat cluster), directly decompress the downloaded tomcat, and rename it to tomcat1

       Prepare a simplest web project (create a deployment in the IDE and copy it out), and modify the contents of index.jsp as follows:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
      
    <title>Session共享</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  
  </head>  
    
  <body>  
      
        SessionID:<%=session.getId()%>  
        <BR>  
        SessionIP:<%=request.getServerName()%>  
        <BR>  
        SessionPort:<%=request.getServerPort()%>  
        <%  
        out.println("This is Tomcat Server 11111");  
        %>  
  </body>  
</html>  

The <distributable /> element         needs to be added to web.xml , as follows:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>webDemo</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<distributable />
</web-app>

        In this way, the basic work is done. Next, we need to modify the configuration file of tomcat. We open the server.xml file under conf and find the following line:

 

 

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

        Without any modification, add the following code below this line:

 

 

<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" <!--Reserved ip for broadcast-->
                       port="45564"  
                       frequency="500"  
                       dropTime="3000"/>  
           <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
                     address="auto"  
                     port="4000" <!--If two tomcats on the same machine do the load, this port cannot be repeated -->
                     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>

 This is the cluster configuration that comes with tomcat. We can see the relevant precautions in cluster-howto.html in the official documentation of tomcat. One of them needs to be paid attention to:

 

Make sure your web.xml has the <distributable/> element

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529648&siteId=291194637