The combination of the Tomcat chapter apache+tomcat+memcached to do session sharing

Source: Internet
Author: User
Tags session id

tomcat1:192.168.1.155

tomcat2:192.168.1.11

apache:192.168.1.155

Front-end agent Apache settings, refer to previous Tomcat articles (based on mod_proxy and MOD_JK modules)

Do not repeat here, directly paste the configuration file:

Cd/etc/httpd/conf.d

[email protected] conf.d]# cat mod_jk.conf

LoadModule Jk_module modules/mod_jk.so

Jkworkersfile/etc/httpd/conf.d/workers.properties

JklogfileLogs/mod_jk.log

JkloglevelDebug

Jkmount/* lbcluster

Jkmount /STATUS/STAT1

[email protected] conf.d]# cat workers.properties

Worker.list=lbcluster,stat1

worker.tomcat1.port=8009

worker.tomcat1.host=192.168.1.155

Worker.tomcat1.type=ajp13

Worker.tomcat1.lbfactor=1

worker.tomcat2.port=8009

worker.tomcat2.host=192.168.1.11

Worker.tomcat2.type=ajp13

Worker.tomcat2.lbfactor=1

Worker.lbcluster.balance_workers=tomcat1,tomcat2

Worker.lbcluster.type=lb

Worker.lbcluster.sticky_session=0

Worker.stat1.type = Status


For Tomcat Configure

Reference: Https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

Add Memcached-session-manager Jars to Tomcat

wget Http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.9.4/memcached-session-manager-1.9.4.jar

wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/1.9.4/ Memcached-session-manager-tc7-1.9.4.jar

wget Http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar

Download the three jar files needed for memcached

Copy the three jar files to the $catalina_home/lib/directory

If you just use Java to do the serializer only need these three packets OK

To modify the $catalina_home/conf/context.conf directory add:

<Context>

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"

Memcachednodes= "n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"

failovernodes= "N1"

Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "

/>

</Context>

See TOMCAT1 configuration as follows: Context.xml

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/84/7F/wKioL1eSHQjSHbiqAAAcnNEB4tE403.jpg "title=" 1.jpg " alt= "Wkiol1eshqjshbiqaaacnneb4te403.jpg"/>

This is the tomcat1:192.168.1.155 configuration.

See TOMCAT2 configuration as follows: Context.xml

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/84/80/wKioL1eSI-HAf-sUAAAfevaI5FM486.jpg "title=" 7.jpg " alt= "Wkiol1esi-haf-suaaafevai5fm486.jpg"/>650) this.width=650; src= http://s5.51cto.com/wyfs02/M00/84/80/ Wkiol1esi_lsyny7aaafevai5fm744.jpg "title=" 7.jpg "alt=" Wkiol1esi_lsyny7aaafevai5fm744.jpg "/>


The main configuration file for Tomcat Server.xml just one thing to note:

tomcat1:192.168.1.155

<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT1" >:jvmroute not less

tomcat2:192.168.1.11

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


Next install memcached on line, here with RPM package installation, convenient and quick

Yum-y Install memcached


Then start the Tomcat and memcached services on each node separately.

For 192.168.1.155-TOMCAT1

/usr/local/tomcat7/bin/catalina.sh start

Service memcached Start


For 192.168.1.11-TOMCAT1

/usr/local/tomcat7/bin/catalina.sh start

Service memcached Start


Check to see if the service port is up, and the browser first observes

tomcat1:192.168.1.155:8080

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/84/80/wKioL1eSH8WiuSBcAABWELPl9wc552.jpg "title=" 3.jpg " alt= "Wkiol1esh8wiusbcaabwelpl9wc552.jpg"/>

tomcat2:192.168.1.11:8080

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/84/80/wKiom1eSH_CwkM0yAABkOv8PfQQ319.jpg "title=" 4.jpg " alt= "Wkiom1esh_cwkm0yaabkov8pfqq319.jpg"/>

Can obviously detect the Tomcat service without problems, then perform session sharing test

Access Browser http://192.168.1.155:80

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/84/80/wKioL1eSIEGilPYDAABU0utgqQ8806.jpg "title=" 5.jpg " alt= "Wkiol1esiegilpydaabu0utgqq8806.jpg"/>

and then refresh

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/84/80/wKiom1eSIGbRBqxiAABZJgX6cEw943.jpg "title=" 6.jpg " alt= "Wkiom1esigbrbqxiaabzjgx6cew943.jpg"/>

It is apparent from the above that the session ID has not changed, and the Tomcat number has changed, and the session sharing is now complete based on the Tomcat cluster.

Two Tomcat index.jsp program code included

<%@ page language= "java"%>

<body>

<table align= "centre" border= "1" >

<tr>

<td>session id</td>

<% Session.setattribute ("abc", "abc"); %>

<td><%= Session.getid ()%></td>

</tr>

<tr>

<td>created on</td>

<td><%= session.getcreationtime ()%></td>

</tr>

</table>

</body>

################### #tomcat2 ###############

<%@ page language= "java"%>

<body>

<table align= "centre" border= "1" >

<tr>

<td>session id</td>

<% Session.setattribute ("abc", "abc"); %>

<td><%= Session.getid ()%></td>

</tr>

<tr>

<td>created on</td>

<td><%= session.getcreationtime ()%></td>

</tr>

</table>

</body>

#############################################

According to a lot of users have selected other serializers, here to indicate a little

If you customize the serializer you have to add your own custom package

Other Serial number device:

Javolution-serializer:msm-javolution-serializer, javolution-5.4.3.1

Xstream-serializer:msm-xstream-serializer, XStream, Xmlpull, xpp3_min

Flexjson-serializer:msm-flexjson-serializer, Flexjson


For example, if you customize the Javolution-serializer this serializer, it relies on some of the following packages

Javolution-serializer:msm-javolution-serializer, javolution-5.4.3.1 (behind the dependent jar package)

Then add the following line to the context.xml in the next few pieces of code

Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory"

So the configuration file context.xml as follows:

<Context>

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"

Memcachednodes= "n1:192.168.1.155:11211,n2:192.168.1.11:11211"

failovernodes= "N1"

Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "

Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory"

/>

</Context>

Then reboot tomcat, if you're adding another serializer just need to change the serial number in the last line you just added.

Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory" To change the javolution serializer in your code to a different serializer name.



The combination of the Tomcat chapter apache+tomcat+memcached to do 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.