Implement session sharing between N Tomcat

Source: Internet
Author: User
Tags tomcat server



Preface



Reprint please indicate the source: http://blog.csdn.net/wlwlwlwl015/article/details/48160433

For Web sites or Web applications with high traffic, high concurrency, the most common solution is to use load balancing for server clusters, such as the more popular nginx+memcache+tomcat. After the cluster, for example, we have n Tomcat, the user may visit our website the first request to distribute under the TOMCAT1, and the second request is distributed to the TOMCAT2, the friend who has the experience of web development know that this session inconsistency can lead to the consequences, So we need to solve the problem of multiple sessions sharing between Tomcat.



Environmental Preparedness



First we need to download Tomcat, here we choose TOMCAT7 version of the solution:


After downloading, we create a new directory called Tomcat_cluster (Tomcat cluster meaning), directly extract the downloaded Tomcat, and rename it to TOMCAT1, you can see the directory structure as follows:


Prepare one of the simplest Web projects (create a deployment in the IDE, copy it), and modify the contents of the index.jsp as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

So the basic work is done, the following need to modify the Tomcat configuration file, we open the Server.xml file under Conf, find the following line:

<engine name= "Catalina" defaulthost= "localhost" >


There is no need to make any changes, add the following code below this line:

	<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster" channelsendoptions= "8" >
                   <manager classname= "Org.apache.catalina.ha.session.DeltaManager" expiresessionsonshutdown= "false" Notifylistenersonreplication= "true"/> <channel classname= "org.apache.catalina.tribes.g Roup.
                        Groupchannel "> <membership classname=" Org.apache.catalina.tribes.membership.McastService "
                        address= "228.0.0.4" port= "45564" frequency= "500"
                      Droptime= "3000"/> <receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address= "Auto" port= "4000" autobind= "100" selectortimeout= "5000" maxthreads= "6"/> <sender classname= "org.apache.catal Ina.tribes.transporT.replicationtransmitter "> <transport classname=" org.apache.catalina.tribes.transport.nio.PooledParallel Sender "/> </Sender> <interceptor classname=" org.apache.catalina.tribes.group.intercept Ors. Tcpfailuredetector "/> <interceptor classname=" Org.apache.catalina.tribes.group.interceptors.MessageDispa Tch15interceptor "/> </Channel> <valve classname=" Org.apache.catalina.ha.tcp.ReplicationVa Lve "filter=" "/> <valve classname=" Org.apache.catalina.ha.session.JvmRouteBinderValve "/&G"

          T
                    <deployer classname= "Org.apache.catalina.ha.deploy.FarmWarDeployer" tempdir= "/tmp/war-temp/" Deploydir= "/tmp/war-deploy/" watchdir= "/tmp/war-listen/" watchenable D= "false"/> <clusterlistener classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/&
  Gt        <clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/> </cluster&gt
 ;


This is the Tomcat cluster configuration, and we can see the relevant considerations in the cluster-howto.html in the Tomcat official documentation, one of which needs to be noted:

Make sure your web.xml has the <distributable/> element

Obviously, we need to have the <distributable/> element in the Web.xml file of our web project, so make the changes in the Web project we just introduced.

So our basic configuration is complete, because it is a cluster, so copy a tomcat1 in the Tomcat_cluster directory and rename it to Tomcat2.



Note the areas that need to be modified:

1, the first is definitely the port number, all the configuration of port typeface to modify the guarantee of two do not repeat, I will tomcat1 Port modified to 7080,TOMCAT2 Port modified to 9080.

2, modify WebApps under the project index.jsp file, because to test session sharing so it is necessary to distinguish, in the JSP to add a line of static text, I am here with the "This is Tomcat Server 111111" and "This is Tomcat Server 22222 to differentiate, start with two Tomcat to test each, We ran the bin directory under the Startup.bat, found that the first Tomcat boot, the second failed to start, because the startup script (Startup.bat) did not modify, reference the same catalina_home must be error, so please note the 3rd below.

3, for our two Tomcat add two environment variables--catalina_home2, Catalina_home3 respectively point to these two Tomcat installation directory:


The two scripts in the two Tomcat bin directory are then modified separately: Startup.bat and Catalina.bat, replacing%catalina_home% in the two files in tomcat1 with%catalina_home2%. Replace the%catalina_home% in the two files in Tomcat2 with%catalina_home3%, and now start Tomcat, you'll find that two tomcat boots, and the decompression version of Tomcat needs to be configured like this. Check to see if Tomcat can be accessed successfully in the browser:



OK, and then visit our test project to see if the session is consistent:


We can find that our two tomcat under the two items session exactly the same, refresh will not change, so far as we have successfully implemented the session sharing, that is, we Tomcat has successfully completed the task, the remaining to Nginx on the OK. Finally, the two Testdemo web.xml need to add such elements:




Summary



This blog describes the required work before the Tomcat cluster--session shared configuration, as well as the start of two Tomcat decompression version of the need to pay attention to the problem, I hope to see the students have some help, the end.

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.