Nginx+tomcat+memcached Implementing session Sharing

Source: Internet
Author: User
Tags serialization sessions

There are many ways to share the session, for the technical implementation of the Web application cluster, the biggest difficulty is how to maintain the consistency of data between multiple nodes in the cluster, session sessions are the most important piece of these data, there are basically two ways to achieve this:

One is to put all the session data into a server or database, all the nodes in the cluster to access the session server to obtain data, and the other is a cluster of all nodes in the session data synchronization between the copy, Any node has all the session data saved.

The following is a specific session synchronization sharing scheme, through the analysis of their respective advantages and disadvantages to find out its adaptation scenario:

1) Implementation of Seesion replication using the server's own cluster features

Most application servers provide session replication functionality in the cluster. such as Tomcat,jboss

Advantages: Simple

Cons: Performance decreases as the server grows, causing broadcast storms to be easily

2) Save session with Database

Database read-write for each request, and HA to solve the database single point of failure

3) Use shared storage to save session

Shared storage is a single point that can be resolved through raid

4) Use memcached to save session

Similar to the database, but because of memory access, the performance is much better than the database, but if the memcached server down, the session will be lost, in the extended Tomcat 6.x, using mecached storage session


System environment: rhel6.5 x64 SELinux and iptables disabled
Host role:

Node1:192.168.0.91:nginx Tomcat memcached
Node2:192.168.0.92:tomcat memcached
Software Download: http://www.nginx.org
Http://code.google.com/p/memcached-session-manager


How the Experiment works:

<T1> <T2>
. \     /   .

. \ /     .

. / \     .

. /     \   .

<M1> <M2>
Tomcat-1 stores the session on Memcached-2 (T2), and T1 stores the session in Memcached-1 (M1 T1 failovernode) only if M2 is not available. The advantage of using this configuration is that when both T1 and M1 crash, the session sessions are not lost and the single point of failure is avoided.

1. Install Tomcat at the same time on two nodes Node1 and Node2

SH jdk-6u26-linux-x64.bin
MV jdk1.6.0_26//USR/LOCAL/JDK
Vi/etc/profile//Setting environment variables for Java
Export JAVA_HOME=/USR/LOCAL/JDK
Export classpath=: $JAVA _home/lib
Export path= $PATH: $JAVA _home/bin
Source/etc/profile
Tar zxf apache-tomcat-7.0.8.tar.gz-c/usr/local//Unzip to/usr/local directory
Mv/usr/local/apache-tomcat-7.0.8/usr/local/tomcat


There are 4 official recommendations for the session's serialization scheme:
1. Java serialization
2. Msm-kryo-serializer
3. Msm-javolution-serializer
4. Msm-xstream-serializer
One of the best-performing serialization schemes is Kryo, which we use kryo approach.


Session sessions are maintained we use MSM (Memcached-session-manager), which requires some class libraries, so the following packages are placed in the/usr/local/tomcat/lib directory
Kryo-1.03.jar
Kryo-serializers-0.8.jar
Memcached-2.5.jar

Memcached-session-manager-1.5.1.jar

Memcached-session-manager-tc7-1.5.1.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.5.1.jar
Reflectasm-0.9.jar

vi/usr/local/tomcat/conf/context.xml    // Changes under this file can immediately drop the session into memcached
<context>
......              //Add the following fields
<manager classname= " De.javakaffee.web.msm.MemcachedBackupSessionManager "
memcachednodes=" n1:192.168.0.91:11211,n2 : 192.168.0.92:11211 " //Add memcached IP and port number at node
failovernodes=" N1 "                                #在 on Node2 This is set to "N2"
requesturiignorepattern= ". *\. ( ICO|PNG|GIF|JPG|CSS|JS) $ "
transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "
/>
</context>

/usr/local/tomcat/bin/startup.sh #启动 Tomcat

2. Install memcached on two nodes
Yum Install Memcached-y
Service memcached start//11211 Port Open

3. Installing Nginx on the node Node1

Yum install-y pcre-devel Openssl-devel
Tar zxf nginx-1.0.6.tar.gz
Tar zxf nginx-sticky-module-1.0.tar.gz-c nginx-1.0.6
Useradd-s/sbin/nologin Nginx
CD nginx-1.0.6
./configure--user=nginx--group=nginx–with-http_ssl_module--with-http_stub_status_module-add-module= nginx-sticky-module-1.0/
Make && make install
Note: Nginx-sticky-module for nginx third-party module, so that nginx support sticky mode, so-called sticky mode
means that the same user's access requests are sent to the same tomcat instance for processing.

HTTP {
Upstream server.example.com {
Sticky
Server 192.168.0.91:8080;
Server 192.168.0.92:8080;
}

server {
Listen
SERVER_NAME 80;
desktop91.example.com;
Location/{
root HTML;
Index index.html index.htm;
}
Error_page 502 503 504/50x.html

Location =/50x.html {
root HTML;
}
Location ~ \.jsp$ {#所有 JSP page to tomcat processing, static and dynamic separation
Proxy_pass http://server.example.com;
}
}
}

4. Below is the test page, save to/usr/local/tomcat/webapps/root/test.jsp//define a session window

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.util.*"%>
<body>
Server Info:
<%
Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br>");%>
<%
Out.println ("<br> ID" + session.getid () + "<br>");
String dataname = Request.getparameter ("Dataname");
if (dataname! = null && dataname.length () > 0) {
String DataValue = Request.getparameter ("DataValue");
Session.setattribute (Dataname, DataValue);
}
Out.print ("<b>session list</b>");
Enumeration E = Session.getattributenames ();
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Session.getattribute (name). toString ();
OUT.PRINTLN (name + "=" + value+ "<br>");
SYSTEM.OUT.PRINTLN (name + "=" + value);
}
%>
<form action= "test.jsp" method= "POST" >
Name:<input type=text size=20 name= "Dataname" >
<br>
Key:<input type=text size=20 name= "DataValue" >
<br>
<input type=submit>
</form>
</body>

Tail-f logs/catalina.out//view Log

Access to http://server.example.com/test.jsp, when different host accesses are scheduled to be processed on different tomcat instances
Requests from the same host are handled by the same Tomcat instance, where you drop the currently responding Tomcat real
Example, Nginx automatically dispatches the user's request to another Tomcat instance, and the session is not discarded.




This article is from the "8397752" blog, please be sure to keep this source http://8407752.blog.51cto.com/8397752/1685410

Nginx+tomcat+memcached Implementing session Sharing

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.