Use memcached as a cache server to do session retention for back-end tomcat servers and load balance with HTTPD's JK module modules

Source: Internet
Author: User
Tags object serialization

The front end uses httpd as the reverse load balancer to the back-end Tomcat host: Tomcat can use memcached as the session server to save the session:


Experimental environment: Physical machine Win7, virtual machine centos7

node1:172.18.11.111 Tomcata

node2:172.18.11.112 TOMCATB

NODE3:172.18.11.113 Scheduler httpd

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/B3/wKioL1devu3y5eTWAAB0iXhw4DI721.png "title=" 2.png " Width= "702" height= "459" border= "0" hspace= "0" vspace= "0" style= "WIDTH:702PX;HEIGHT:459PX;" alt= " Wkiol1devu3y5etwaab0ixhw4di721.png "/>


Installing Tomcat

Install Tomcat in Node1 and Node2, respectively:

]# yum-y Install JAVA-1.7.0-OPENJDK java-1.7.0-openjdk-devel

]# vim/etc/profile.d/java.sh

Export JAVA_HOME=/USR

]# . /etc/profile.d/java.sh

]# yum-y Install tomcat tomcat-lib Tomcat-webapps Tomcat-admin-webapps

]# cd/var/lib/tomcat/webapps/

]# mkdir TestApp

]# CD testapp/

]# mkdir classes Lib Web-inf meta-inf

]# Vim index.jsp

<%@ page language= "java"%>

<%@ page import= "java.util.*"%>

<title>jsp Test Page </title>

<body>

<% out.println ("Hello World,tomcat 1,172.18.11.111");

%>

</body>


]# Scp-r/var/lib/tomcat/webapps/testapp/node2:/var/lib/tomcat/webapps/


To modify the configuration on Node2:

]# vim/var/lib/tomcat/webapps/testapp/index.jsp

Modify only one: <% out.println ("Hello World,tomcat 2,172.18.11.112");


In Node3:

Compile and install the JK module, this module is not httpd the default module needs to be compiled to use;

Install the development environment:

]# Yum Groupinstall "Development Tools" "Server Platform Development"


Need to install httpd own development package in order to compile and install third-party modules;

]# yum-y Install Httpd-devel


]# which APXS

/usr/bin/apxs

This path is used at compile time;


Download the JK module and unzip it:

]# Tar XF tomcat-connectors-1.2.40-src.tar.gz

]# CD tomcat-connectors-1.2.40-src/native/

]#./configure--with-apxs=/usr/bin/apxs

]# make

]# make Install

Display part of the content:

chmod 755/usr/lib64/httpd/modules/mod_jk.so

Indicates the completion of the compilation;


]# vim/etc/httpd/conf.d/mod_jk.conf

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/B3/wKioL1dev32DYc_WAAAn_gLYPas471.png "title=" 3.png " alt= "Wkiol1dev32dyc_waaan_glypas471.png"/>

]# vim/etc/httpd/conf.d/workers.properties

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/B5/wKiom1devmOBQ2LVAABZd1YYops654.png "title=" 4.png " alt= "Wkiom1devmobq2lvaabzd1yyops654.png"/>


]# httpd-t

Display: Syntax OK

]# Systemctl Reload Httpd.service

Browser input: http://172.18.11.113/test/

Refresh to achieve load balance effect, load balance effect, Node1 and node2 ratio is 1:2;

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/B5/wKiom1dev8WiDFn3AAAuUThKc48655.png "title=" 20.png "alt=" Wkiom1dev8widfn3aaauuthkc48655.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/B5/wKiom1devzPz6sPZAAAxS6LMkNg999.png "style=" float: none; "title=" 21.png "alt=" Wkiom1devzpz6spzaaaxs6lmkng999.png "/>



If you want to implement session binding, simply add it in the/etc/httpd/conf.d/workers.properties configuration file:

Worker.tcsrvs.sticky_session=1

However, you will also need to add a ' jvmroute= ' directive to identify the Tomcat server ID number on Node1 and Node2, in the engine configuration section of the Tomcat configuration file/etc/tomcat/server.xml;


On the Node1:

]# Vim/etc/tomcat/server.xml

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

]# systemctl Restart Tomcat.service

On the Node2:

]# Vim/etc/tomcat/server.xml

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

]# systemctl Restart Tomcat.service


Browser input: http://192.168.255.3/test/

Implements session binding.


Only the JK module using HTTPD and the demo of Session binding are implemented. However, session-based sticky binding sessions are not actually used because this method can compromise the effect of load balancing, and once a server is dropped, sessions on that server will be lost.


In practice, the session server can be used to save the session for Tomcat, while using memcached as the session server. The configuration is as follows:


Based on the above configuration load balancing, the session server for Tomcat is built on memcached:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/B3/wKioL1dew2Syb7B1AAC0TZiTcRY810.png "title=" 1.png " Width= "742" height= "485" border= "0" hspace= "0" vspace= "0" style= "width:742px;height:485px;" alt= " Wkiol1dew2syb7b1aac0tzitcry810.png "/>

With a session server, Tomcat support is required, and Tomcat does not support it and requires third-party programs;

A session is an object when it is saved in memory, Tomcat is object-oriented, so everything inside is an object, objects have a specific structure, and the network transport is to serialize the data before it can be transferred; The session server can only store serialized data, and the default object is not serialized;

The serialization tool is required to convert the session object to a sequential format before it can be stored on the session server (memcached), and the Tomcat session is saved in memcached, memcached as a session server;


While different session servers store data in the same way, different serialization tools may be different when serializing data, so there are several object serialization tools, such as Memcached-session-manager, which are closely related to the Tomcat version, Download the corresponding version of Memcached-session-manager according to the Tomcat version you are using.


Download the Memcached-session-manager required to the relevant class library:

Javolution-5.5.1.jar

Memcached-session-manager-1.8.2.jar

Memcached-session-manager-tc7-1.8.2.jar

Msm-javolution-serializer-1.8.2.jar

Spymemcached-2.10.2.jar


Copy these class libraries to the class library directory at the time of the Tomcat installation:

]# CP */usr/share/tomcat/lib/


Copy one to Node2:

]# scp-r msm/* node2:/usr/share/tomcat/lib/

Stop Node1 and Node2 Tomcat.

]# Systemctl Stop Tomcat.service


While experimenting, node1 and Node2 are two Tomcat hosts and two memcached session servers:


Install memcached:

In Node1 and Node2:

]# yum-y Install memcached

]# systemctl Start Memcached.service

]# SS-TNL ensure 11211 port monitoring;


With the memcached session server, a new session manager is introduced for Tomcat;

This session manager can be placed in the engine, Context, host can be, according to which level you want to take effect in which configuration section;


In Node1:

]# Vim/etc/tomcat/server.xml

...

<context path= "/test" docbase= "TestApp" >

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

Memcachednodes= "n1:172.18.11.111:11211,n2:172.18.11.112:11211"

failovernodes= "N1"

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

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

/>

</Context>

...


Copy the configuration file to Node2:

]# Scp/etc/tomcat/server.xml node2:/etc/tomcat/


In Node2:

]# Vim/etc/tomcat/server.xml

Change only:

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


Start Tomcat in Node1 and Node2, respectively:

]# Systemctl Start Tomcat


Browser input: http://172.18.11.113/test/test.jsp

After the refresh, dispatch to the back end different host response, but the session has not changed;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/B3/wKioL1dexleQeKx1AABdmN20fRU929.png "style=" float: none; "title=" 28.png "alt=" Wkiol1dexleqekx1aabdmn20fru929.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/B5/wKiom1dexUfRumOqAABXPnd9BuY628.png "style=" float: none; "title=" 29.png "alt=" Wkiom1dexufrumoqaabxpnd9buy628.png "/>


To manually drop the N2 memcached session server:

Test another session server to still work:

Note: Because of the information of the backend server in this session information, the session will change when the session server is switched; this may be because of a problem with the JSP program, if the information of the backend host is not added to the session information, the session information should not change when dispatching to different host responses.


Use memcached as a cache server to do session retention for back-end tomcat servers and load balance with HTTPD's JK module modules

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.