Nginx+tomcat+memcached Implementing session Sharing

Source: Internet
Author: User
Tags memcached nginx server tomcat server

I. Working principle and schematic diagram

Working principle
In Tomcatbring your own cache function。 Memcached when a centralized buffer, you can use Tomcat and memcached to make a cross-time data caching mechanism, when the TOMCAT1 or tomcat2 down, you can use the intersection of the M2 in the intersection to extract the data cached in TOMCAT1, and save it in TOMCAT2. If at least one tomcat server is required in the crossover mechanism, the data will not be lost. Tomcat responds directly to the Nginx server. Tomcat first extracts the memcached data from the intersection point. When the memcached of the intersection is down, the data in the memcached in its own server is extracted. There is no synchronization mechanism for M1 and M2.

<strong>nginx Server:172.25.48.1</strong>
Add the sticky module sticky on the Nginx server.
# tar zxf nginx-sticky-module-1.0.tar.gz
# CD nginx-1.8.0
#./configure
--prefix=/usr/local/lnmp/nginx
--with-http_ssl_module
--with-http_stub_status_module--add-module=/nginx-sticky-module-1.0
# Make &amp;&amp; Make install
# vim/usr/local/lnmp/nginx/conf/nginx.conf
HTTP {
Upstream Tomcat {
Sticky
Server 172.25.48.8:8080;
Server 172.25.48.7:8080;
}
server {
Location ~ \.jsp$ {
Proxy_pass Http://tomcat;
}
}
}
# nginx-t
# nginx-s Reload
<strong>172.25.48.8 hosts and 172.25.48.7 build Tomcat servers and set up the Java locale. </strong>
# vim/usr/local/tomcat/webapps/root/tset.jsp
&lt;%@ page contenttype= "text/html; CHARSET=GBK "%&gt;
&lt;%@ page import= "java.util.*"%&gt;
Cluster App teserver Info:
&lt;%
Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "
");%&gt;
&lt;%
Out.println ("
ID "+ session.getid () +"
");
String dataname = Request.getparameter ("Dataname");
if (dataname! = null &amp;&amp; dataname.length () &gt; 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+ "
");
SYSTEM.OUT.PRINTLN (name + "=" + value);
}
%&gt;
Open the browser, enter "172.25.48.1/test.jsp", because the Nginx server uses a sticky module, so press F5 refresh will not be converted, add users and key.
If Tomcat on the 172.25.48.8 host is down, server info becomes 172.25.48.7:8080 and the previously added user is lost.
<strong> when both Tomcat servers are down, an error message is displayed. </strong>
Add Java library files and move Java library file scripts to the/usr/local/lnmp/tomcat/lib/directory to support Java language compilation.
# mv Asm-3.2.jar Minlog-1.2.jar Memcached-session-manager-tc7-1.6.3.jar Kryo-1.04.jar Reflectasm-1.01.jar Kryo-serializers-0.10.jar Msm-kryo-serializer-1.6.3.jar Memcached-session-manager-1.6.3.jar spymemcached-2.7.3.jar/usr/local/tomcat/lib/
# Vim/usr/local/tomcat/conf/context.xml
172.25.48.8 Host:

&lt; Manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "n1:172.25.48.8:11211,n2 : 172.25.48.7:11211 "failovernodes=" N1 "requesturiignorepattern=". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory " /&gt;

&nbsp;

172.25.48.7 Host:

&lt; Manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "n1:172.25.48.8:11211,n2 : 172.25.48.7:11211 "failovernodes=" N2 "requesturiignorepattern=". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory " /&gt;

#/usr/local/tomcat/bin/shutdown.sh
# yum Install memcached-y # #下载集中式缓存器memcached
#/etc/init.d/memcached Start
# tail-f/usr/local/tomcat/logs/catalina.out # #查看tomcat的日志文件
If the following information appears, the memcached is turned on successfully:
Info:memcachedsessionservice finished initialization, sticky true, operation timeout, with node IDs [N2] and Failove R node IDs [N1]
#/usr/local/lnmp/tomcat/bin/startup.sh # #开启tomcat服务器
Open the browser, enter "http://172.25.48.1/test.jsp", find the 172.25.48.8 host response,
Add name and key.
User1 AAA
User2 BBB
The 172.25.48.8 Tomcat is down, the browser is refreshed, and the 172.25.48.7 host response is displayed, and the previously added user and password remain.
The result shows that after 172.25.48.8 Tomcat is down, the name and key previously added are cached in the memcached service in 172.25.48.8 and 172.25.48.7, and when the name and key are added again, it is automatically converted to the to in the 172.25.48.7 host. MCAT, because of the time-crossing mechanism, 172.25.48.7 Tomcat obtains data from memcached in 172.25.48.8 and caches it in 172.25.48.7 Tomcat and memcached.
Add name and key again.
User3 CCC
Turn off the memcached service for 172.25.48.7 and add the name and key:
User4 DDD
Open the 172.25.48.8 host's Tomcat server, refresh the browser, the results show that the previously added User1,user2,user3 are saved, not lost.
Take 172.25.48.8 tomcat down and turn on 172.25.48.7 Tomcat again to add name and key:
User5 eee
The results show that the previous name and key did not disappear, and that the server info was converted to 172.25.48.7.
When 172.25.48.8 of Tomcat and Memcached and 172.25.48.7 's memcached are down, add name and key again:
User6 FFF
The results show that the name and key you added earlier are missing, but the odds are low.

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.