使用memcached缓存tomcat7会话信息

  1. 云栖社区>
  2. 博客>
  3. 正文

使用memcached缓存tomcat7会话信息

余二五 2017-11-14 14:32:00 浏览862
展开阅读全文
tomcat7 + memcached

全部系统采用centos7.1 

各节点配置好hosts文件
cat /etc/hosts
172.16.92.1    node1.centos7.com    node1
172.16.92.2    node2.centos7.com    node2
172.16.92.3    node3.centos7.com    node3
172.16.92.6    node6.centos7.com    node6
172.16.92.7    node7.centos7.com    node7

node1 --> nginx反向代理至两个tomcat节点
node2, node3 --> tomcat把session信息存储在两个memcached中
node6, node7 --> memcached ()


================ node6 172.16.92.6/16 ===================
mount /dev/cdrom /mnt/cdrom/
yum -y install memcached
systemctl start memcached
netstat -taunl | grep 11211


================ node7 172.16.92.7/16 ===================
mount /dev/cdrom /mnt/cdrom/
yum -y install memcached
systemctl start memcached
netstat -taunl | grep 11211


================ node2 172.16.92.2/16 ===================

mount /dev/cdrom /mnt/cdrom/
yum -y install tomcat
mkdir -pv /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}
vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.centos7.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("centos7.com","centos7.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
============================
cd /usr/share/tomcat/lib
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/1.8.3/memcached-session-manager-tc7-1.8.3.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.8.3/memcached-session-manager-1.8.3.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/1.8.3/msm-javolution-serializer-1.8.3.jar
wget http://mirrors.ibiblio.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar
wget http://www.java2s.com/Code/JarDownload/javolution/javolution-5.4.3.1.jar.zip
unzip javolution-5.4.3.1.jar.zip

cp /etc/tomcat/server.xml{,.bak}
vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="node2.centos7.com">
<Host name="node2.centos7.com"  appBase="/data/webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/data/webapps/ROOT" reloadable="true">
                <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                        memcachedNodes="n1:172.16.92.6:11211,n2:172.16.92.7:11211"
                        failoverNodes="n1"
                        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>
        </Context>
============================
systemctl start tomcat.service


windows浏览器访问 http://172.16.92.2:8080/
TomcatA.centos7.com
Session ID     AB327A3B9B86D87D51CB2D31297D2C4A-n2
Created on     1446005990634







================ node3 172.16.92.3/16 ===================

mount /dev/cdrom /mnt/cdrom/
yum -y install tomcat
mkdir -pv /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}
vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.centos7.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("centos7.com","centos7.com"); %>        
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>    </table>
  </body>
</html>
============================
cd /usr/share/tomcat/lib
wget http://www.java2s.com/Code/JarDownload/javolution/javolution-5.4.3.1.jar.zip
wget http://mirrors.ibiblio.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/1.8.3/msm-javolution-serializer-1.8.3.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.8.3/memcached-session-manager-1.8.3.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/1.8.3/memcached-session-manager-tc7-1.8.3.jar
unzip javolution-5.4.3.1.jar.zip

cp /etc/tomcat/server.xml{,.bak}
vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="node3.centos7.com">
<Host name="node3.centos7.com"  appBase="/data/webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/data/webapps/ROOT" reloadable="true">
                <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                        memcachedNodes="n1:172.16.92.6:11211,n2:172.16.92.7:11211"
                        failoverNodes="n1"
                        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>
        </Context>
============================
systemctl start tomcat.service



windows浏览器访问 http://172.16.92.3:8080/
TomcatB.centos7.com
Session ID     B22E48AA2A89BCE2BAD4021D9E912E03-n2
Created on     1446005950406










================ node1 172.16.92.1/16 ===================
nginx做tomcat1,2的反向代理和负载均衡

安装epel源
wget -O /etc/yum.repos.d/epel7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install nginx
cd /etc/nginx
cp nginx.conf{,.bak}
vim nginx.conf
:1,$d -- 删除全部内容
:set paste --> 再粘贴如下内容
################
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    upstream tcsrvs {
        server node2.centos7.com:8080;
        server node3.centos7.com:8080;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
        location ~* \.(jsp|do)$ {
                proxy_pass http://tcsrvs;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
==================end of nginx.conf
nginx -t
>出现ok和successful相关句子就没问题了
systemctl start nginx.service
ss -tanl | grep 80



windows浏览器访问 http://172.16.92.1/index.jsp, 在下面两个页面轮询,但是会话ID是一样的
-----------------------------------------------------------------------
TomcatA.centos7.com
Session ID     DE4FB78238A882475C86E1735779F5C9-n2
Created on     1446006726402
-----------------------------------------------------------------------
TomcatB.centos7.com
Session ID     DE4FB78238A882475C86E1735779F5C9-n2
Created on     1446006726402
-----------------------------------------------------------------------





================ node7 172.16.92.6/16 ===================
systemctl stop memcached

windows浏览器访问 http://172.16.92.1/index.jsp, 在下面两个页面轮询,
但是会话ID是一样的, 并且会话ID是从另一个缓存服务器取得.
-----------------------------------------------------------------------
TomcatA.centos7.com
Session ID     DE4FB78238A882475C86E1735779F5C9-n1
Created on     1446007835727
-----------------------------------------------------------------------
TomcatB.centos7.com
Session ID     DE4FB78238A882475C86E1735779F5C9-n1
Created on     1446007835727
-----------------------------------------------------------------------







本文转自 zhuhc1988 51CTO博客,原文链接:http://blog.51cto.com/changeflyhigh/1707491,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
余二五
+ 关注