Tomcat three ways to achieve session retention, using MSM to build JSP Web site

Source: Internet
Author: User
Tags server port jcenter

Tomcat is a simple analogy to PHP's functionality, mainly to implement the compilation of Java programs, the final presentation to the user is HTML-formatted code, using the user can be accessed in the browser. Tomcat is developed in the Java language, so it relies on Java's virtual machine (JVM).

First, using the front-end scheduler implementation to achieve session maintenance

The implementation of the principle, such as the session is maintained using the front-end scheduler implementation. For example, with Ngnix scheduling, the Ip_hash algorithm can be implemented.

650) this.width=650; "title=" 1 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "1" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410274gPHj.png" width= "654" height= "356"/>

Configuration steps: 1, installation configuration Tomcat
Operating system:  centos 6,5 target host: 172.16.10.122  172.10.10.133 First step: Install the Java Virtual machine  ###  get the JDK software   jdk-6u31-linux-x64-rpm.bin  here: The corresponding JDK version has the bin version and the RPM version, can be downloaded according to their own needs. is the:###  installation step chmod +x jdk-6u31-linux-x64-rpm.bin./jdk-6u31-linux-x64-rpm.bin## cat /etc/ profile.d/java.sh export java_home=/usr/java/latestexport path=/usr/java/latest/bin/: $PATH # # #  source /etc/profile.d/java.sh  Step Two: Install tomcat###  get tomcat software  apache-tomcat-7.0.55.tar.gz  ###  Installation Steps tar xf ache-tomcat-7.0.55.tar.gz -c /usr/localln -sv  Apache-tomcat-7.0.55 tomcat### cat /etc/profile.d/tomcat.sh export catalina_home=/usr /local/tomcatexport path= $CATALINA _home/bin: $PATH # # #第三步: Add a test site, the number of directories in the site is as follows: ### tree /usr/local/ tomcat/webapps/test//usr/local/tomcat/webapps/test/|-- web-inf|   |-- classes|     '-- lib '--&NBSP;INDEX.JSP3&NBSP;DIRECTORIES,&NBSp;1 file# 3 directories web-inf classes lib 1 files  #  file contents:172.16.10.122<%@  Page language= "Java"  %>Examples of results:

650) this.width=650; "title=" 2 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "2" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410275oRbq.png" width= "815" height= "101"/>

650) this.width=650; "title=" 3 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "3" src= "Http://img1.51cto.com/attachment/201410/27/8886857_14144102849SUP.png" width= "819" height= "322"/>

650) this.width=650; "title=" 4 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "4" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410286P1Sn.png" width= "819" height= "193"/>

After the above test succeeds, configure the following content. 2. Configure the front-end scheduler
Operating system: CentOS 6,5 target host: 172.16.10.1 The first step: Install ngnix### can be installed using the source code, you can also use the RPM installation (need to configure a good Epel source) here to use the RPM method of installation. Yum Install ngnix-y Second step: Configure the Ngnix front-End Scheduler # # # CAT/ETC/NGINX/CONF.D/DEFAULT.CONF The main configuration is as follows: Upstream www.tomcat.org {ip_hash; Server 172.16.10.122:8080;server 172.16.10.133:8080;}    server {Listen 80;    server_name www.tomcat.org;        Location/{Proxy_pass http://www.tomcat.org;    Index index.jsp index.html index.htm; }}
Results test:

650) this.width=650; "title=" 5 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "5" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410286SdRb.png" width= "723" height= "179"/>

This is achieved through the Ip_hash scheduling method. Consider the following question: Does this also reduce the pressure on the back-end tomcat??

WORKAROUND: Save the session information on each tomcat node and make it into a Tomcat cluster (session cluster).

Ii. using the Tomcat cluster to keep the session

The implementation schema is the same.

1. Configuring the Tomcat cluster
###  in  /usr/local/tomcat/conf/server.xml  <Host> </Host> Internal add: Target host: 172.16.10.122 172.16.10.133<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.group.GroupChannel" >    < Membership classname= "Org.apache.catalina.tribes.membership.McastService"                  address= "228.10.10.14"                  port= "45564"                  frequency= "500"                 droptime= "3000 "/>    <receiver classname=" Org.apache.catalina.tribes.transport.nio.NioReceiver "               address= "172.16.10.122/172.16.10.133"                   #  Note This option is auto by default and needs to be configured here. Each node is configured with its own IP address,                #   means to receive the address of the session heartbeat message                port= "4000"               autobind= "100"               selectortimeout= "              maxthreads=" "6"/>     <sender classname= "Org.apache.catalina.tribes.transport.ReplicationTransmitter ">      <transport classname=" Org.apache.catalina.tribes.transport.nio.PooledParallelSender "/>    </sender>     <interceptor classname= " Org.apache.catalina.tribes.group.interceptors.TcpFailureDetector "/>    <interceptor  classname= "Org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>   </channel>  <valve classname= "Org.apache.catalina.ha.tcp.ReplicationValve"           filter= ""/>  <valve classname= " Org.apache.catalina.ha.session.JvmRouteBinderValve "/>  <deployer classname=" Org.apache.catalina.ha.deploY.farmwardeployer "            tempdir="/tmp/ war-temp/"            deploydir="/tmp/war-deploy/"             watchdir= "/tmp/war-listen/"              watchenabled= "false"/>  < Clusterlistener classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>   <clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/></ cluster>###  Add <distributable\>;  can also add a target host globally in the corresponding application's Web. XML:172.16.10.122  172.16.10.133cp /usr/local/tomcat/webapps/test/web-inf/web.xml /usr/local/tomcat/webapps/test/ web-inf/#  edit  /usr/local/tomcat/webapps/test/WEB-INF/web.xml  Add in  <web-app> : <distributable/>;
2. Configure the front-end scheduler

This is mainly the use of polling scheduling, not using Ip_hash scheduling. Simply remove the Ip_hash from the configuration file.

Examples of results:

After the configuration is complete, restart Tomcat.

650) this.width=650; "title=" 8 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "8" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410296iUjR.png" width= "813" height= "242"/>

From the results can be known, the implementation of the session is maintained.

650) this.width=650; "title=" 6 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "6" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410297OHxX.png" width= "681" height= "184"/>

650) this.width=650; "title=" 7 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "7" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410298uYhU.png" width= "688" height= "/>"

Think about the following questions? The session in this way holds a session message through each node, but the seesion information can be quite large when the cluster nodes are many. At this point, the session replication between the nodes becomes the bottleneck that affects the performance of the system.

WORKAROUND: Use the session server to uniformly save the seession information to the cache server.

Second, use session server (memcached) to maintain session

The test topology diagram is as follows:

650) this.width=650; "title=" 9 "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "9" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410300W6zG.png" width= "875" height= "331"/>

Tomcat implements this functionality with the help of MSM (Memcached-session-management) modules. Need to download jar packages, javolution, MEMCACHED-SESSION-MANAGER-TC7, spymemcached, Memcached-session-manager, Msm-javolution-serializer. These jar packages are copied to the/usr/local/tomcat/lib/directory to support MSM functionality. These packages can be found in Google code.

When downloading, be aware that you want to correspond to the version of Tomcat.

1, Configuration memcached
Operating system: CentOS 6.5 target host: 172.16.10.1 172.16.10.9yum install Memcached-yservice memcached start
2. Configuring Tomcat
Target host:172.16.10.122 172.16.10.133##  on the basis of test one, continue to configure ###  in  /usr/local/tomcat/conf/server.xml   <Host> </Host> Internal Add the following: <context path= "/test"  docbase= "/usr/local/tomcat/ webapps/test/" reloadable=" true ">         <manager  classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"                    memcachednodes= "n1:172.16.10.9:11211, n2:172.16.10.1:11211 "                   failovernodes= "N1"                    requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "          transcoderfactoryclass=" De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactorY "         />         </Context>
3, configure the Ngnix front-end scheduling method, the same as Test two.

Examples of results:

650) this.width=650; "title=" style= "border-right-width:0px;border-bottom-width:0px;border-top-width:0px;" Border= "0" alt= "ten" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410302a6fh.png" width= "632" height = "188"/>

650) this.width=650; "title=" style= "border-right-width:0px;border-bottom-width:0px;border-top-width:0px;" Border= "0" alt= "one" src= "Http://img1.51cto.com/attachment/201410/27/8886857_1414410304d8Yr.png" width= "633" height = "166"/>

Additional notes:

The Apache and Haproxy reverse proxies can also be used for front-end schedulers.

Iv. instances of Tomcat

Tomcat + memcacahed + nginx + MySQL + NFS implementation JSP format forum. The test topology diagram is as follows:

650) this.width=650; "title=" style= "border-right-width:0px;border-bottom-width:0px;border-top-width:0px;" Border= "0" alt= "src=" http://img1.51cto.com/attachment/201410/27/8886857_1414410308SWRF.png "width=" 772 "height = "511"/>

1. Configure Data node 172.16.10.1
# # # # Configure NFS Server mkdir/tomcatdata# edit/etc/exports, add:/tomcatdata 172.16.0.0/16 (rw) service NFS start### configuration Mysqlyum Install Mysql-server-y servive mysqld startmysql> CREATE DATABASE jcenter1;mysql> GRANT all on jcenter1.* to [email Protec Ted] ' 172.16.%.% ' identified by ' Jcenter ';mysql> FLUSH privileges;
2. Configure Tomcat on the basis of test one:
Target host:  172.16.10.122 172.16.10.133 First step: Build site mkdir /tomcat/webapps/mount -t nfs  172.16.10.1:/tomcatdata /tomcat/webapps/#  Get JSP program  JavaCenter_Home_2.0_GBK.tar.bz2  Java Program Code Tar -xf javacenter_home_2.0_gbk.tar.bz2 -c /tomcat/webapps/cd /tomcat/webapps /ln -sv javacenter_home_2.0_gbk jct#  Configuration  javacenter:/tomcatdata/jct/config.properties#   mainly modifies the following:#  database server address (typically local localhost or 127.0.0.1) dbhost = 172.16.10.1#  database server port number (typically 3306) dbport = 3306#  database user name dbuser = jcenter#  database password dbpw = jcenter#  Database name Dbname = jcenter1 Step two: Configure TomcatExamples of results:

650) this.width=650; "title=" "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "src=" Http://img1.51cto.com/attachment/201410/27/8886857_1414410310Nyaf.png "width=" 826 "height = "501"/>

650) this.width=650; "title=" style= "border-right-width:0px;border-bottom-width:0px;border-top-width:0px;" Border= "0" alt= "src=" http://img1.51cto.com/attachment/201410/27/8886857_1414410318qsgK.png "width=" 828 "height = "507"/>

650) this.width=650; "title=" style= "border-right-width:0px;border-bottom-width:0px;border-top-width:0px;" Border= "0" alt= "src=" Http://img1.51cto.com/attachment/201410/27/8886857_1414410321by9I.png "width=" 827 "height = "372"/>

650) this.width=650; "title=" style= "border-right-width:0px;border-bottom-width:0px;border-top-width:0px;" Border= "0" alt= "src=" Http://img1.51cto.com/attachment/201410/27/8886857_1414410330zSMu.png "width=" 820 "height = "448"/>

At this point, the configuration is basically complete.

This article is from the "Upstream Cold" blog, please be sure to keep this source http://guoting.blog.51cto.com/8886857/1568583

Tomcat three ways to achieve session retention, using MSM to build JSP Web site

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.