Analysis of tomcat8+memcached session sharing

Introduction to Memcached:

Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。
Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。
Memcached简洁而强大。它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存的很多问题。它的API兼容大部分流行的开发语言。
本质上,它是一个简洁的key-value存储系统。
一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。

Memcached official website: https://memcached.org/

Features of Memcached:

  • Simple agreement
  • Event handling based on libevent
  • Built-in memory storage method
  • Distributed memcached does not communicate with each other

Environment configuration:

nginx服务器:192.168.117.6
web&memcached服务器TomcatA:192.168.117.4
web&memcached服务器TomcatB:192.168.117.5

Note: All three virtual machines in this experiment need to close selinux and firewall to avoid unnecessary errors.

systemctl stop firewalld #关闭防火墙
systemctl disable firewalld #关闭防火墙开机自启动
getenforce #查看selinux的状态
setenforce 0 #临时调整selinux为宽松模式
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #永久关闭selinux

Web server configuration:

Note: Java environment is required to run tomcat service.

[root@tomcat ~]# yum list all | grep jdk  
#查看java软件包,本实例以java-1.8.0-openjdk为例
[root@tomcat ~]# yum install java-1.8.0-openjdk -y
[root@tomcat ~]# java -version  #查看java版本

This example takes apache-tomcat-8.0.30.tar.gz as an example

Tomcat official website: https://tomcat.apache.org/

Note: The configuration of the following two tomcats, except the web interface and server.xml configuration are different, other operations are the same

[root@tomcat ~]# cd /usr/local/src/ #此目录一般用于存放源码包,可以把下载的apache-tomcat-8.0.30.tar.gz放到此目录下
[root@tomcat src]# tar xf apache-tomcat-8.0.30.tar.gz -C /usr/local/  
#解压tar格式压缩包,-C(大写的C)选项,指定解压目录
[root@tomcat src]# cd ..  #..代表上级目录
[root@tomcat local]# ls
apache-tomcat-8.0.30  bin  etc  games  include  lib  lib64  libexec  sbin  share  src  
[root@tomcat local]# ln -sv apache-tomcat-8.0.30/ tomcat 
#给apache-tomcat-8.0.30作软链接,可以简单(不精确)的认为软链接之后,两目录完全相同
[root@tomcat local]# cd tomcat/            
[root@tomcat tomcat]# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
#在/etc/profile.d/下创建一个tomcat.sh的文件,用来声明tomcat bin目录下的环境变量,换句话说:bin目录下的所有脚本,可以在任意路径下直接运行。

1、创建web虚拟机特有的目录结构
[root@tomcat ~]# mkdir /data/mywebapps/{lib,classes,WEB-INF,META-INF} -pv
[root@tomcat ~]# tree /data/mywebapps/
/data/mywebapps/
├── classes
├── index.jsp
├── lib
├── META-INF
└── WEB-INF
注:一个webapp目录至少应有此5个文件
[root@tomcat ~]# mkdir /data/logs #提供此web访问日志

2、提供两个web界面
[root@TomcatA~]# vim /data/mywebapps/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="blue">TomcatA.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>

[root@TomcatB~]# vim /data/mywebapps/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="red">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>

3、下载jar包至各tomcat节点的tomcat安装目录下的lib目录中
[root@tomcat ~]# tree jar/
jar/
├── javolution-5.5.1.jar
├── kryo-serializers-0.11.jar
├── memcached-session-manager-1.9.7.jar
├── memcached-session-manager-tc8-1.9.7.jar
├── msm-javolution-serializer-1.9.7.jar
├── msm-kryo-serializer-1.8.1.jar
└── spymemcached-2.12.3.jar

Jar package download address: https://pan.baidu.com/s/1sl9Nob7

4、添加web虚拟机配置文件:
[root@Tomcat A~]# vim /usr/local/tomcat/conf/server.xml
1) <Engine name="Catalina" defaultHost="web.test.com" jvmRoute="TomcatA">
2) <Host name="web.test.com" appbase="/data/mywebapps">
          <Context path=""  docBase="/data/mywebapps" unpackWARs="true"  autoDeploy="true" >
                    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                                    memcachedNodes="n1:192.168.117.4:11211,n2:192.168.117.5:11211"
                                    failoverNodes="n1"
                                    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                                    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
                                />
          </Context>
          <Valve className="org.apache.catalina.valves.AccessLogValve"  directory="/data/logs"
             prefix="webA_access_log" suffix=".txt"
             pattern="%h %l %u %t "%r" %s %b" />
</Host>

[root@TomcatB~]# vim /usr/local/tomcat/conf/server.xml
1)<Engine name="Catalina" defaultHost="web.test.com" jvmRoute="TomcatB">
2)<Host name="web.test.com" appbase="/data/mywebapps">
         <Context path=""  docBase="/data/mywebapps" unpackWARs="true"  autoDeploy="true" >
                      <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                                    memcachedNodes="n1:192.168.117.4:11211,n2:192.168.117.5:11211"
                                    failoverNodes="n1"
                                    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                                    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
                                />
          </Context>
          <Valve className="org.apache.catalina.valves.AccessLogValve"  directory="/data/logs"
             prefix="webB_access_log" suffix=".txt"
             pattern="%h %l %u %t "%r" %s %b" />
</Host>
[root@tomcat ~]# configtest.sh  #查看server.xml配置文件是否有语法错误
[root@tomcat ~]# catalina.sh start  #启动tomcat
[root@tomcat ~]# netstat -tan | grep 8005 #管理jvm虚拟机的端口
[root@tomcat ~]# netstat -tan | grep 8009 #ajp协议端口
[root@tomcat ~]# netstat -tan | grep 8080 #http协议端口

Memcached server configuration:

[root@tomcat ~]# yum install memcached -y
[root@tomcat ~]# systemctl restart memcached.service  
#web服务和memcached服务安装在同一台服务器上,两台web服务器都要安装

Nginx server configuration:

[root@nginx ~]# yum  install nginx -y
[root@nginx ~]# rpm -ql nginx               
#rpm的-ql选项,通常用来查看某软件安装配置的相关文件,而软件的配置文件一般以.conf结尾的文件
[root@nginx ~]# cp /etc/nginx/nginx.conf{,.bak } #备份配置文件,尽可能养成习惯
[root@nginx ~]# vim /etc/nginx/nginx.conf
                upstream tcsrvs {    #upstream定义y一个集群,tcsrvs集群名称(可自定义)
        ip_hash;  #调度算法,也用此算法实现了session 绑定
        server 192.168.117.4:8080; #指定集群成员
        server 192.168.117.5:8080;
    }
                location ~*\.(jsp|do)$ {                          
                proxy_pass http://tcsrvs; #此处的集群名称tcsrvs,一定要与上面定义的保持一致
        }
*            在此配置文件中添加一个location,在server之内,与其他location平行。 
*           ~*\.(jsp|do)$:正则匹配,表示来自客户端所有的以.jsp或.do结尾的URL请求,从而实现动静分离。
*           proxy_pass:将用户的请求代理到proxy_pass后面集群上

[root@nginx ~]# systemctl start nginx.service
[root@nginx ~]# netstat -tan | grep 80 #查看nginx是否正常启动

Client verification (local physical machine):

The
effect of http://192.168.117.6/index.jsp is as follows: Session ID is the same
Analysis of tomcat8+memcached session sharingAnalysis of tomcat8+memcached session sharing

Guess you like

Origin blog.51cto.com/15134854/2678298