Apache implements Tomcat proxy, load balancing, and session binding based on three different scenarios

Source: Internet
Author: User
Tags session id tomcat server haproxy

Apache is based on MOD_PROXY_AJP, mod_proxy_http, mod_jk three kinds of schemes to implement agent, load balancing, session binding and Tomcat session cluster


1, Nginx, Haproxy, Apache (MOD_PROXY_AJP, Mod_proxy_http, MOD_JK) anti-user request to Tomcat;

2, Nginx, Haproxy, Apache (MOD_PROXY_AJP, mod_proxy_http, mod_jk) load user request to Tomcat servers; additional implementation session sticky;

3, Tomcat session cluster;

4, Tomcat session server (MSM);


Planning:

172.16.1.2 Apache anti-generational server

172.16.1.3 Tomcat A

172.16.1.10 Tomcat B


Description

Title requirements, you can choose to use Nginx, Haproxy, Apache three one to reverse the user request to Tomcat, because Apache and Tomcat are the Apache Software Foundation products, both have better compatibility, so choose to use Apache, And will be MOD_PROXY_AJP, Mod_proxy_http, mod_jk three ways to achieve the requirements of the topic respectively.


1. Programme I: Mod_proxy_http

Vim/etc/httpd/conf.d/lb_tomcat.conf


header add set-cookie  "routeid=.%{balancer_worker_route}e; path=/"  env=BALANCER_ROUTE _changed<proxy balancer://tcsrvs>  balancermember http://172.16.1.10:8080/testapp  route=tomcata loadfactor=1  balancermember http://172.16.1.3:8080/testapp route =tomcatb loadfactor=2        proxyset lbmethod=byrequests         ProxySet stickysession=ROUTEID</Proxy>< virtualhost *:80>        servername       lb.zrs.com        ProxyVia On         proxyrequests off        <proxy * >                require  all granted &nbSp;      </proxy>        proxypass  / balancer://tcsrvs/        <Location />                 require all  granted        </Location></VirtualHost>


Add Jvmroute to the engine in the Server.xml configuration file for two Tomcat, respectively


The first tomcat is configured as Tomcata:<engine name= "Catalina" defaulthost= "localhost" jvmroute= "Tomcata" > The second tomcat is configured as TOMCATB: <engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCATB" >


Provides a test page for TestApp of two Tomcat servers, respectively:

Tomcata:

# Mkdir-pv/usr/share/tomcat/webapps/testapp/{web-inf,classes,lib}

# vim/usr/share/tomcat/webapps/testapp/index.jsp

Add the following content:


<%@ page language= "Java"  %>


TOMCATB:

# Mkdir-pv/usr/share/tomcat/webapps/testapp/{web-inf,classes,lib}

# vim/usr/share/tomcat/webapps/testapp/index.jsp

Add the following content:


<%@ page language= "Java"  %>


Client testing, because did the session sticky, will always be dispatched to the back end of a server, such as mine has been tomcata

Turn off the Tomcata and you will be dispatched to the TOMCATB.


2. Programme II: MOD_PROXY_AJP

Using MOD_PROXY_AJP, only need to change the mod_proxy_http http to AJP, port 8080 to 8009, you need to note that the front end with a AJP, you can put the back end of the Tomcat 8080 port comment out, do not let it listen, This allows for higher security.


Vim/etc/httpd/conf.d/lb_tomcat.conf


header add set-cookie  "routeid=.%{balancer_worker_route}e; path=/"  env=BALANCER_ROUTE _changed<proxy balancer://tcsrvs>  balancermember ajp://172.16.1.10:8009/testapp  route=tomcata loadfactor=1  balancermember ajp://172.16.1.3:8009/testapp route= Tomcatb loadfactor=2        proxyset lbmethod=byrequests         ProxySet stickysession=ROUTEID</Proxy>< virtualhost *:80>        servername       lb.qhdlink.com        ProxyVia On         proxyrequests off        <proxy  *>                 require all granted        </proxy>         proxypass / balancer://tcsrvs/        <location / >                require  all granted        </Location></VirtualHost>

Close session sticky, client view

3. Programme three: MOD_JK


This scenario requires the MOD_JK module

Need to install this file tomcat-connectors-version.tar.gz


Configuration file:

Vim/etc/httpd/conf.d/mod_jk_lb.conf

LoadModule Jk_module Modules/mod_jk.sojkworkersfile/etc/httpd/conf.d/workers.propertiesjklogfile Logs/mod_ Jk.logjkloglevel debugjkmount/* Tcsrvsjkmount/jk-status Stat

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

Worker.list=tcsrvs,statworker.tomcata.host=172.16.1.10worker. Tomcata.port=8009worker. Tomcata.type=ajp13worker. Tomcata.lbfactor=1worker. Tomcatb.host=172.16.1.3worker. Tomcatb.port=8009worker. Tomcatb.type=ajp13worker. Tomcatb.lbfactor=2worker.tcsrvs.type=lbworker.tcsrvs.balance_workers=tomcata,tomcatbworker.tcsrvs.sticky_ Session=1worker. Stat.type=status


4.tomcat Session Cluster


Enable clustering on two Tomcat server.xml configurations

Add the following configuration and comment out port 8080

Note that the address= "auto" is changed to the IP address of the host in the following content


<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.100.0.4"

Port= "45564"

Frequency= "500"

Droptime= "/>"

<receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"

address= "Auto"

port= "4000"

autobind= "100"

selectortimeout= "5000"

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>



and add a context component to host

Unpackwars= "true" autodeploy= "true" >

<context path= "/test" docbase= "/myapps/webapps" reloadable= "true"/>


Create a Directory

[[email protected] tomcat]# Mkdir-pv/myapps/webapps/{classes,lib,web-inf}


Create a test page

Create ab two different pages like the one above

[Email protected] tomcat]# vim/myapps/webapps/index.jsp


Copy Web. XML to the specified directory

[email protected] tomcat]# CP web.xml/myapps/webapps/web-inf/


Edit Web-inf/web.xml

Add <distributable/> to any non-paragraph

[Email protected] tomcat]# Vim/myapps/webapps/web-inf/web.xml


Anti-generational server configuration

[Email protected] conf.d]# vim mod_jk_proxy.conf


LoadModule Jk_module Modules/mod_jk.sojkworkersfile/etc/httpd/conf.d/workers.propertiesjklogfile Logs/mod_ Jk.logjkloglevel debugjkmount/* Tcsrvsjkmount/jk-status Stat



[Email protected] conf.d]# vim workers.properties


Worker.list=tcsrvs,statworker.tomcata.host=172.16.1.10worker. Tomcata.port=8009worker. Tomcata.type=ajp13worker. Tomcata.lbfactor=1worker. Tomcatb.host=172.16.1.3worker. Tomcatb.port=8009worker. Tomcatb.type=ajp13worker. Tomcatb.lbfactor=2worker.tcsrvs.type=lbworker.tcsrvs.balance_workers=tomcata,tomcatbworker.tcsrvs.sticky_ Session=0worker. Stat.type=status

HTTPD Services and Tomcat services for three hosts respectively


Client Testing

You can see that the reverse server polls the back-end two tomcat servers in the order of ABB, but the ID is not changed to achieve session cluster.

5.tomcat Session Server (MSM)

Then the configuration of the environment, the principle of this scheme is to put all the sessions on the same back-end Tomcat server, when the back end of this tomcat server down, the entire building will not be able to provide services, so also to the Tomcat server to be highly available.


This build scenario requires memcached as a side-hanging cache server, so you need to download and start the memcached service in advance.


Stop two Tomcat services and restore the Server.xml configuration file

Comment out port 8080 and add jvmroute to the engine, as follows


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

Or

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


Adding configuration content to the host segment


<context path= "/test" docbase= "/myapps/webapps" reloadable= "true" >

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

Memcachednodes= "n1:172.16.1.10:11211,n2:172.16.1.3:11211"

failovernodes= "N1"

Requesturiignorepattern= ". *\. (ico|png|gif|jpg|css|js|html|htm) $ "

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

/>

</Context>


Importing class files

Memcached-session-manager Project Address, http://code.google.com/p/memcached-session-manager/


Download the following jar file to the Lib directory in the Tomcat installation directory of each tomcat node, where ${version} is replaced with the version number you need, tc${6,7,8} to be the same version number as the Tomcat version.

Memcached-session-manager-${version}.jar

Memcached-session-manager-tc${6,7,8}-${version}.jar

Spymemcached-${version}.jar

Msm-javolution-serializer-${version}.jar

Javolution-${version}.jar


Place all classes in the/usr/share/java/tomcat directory.


Start the Tomcat service to see that the reverse server polls the back-end two tomcat servers in AB order, but the session ID does not change.


Apache implements Tomcat proxy, load balancing, and session binding based on three different scenarios

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.