基于Memcached的session server

  • session server:将Tomcat的session信息保存在Memcached中,实现session server

    • 前提:

      • 两个Tomcat节点,两个Memcached节点,一个nginx节点;Tomcat需要使用到Memcached-session-manager这个Java类,即上面所说的调用了Memcached提供的API的模块,以达到Memcached通信的目的
      • Add memcached-session-manager jars to tomcat:

        • Memcached-session-manager项目下载地址:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration;下载如下jar文件至各Tomcat节点的$CATALINA_HOME/lib目录中,其中的memcached-session-manager-${version}.jar的版本号和Tomcat的版本号是两码事,它的${version}要和memcached-session-manager-tc${6,7,8}-${version}.jar中的${version}一致,tc${6,7,8}对应Tomcat版本号
        • Independent of the chosen serialization strategy you always need the memcached-session-manager-${version}.jar and either memcached-session-manager-tc6-${version}.jar for tomcat6, memcached-session-manager-tc7-${version}.jar for tomcat7 (attention: tomcat 7.0.23+), memcached-session-manager-tc8-${version}.jar for tomcat8 or memcached-session-manager-tc9-${version}.jar for tomcat9.
        • If you're using memcached, you also need the spymemcached-${version}.jar. Tested up to v2.12.3.
        • msm-javolution-serializer-${version}.jar序列化工具
        • Please download the appropriate jars and put them in $CATALINA_HOME/lib/

 

  • 分别在两个Tomcat上的某host中的context容器中创建会话管理器,如下所示:

    • <Context path=”/NAME1” docBase=”/usr/local/tomcat/webapps/NAME2” reloadable=”true”> 这里注意NAME1和NAME2不能相同,而且docBase还需要是绝对路径

<Manager className=”de.javakaffee.web.msm.MemcachedBackupSessionManager”

memcachedNodes=”n1:172.16.8.93:11211,n2172.16.8.94:11211” 实现Tomcat和后端的两个memcached节点通信,当Tomcat将缓存存到一个节点后,自动将副本复制到另外一个节点

failoverNodes=”n1” 指明n1为备用节点

requestUriIgnorePattern=”.*\.(ico|png|gif|jpg|css|js)$” 忽略这些结尾的URL,不以缓存

transcoderFactoryClass=”de.javakaffee.web.msm.serializer.javolution.JavolutionTranscodeFactory” /> 指定序列化工具

</Context>

  • 分别为两个context提供测试页面:

    • # 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 | TomcatB</title></head>

  <body>

    <h1><font color="{red | bule}">{TomcatA | TomcatB}.magedu.com</font></h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("magedu.com","magedu.com"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

  • nginx中的配置:

    • vim /etc/nginx/nginx.conf

http {

upstream tservers {

       server 172.16.8.93:8080 weight=2;

       server 172.16.8.94:8080 weight=3;

    }

}

  • vim /etc/nginx/conf.d/tomcat.conf

server {

        listen 80;

        location ~* \.(jsp|do)$ {

                proxy_pass http://tservers;

        }

}

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/93920

(0)
DPingDPing
上一篇 2018-03-31
下一篇 2018-03-31

相关推荐