Tomcat implements session persistence

There are three ways to keep a Tomcat session 

1. Session sticky: through the load balancing algorithm of the load balancer, such as: source address hash and cookie

2、session cluster: shared storage for sessions, login information is stored in the database

Synchronize all sessions by defining Cluster Node DeltaManager Session Manager Full Node Replication
Disadvantages: suitable for small scale, one update in the cluster also needs to update the session multicast replication to all nodes in the cluster 

3, session serve: Implemented through a memory-level non-relational database
Based on the server-side implementation of session storage in memcache
msm memcache session management tool
memcached-session-manager project address, http://code.google.com/p/memcached-session-manager/, https://github.com/magro/memcached-session-manager


A session server that implements session session retention

One: Premise:
two tomcat nodes: 172.17.250.111 (tomcatA.magedu.com), 172.17.250.112 (tomcatB.magedu.com)
a memcached node: 172.17.250.113
a load balancing node: 172.17.250.114

Clients-->172.17 .250.114-->(tomcatA, tomcatB)

2. Download the following jar files to the lib directory under the tomcat installation directory of each tomcat node, where ${version} should be replaced with the version number you need, and tc${6,7,8} should be replaced with tomcat version with the same version number.

For example I use


    memcached-session-manager-${version}.jar
    memcached-session-manager-tc${6,7,8}-${version}.jar
    spymemcached-${version}.jar
    msm-javolution-serializer-${ version}.jar
    javolution-${version}.jar


3. Define a context container for testing on a host on two tomcats, and create a session manager in it, as shown below:
           <Context path= "/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true">
              <Manager lsclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                memcachedNodes="n1:172.17.250.113:11211"
                  failoverNodes="n1"
                  requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
              />
             </Context>

Fourth, install memcache on 172.17.250.113 and start the service


五、分别为两个context提供测试页面:
tomcatA:
# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.baidu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("baidu.com","baidu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>


tomcatB:
# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.baidu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("baidu.com","baidu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>


六、在172.17.250.114上配置反向代理的负载均衡内容,类似如下所示:

http段:

upstream tomcat_cluster {

  
server 172.16.100.7:8080  weight=2;
server 172.16.100.8:8080  weight=1;
}

server段:
location / {
  
proxy_pass http://tomcat_cluster;

}



测试结果,在浏览器中访问http://172.17.250.114/test,结果如下所示,其session ID在负载均衡环境中保持不变。

TomcatA.baidu.com



TomcatB.baidu.com






Guess you like

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