HTTPD load balancer for high performance Web services Tomcat implementation session sticky and session Cluster

Source: Internet
Author: User

HTTPD load Balancing Tomcat implements the session sticky and session cluster architecture as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/88/6C/wKioL1f3ajTxPkM9AADT2sKTejY987.png "title=" Session frame composition. png "alt=" Wkiol1f3ajtxpkm9aadt2sktejy987.png "/>

The implementation process is as follows:

Configuring the Tomcat Service (TOMCAT1\TOMCAT2)

(1) Installing the JDK

# rpm -ivh jdk-7u9-linux-x64.rpm --files generated after installation of JDK #  cd /usr/java/ ; lltotal 4lrwxrwxrwx  1 root root    16 sep 27 09:09 default -> /usr/java/latestdrwxr-xr-x 10 root  root 4096 sep 27 09:09 jdk1.7.0_09lrwxrwxrwx  1 root root    21 sep 27 09:09 latest -> /usr/java/jdk1.7.0_09--Configuring the JDK environment variable #  vim /etc/profile.d/java.shexport java_home=/usr/java/jdk1.7.0_09export path= $PATH: $JAVA _ home/bin--perform the # . /etc/profile.d/java.sh--test to make the environment variable effective:# java -versionjava version  "1.7.0_45"         --JDK version openjdk runtime environment  ( RHEL-2.4.3.3.EL6-X86_64 U45-B15) openjdk 64-bit server vm  (build 24.45-b08,  Mixed mode) 

(2) Install Tomcat

--Unzip the tomcat bag to/usr/local # tar xf apache-tomcat-7.0.72.tar.gz -c /usr/local/# cd  /usr/local/# ln -sv apache-tomcat-7.0.72 tomcat--Configuring the Tomcat environment variable # vim /etc/ Profile.d/tomcat.shexport catalina_home=/usr/local/tomcatexport path= $PATH: $CATALINA _home/bin#  The.  /etc/profile.d/tomcat.sh--test is as follows:# catalina.sh versionusing catalina_base:    /usr/local/tomcatusing catalina_home:   /usr/local/tomcatusing catalina_tmpdir:  /usr/local/tomcat/tempusing jre_home:        /usr/java/ Jdk1.7.0_09using classpath:       /usr/local/tomcat/bin/bootstrap.jar :/usr/local/tomcat/bin/tomcat-juli.jarserver version: apache tomcat/7.0.67server built:    Dec 7 2015 13:07:11 UTCServer number:  7.0.72.0OS  name:         linuxos version:     2.6.32-431.el6.x86_64architecture:    amd64JVM Version:    1.7.0_09-b05JVM Vendor:      oracle corporation--provides the SRV startup script for Tomcat # vim /etc/init.d/tomcat#!/bin/sh# tomcat  init script for Linux.# # chkconfig: 2345 96 14#  Description: the apache tomcat servlet/jsp container.# java_opts= '-Xms64m - Xmx128m ' Java_home=/usr/java/latestcatalina_home=/usr/local/tomcatexport java_home catalina_homecase  $1 instart)    $CATALINA _home/bin/catalina.sh start;; Stop)    $CATALINA _home/bin/catalina.sh stop;; Restart)    $CATALINA _home/bin/catalina.sh stop  sleep 2   $CATALINA _home /bin/catalina.sh start;; *)   echo  "usage:  ' basename $0 '  {start|stop|restArt} "  exit 1  ;; esac# chmod +x /etc/init.d/tomcat# chkconfig --add tomcat# service  tomcat start--Check the TOMACAT default listener port as shown below:# ss -tunlp | grep javatcp     LISTEN     0      100                    :::8080                  :::*        users: (("Java", 19525,42) tcp    listen      0      1       ::ffff:127.0.0.1:8005                  :::*       users: (("Java", 19525,47) TCP    LISTEN &NBsp;   0      100                    :::8009                  :::*        Users: (("Java", 19525,43))

(3) Configure the virtual host as shown below (two tomcat to configure):

# vim/usr/local/tomcat/conf/server.xml #设置默认虚拟主机为www. samlee.com #第二台tomcatserver的jvmRoute设置为TomcatB <engine Name = "Catalina" defaulthost= "www.samlee.com" jvmroute= "Tomcata" > #添加虚拟主机: 

(4) Create a Web page file (two tomcat to configure):

Tomcata:# vim /web/webapps/index.jsp<%@ page language= "Java"  %>

Restart the Tomcat service deployment:

# Service Tomcat stop# service Tomcat start

The access tests are as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/88/70/wKiom1f3cOzTF69NAAC-rAEnpUg512.png "style=" float: none; "title=" Tomsamlee1.png "alt=" Wkiom1f3coztf69naac-raenpug512.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/88/6C/wKioL1f3cO_jPD00AADBjw-xilA024.png "style=" float: none; "title=" Tomsamlee2.png "alt=" Wkiol1f3co_jpd00aadbjw-xila024.png "/>


Configure HTTPD to implement load balancing reverse proxy tomcat

The configuration file looks like this:

# vim/etc/httpd/conf.d/proxy.conf<proxy balancer://lbcluster> Balancermember ajp://192.168.56.11:8009 Loadfactor=1 route=tomcata balancermember ajp://192.168.56.12:8009 loadfactor=1 route=TomcatB</Proxy><  VirtualHost *:80> ServerName www.samlee.com Proxyvia on proxyrequests off proxypassreverse/balancer://lbcluster/ Proxypass/balancer://lbcluster/</virtualhost>

Restart the HTTPD service access test as follows:

# Service httpd Restart

The test is as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/88/74/wKiom1f4abmQh3DxAAE-RqgzfK8266.gif "title=" Tomcathttp1.gif "alt=" Wkiom1f4abmqh3dxaae-rqgzfk8266.gif "/>

Above we can see that the reverse proxy and load balancer are already in effect.


configuring HTTPD to implement session sticky sessions binding

If Tomcat is running a dynamic site, then the above byrequest load balancing scheduling algorithm has a lot of problems, may just login to the site and then refresh back to no login status, so we will implement session sticky, in fact session Sticky is our previous LVS of the SH algorithm, and nginx Ip_hash implementation of the effect

The configuration file looks like this:

# Vim/etc/httpd/conf.d/proxy.confheader Add Set-cookie "routeid=.%{balancer_worker_route}e; path=/"Env=balancer_route_changed<proxy balancer://lbcluster> balancermember ajp://192.168.56.11:8009 Loadfactor=1 route=tomcata balancermember ajp://192.168.56.12:8009 loadfactor=1 Route=TomcatB ProxySet stickysession=  Routeid</proxy><virtualhost *:80> ServerName www.samlee.com Proxyvia on proxyrequests off Proxypassreverse /balancer://lbcluster/proxypass/balancer://lbcluster/</virtualhost>

Restart the HTTPD service access test as follows:

# Service httpd Restart

The test is as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/88/71/wKioL1f4dBihp0dFAADDS9gYluY866.gif "title=" Tomcathttp2.gif "alt=" Wkiol1f4dbihp0dfaadds9gyluy866.gif "/>

As shown, the display has successfully bound the session sessions to the Tomcata host.


Configuring the Tomcat implementation session cluster sessions information sharing replication:

* Using session sticky will have a lot of problems, if a host is down, then the user's session is lost, so we can also save user session through session cluster.

* * What is session Cluster?? Under normal circumstances, the session information is stored in the user's access to the server, the server down, the user's session is lost, but we can use the session cluster way to achieve the user's session information is stored on the backend all the servers, so that, The session is not lost regardless of which server the user is accessing.


Four common session Manager

**standardmanager**persistentmanager: Session information can be saved in persistent storage **deltamanager: Sharing session information to other nodes via multicast * * Backupmanager: Sharing session information to a specific node

Before you configure Tomcat, the session sticky of the httpd configuration is canceled:

To build the Deltamanager cluster steps:
1. Add the contents of the engine or host container in the server.xml of each node; Note Modify the multicast address address= "228.0.0.4" in the membership component, It is suggested that the address in receiver should be modified to deliver the heartbeat information.
2, in each node to use the multicast address to add multicast routing, format:
Route add-net $MCAST _address netmask 255.255.255.255 Dev eth0
3. Add <distributable\>; in the corresponding application's Web. xml

The edit configuration Tomcat configuration file is as follows (two Tomcat hosts to configure):

The following content is placed in the <Host />     <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.0.1.7"      &Nbsp;              port= "45564"                       frequency= "              "       droptime= "/>        <" Receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"                    address= "192.168.56.12"     --here to modify the native IP address                    port= "4000"                    autobind= "          "         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 " &Nbsp;           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&nbSp;classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/>    </cluster >

The Web. xml file in configuration Web-inf is as follows:

# cp/usr/local/tomcat/webapps/root/web-inf/web.xml/web/webapps/web-inf/# Vim/web/webapps/web-inf/web.xml #添加下面这一行 , in </web-app> <distributable/>--copy configuration file Web. xml to tomcata host # scp/web/webapps/web-inf/web.xml [email protected]:/web/webapps/web-inf/--Copy the configuration file Server.xml to Tomcata host and modify the IP address and the virtual host jvmroute# as required scp/usr/local/tomcat/ conf/server.xml [Email protected]:/usr/local/tomcat/conf/

Add multicast routing addresses for each Tomcat node (two Tomcat hosts to configure):

Route add-net 228.0.1.7 netmask 255.255.255.255 Dev eth0

Restart the Tomcat service

# Service Tomcat stop# service Tomcat start

The access tests are as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/88/75/wKiom1f4gY7jT5pZAAEW7R4l5fo298.gif "title=" Tomcathttp3.gif "alt=" Wkiom1f4gy7jt5pzaaew7r4l5fo298.gif "/>

We see that the session ID has not been accessed since then, proving that the session sharing has been configured successfully.

This article is from the "Opensamlee" blog, make sure to keep this source http://gzsamlee.blog.51cto.com/9976612/1860175

HTTPD load balancer for high performance Web services Tomcat implementation session sticky and session Cluster

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.