Using memcached to cache TOMCAT7 session information

Source: Internet
Author: User
Tags session id nginx reverse proxy

TOMCAT7 + memcached

All systems use centos7.1

Each node is configured with the Hosts file.
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 reverse proxy to two tomcat nodes
Node2, Node3-to-Tomcat stores session information in two 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"%>
<body>
<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>
============================
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" >
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 browser Access 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"%>
<body>
<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>
============================
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" >
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 browser Access http://172.16.92.3:8080/
TomcatB.centos7.com
Session ID b22e48aa2a89bce2bad4021d9e912e03-n2
Created on 1446005950406










================ Node1 172.16.92.1/16 ===================
Nginx to do tomcat1,2 reverse proxy and load balancer

Installing the Epel source
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--Delete all contents
: Set paste--and paste the following
################
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 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 502 503 504/50x.html;
Location =/50x.html {
}
}
}
==================end of Nginx.conf
Nginx-t
> appear ok and successful related sentences there's no problem.
Systemctl Start Nginx.service
ss-tanl | grep 80



Windows Explorer accesses http://172.16.92.1/index.jsp, polling on the following two pages, but the session ID is the same
-----------------------------------------------------------------------
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 Explorer accesses http://172.16.92.1/index.jsp, polling on the following two pages,
But the session ID is the same, and the session ID is obtained from another cache server.
-----------------------------------------------------------------------
TomcatA.centos7.com
Session ID de4fb78238a882475c86e1735779f5c9-n1
Created on 1446007835727
-----------------------------------------------------------------------
TomcatB.centos7.com
Session ID de4fb78238a882475c86e1735779f5c9-n1
Created on 1446007835727
-----------------------------------------------------------------------


Using memcached to cache TOMCAT7 session information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.