Tomcat Load balancer (MOD_JK for Apache)

Source: Internet
Author: User

Tomcat Load balancer (MOD_JK for Apache):
1, based on the Apache MOD_JK to achieve:
Apache
/    \
Tom1 Tom2
After completing the installation of two tomcat1,tomcat2, it is hoped that the load balance can be achieved, and using MOD_JK can realize the fault recovery function of the node, that is, a node problem, MOD_JK will not participate in the load,

After a node failure is restored, the MOD_JK will let it participate in the load, and if this node fails, it is transparent to the user.
Implementation process:
A, the user access to the data after Apache, if it is static content, Apache immediately respond;
B. If Apache discovers that the user is requesting dynamic content such as JSP, it will throw the request to one of the tomcat to handle it.
C, each time the user repeated access, are assigned to the same machine (can do session sticky or session replication)
This article environment on the basis of the new addition of a machine (do load balancer), in fact, responsible for load balancing is mainly done by a configuration file! That's workers.propertise.

Define each tomcat's host by defining the value of the Worker.list,port,host,type,lbfactor, and by Lbfactor to differentiate the weights of each machine, the larger the weight value, the more the number of user requests will be obtained. So, just focus on the content of the file.
Implement:
A, installation Aapche
# TAR-XZF Httpd-2.2.22.tar.gz
# chown Root.root-r httpd-2.2.22
# CD httpd-2.2.22
#./configure--prefix=/usr/local/apache
# Make && make install
b, compile the MOD_JK module
# TAR-XZF Tomcat-connectors-1.2.32-src.tar.gz
# CD TOMCAT-CONNECTORS-1.2.32-SRC
# CD native/
#./configure--with-apxs=/usr/local/apache/bin/apxs
# Make && make install
# ll/usr/local/apache/modules/mod_jk.so
-rwxr-xr-x 1 root root 1076923 Feb 14:17/usr/local/apache/modules/mod_jk.so
C, Load MOD_JK
# vim/usr/local/apache/conf/httpd.conf
#增加的内容
LoadModule Jk_module modules/mod_jk.so
Jkworkersfile/usr/local/apache/conf/workers.properties
Jkmountfile/usr/local/apache/conf/uriworkermap.properties
Jklogfile/usr/local/apache/logs/mod_jk.log
Jkloglevel Info
Jklogstampformat "[%a%b%d%h:%m:%s%Y]"
#告诉apache, how to deal with the relevant class-shaped files (these lines can not be, as long as the urlworkermap.properties inside a definition is also possible!) :)
Jkmount/* Controller
Jkmount/*.jsp Controller
Jkmount/*.action Controller
# cat/usr/local/apache/conf/workers.properties
# Cat Workers.properties
Worker.list=controller
#========tomcat1========
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.10.56
Worker.tomcat1.type=ajp13
Worker.tomcat1.lbfactor = 1
worker.tomcat12.connection_pool_timeout=750
Worker.tomcat12.socket_keepalive=0
worker.tomcat12.socket_timeout=3000
worker.tomcat12.connect_timeout=1000
worker.tomcat12.reply_timeout=3300
#========tomcat2========
worker.tomcat2.port=8009
worker.tomcat2.host=192.168.10.57
Worker.tomcat2.type=ajp13
Worker.tomcat2.lbfactor = 1
worker.tomcat12.connection_pool_timeout=750
Worker.tomcat12.socket_keepalive=0
worker.tomcat12.socket_timeout=3000
worker.tomcat12.connect_timeout=1000
worker.tomcat12.reply_timeout=3300
#=======balance controller====
Worker.controller.type=lb
Worker.retries=3
Worker.controller.balance_workers=tomcat1,tomcat2
Worker.controller.sticky_session=1
Related parameter Description:
worker.tomcat12.socket_keepalive=0# This property tells the operating system to send keep_alive information in an inactive connection (the sending interval depends on the operating system's settings, typically 120 seconds), This prevents the firewall from cutting off the inactive network connection.
worker.tomcat12.socket_timeout=300000# This property describes how long the connection lasts for an inactive state, and Web server will actively sever it.
worker.tomcat12.connect_timeout=10000# This property describes the time, in MS, that Web server waits for a pong response.
worker.tomcat12.reply_timeout=330000# This property tells Web server to wait a while before receiving a remote tomcat that is dead and switching to another tomcat response in the cluster in real time.
worker.tomcat12.reply_timeout=0# This property describes how Web server recovers after a tomcat failure is detected (default is 0, all recovery)
Worker.loadbalancer.sticky_session=0# describes whether to route requests for session IDs back to the same tomcat worker. (default is 0,session not copied)
# Cat Uriworkermap.properties
/*=controller
!/*.jpg=controller
!/*.gif=controller
!/*.png=controller
!/*.bmp=controller
!/*.html=controller
!/*.htm=controller
!/*.swf=controller
!/*.css=controller
!/*.js=controller
Once you've done all of this, you'll find that both Tomcat is actually loaded, but a new problem arises and the session issues. The first commit and the second commit are not on the same server, which can basically be considered data loss.
If just browsing, then the problem may not be obvious, of course, if you log in, then the system will prompt you to log in. So, there is a copy of Tomcat's session!
There are two common approaches to session replication for Tomcat: Using Tomcat's own simple clustering feature to help with replication sessions.

Another is to use caching programs such as memcache to cache sessions.  

Prerequisites:
1, memcached normal work.
2, Tomcat normal work.

1. Installation method and installation package
Javolution-5.4.3.1.jar
Memcached-2.4.2.jar
Memcached-session-manager-1.3.0.jar
Msm-javolution-serializer-cglib-1.3.0.jar
Msm-javolution-serializer-jodatime-1.3.0.jar

Installation method: Put these bags into the/usr/local/tomcat6.0/lib.

2. Edit the Tomcat configuration file. < This is important because most of the articles on the web are about modifying the server.xml configuration; I tried but eventually found that the session information was not stored in the memcache,

I realized that this could be a problem with the configuration. Currently found editing context.xml this file, it is possible to immediately throw the session into the memcache. >
# Vim/usr/local/tomcat6.0/conf/context.xml
<Context>
<!--Add the following in this field, enter your memcached server IP and port number on the node--
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:10.254.41.197:11211"
Requesturiignorepattern= ". *\. (PNG|GIF|JPG|CSS|JS) $ "
Sessionbackupasync= "false"
sessionbackuptimeout= "100"
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
Copycollectionsforserialization= "false"/>
</Context>

3, how to test?
My current test method is to use Apache's MOD_JK for load balancing (written in the previous article), and to enable session replication between Tomcat.
The result is that the Seesion ID transforms the node each time the user commits, but the ID does not change.
For example:
ID CFC1ACAC6B0B8C27BEA76919F3A99BF8-N1.TOMCAT2
The next commit becomes: The < node is switched, but the ID is not changed >
ID CFC1ACAC6B0B8C27BEA76919F3A99BF8-N1.TOMCAT1

Combined with the status of Memcache: < I did not successfully drop the session to memcached here, Cmd_set is 0 >
# perl memcached-tool.pl 10.254.41.197:11211 Stats
Cmd_get 27
Cmd_set 15
Get_hits 12
# perl memcached-tool.pl 10.254.41.197:11211 Stats
Cmd_get 31
Cmd_set 17
Get_hits 14

I want to pass the above judgment, the configuration is successful!

Tomcat Load balancer (MOD_JK for Apache)

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.