Combine Apache and Tomcat to achieve cluster and load balancing

Source: Internet
Author: User
Tags xmlns port number server port tomcat tomcat server websphere application server
In this paper, the basic reference from the easy implementation of APACHE,TOMCAT cluster and load balance, through the actual experience recorded, encountered some discrepancies, as well as individual places according to personal habits, so to a certain extent failed to maintain the integrity of the original, but also hope that the original Haihan.

Because there are more maps in the original, if you do not want to see the reader's own hand and straight to look at the configuration effect, you can view the original.

One: Software Environment
1. Apache:apache 2.0.55 (Download by http://httpd.apache.org/) (Click to download Apache 2.0.55)
2. Tomcat:tomcat 5.5.25 (Download by http://tomcat.apache.org/) (Click to download Tomcat 5.5.25 zip version)
3. MOD_JK: Find the Tomcat connectors link under the page http://tomcat.apache.org/Download header (click to download mod_jk-apache-2.0.55.so), it looks like a Unix /linux Dynamic Library, the actual should be a Win32 DLL dynamic library, presumably to maintain the consistency of different platform configuration, only use this extension.

Two: Load Balancing
Use Apache to shunt, the request according to weight and then load tomcat1,tomcat2 ... To deal with

1. Install Apache,tomcat
I've installed Apache in D:/apache group/apache2.
Extract two points tomcat, respectively, in D:/apache Group/tomcat5_1,d:/apache group/tomcat5_2
If a different version of Tomcat is clustered, the directory can be Tomcat4_3 (the third Tomcat Server version 4.x), Tomcat6_4 (the third Tomcat server in version 6.x), which is the custom of Unmi.

2. Modify Apache configuration file http.conf

In the Apache installation directory in the Conf directory to find http.conf, at the end of the file with the following sentence can be added

Include conf/mod_jk.conf

3. http.conf the same directory under the new mod_jk.conf file, the contents are as follows #加载mod_jk Module LoadModule jk_module modules/mod_jk-apache-2.0.55.so #指定 Workers.properties file path jkworkersfile conf/workers.properties #指定那些请求交给tomcat处理, "Controller" Jkmount/*.jsp Controller for the load allocation controller specified in Workers.propertise

If you also want to specify *.do also split a line
Jkmount/*.do Controller

If you want to triage all requests just write
Jkmount/controller

4. Create a new workers.properties file in the same directory as http.conf (you may want to remove the note with # Not at the beginning of the line) Worker.list = controller,tomcat1,tomcat2 #server list #====== ==tomcat1======== worker.tomcat1.port=8009 #ajp13 port number, server.xml configuration under Tomcat, default 8009 worker.tomcat1.host=localhost # Tomcat host address, if not native, please fill in IP address worker.tomcat1.type=ajp13 worker.tomcat1.lbfactor = 1 #server的加权比重, the higher the value, the more requests #======== tomcat2======== worker.tomcat2.port=8109 #ajp13 port number, server.xml configuration under Tomcat, default 8009 worker.tomcat2.host=localhost # Tomcat host address, if not native, please fill in IP address worker.tomcat2.type=ajp13 worker.tomcat2.lbfactor = 2 #server的加权比重, the higher the value, the more requests #======== Controller, load Balancing controller ======== worker.controller.type=lb WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2 # Specifies the Tomcat worker.controller.sticky_session=1

for the allocation request.

5. Modify Tomcat profile Server.xml
If you are a horizontal cluster, that is, the number of installed Tomcat,tomcat installed on different computers is one, you do not have to modify the Tomcat configuration file. I have two Tomcat installed on the same computer, is to achieve the vertical cluster approach, so you have to modify one of these settings to avoid a port conflict, and the reference article is to change the port number beginning with 9 to the 9 start port number, but if the port number that starts with 9 on my machine, such as 9080, 9082, will be the same as my WebSphere Application server configuration conflict, so the strategy I'm taking here is to change the third digit of the original port number to 1, as 8080 to 8180.

Open the Tomcat2/conf/server.xml file

1 To change the listening port for Tomcat off from 8005 to 8105
 <server port= "8005" shutdown= "Shutdown"
instead
 <server port= "8105" shutdown= "Shutdown"

2) Change the HTTP service port number from 8080 to 8180
Find
 <!--Define a non-ssl http/1.1 Connector on port 8080;
  & nbsp <connector port= "8080"
Change this 8080 to 8180

3) Change the AJP port number from 8009 to 8109
Find
 <!--Define an AJP 1.3 Connector on port 8009
    <connector port= "8009"
Change this 8009 to 8109

4) to send HTTP The proxy port is changed from 8082 to 8182 (this configuration is commented out by default and can be skipped)
Find
<connector port= "8082"
Change this 8082 to 8182

5) write a test JSP
Create a directory testcluster with a new test.jsp in it, which reads

<% System.out.println ("==========================="); %>
Put the testcluster under the WebApps of the TOMCAT1,TOMCAT2.

6) Start APACHE,TOMCAT1,TOMCAT2 and test
Through http://localhost/TestCluster/test.jsp access, refresh several times the page, view the TOMCAT1 and Tomcat2 window, you will be able to see the printed line "========================== = "and statistically speaking, the number of TOMCAT2 printed in the TOMCAT1 is twice times, you can see the request will be tomcat1,tomcat2 according to different weights of the shunt processing, achieve load balance.

For the following cluster configuration, please change the weights of TOMCAT1 and TOMCAT2 to the same in workers.properties, so that the request is distributed more evenly, and it will be easy to see the effect of the experiment.

Three: Configure the cluster
Only configure load balancing is not enough, but also session replication, that is, any one of the added session of Tomcat, is to replicate to other Tomcat, the cluster Tomcat have the same session

1. Modify TOMCAT1, Tomcat2 Server.xml, the cluster part configuration, that is, the <Cluster> node in the annotation of the deletion, and the Tomcat2 4001 port to 4002 to avoid conflict with Tomcat, of course, If you are two computers, you do not need to change the port, remove the annotation character can be
That is, cancel the following

<cluster classname= "Org.apache.catalina.cluster.tcp.SimpleTcpCluster" Managerclassname= " Org.apache.catalina.cluster.session.DeltaManager "expiresessionsonshutdown=" false "... <... Clusterlistener classname= "Org.apache.catalina.cluster.session.ClusterSessionListener"/> </Cluster>
Before and after the annotation tag <!----, enable this configuration to implement session replication between servers.

2. Add Jvmroute for TOMCAT1 and TOMCAT2 (skip this step and have the energy to experiment)
In TOMCAT1 and Tomcat2 's Server.xml file, find
<engine name= "Catalina" defaulthost= "localhost" >
Changed to
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT1" >
And
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT2" >

However, the actual configuration when I can not add Jvmroute properties, configuration has a problem.
Refreshing the browser window always prints on one of the Tomcat consoles as

SESSIONID:154678FA6D4D0ABD57658B750E7A3532.TOMCAT1 (in TOMCAT1 window)
Or
SESSIONID:3800571A532AECEA7280F45361861AD4.TOMCAT2 (in Tomcat2 window)

The results printed by the console show that the SessionID is generated on which Tomcat, so subsequent requests for that session will always be handled by this tomcat.

and notice that the form of SessionID is a suffix more than usual. Tomcat1 or. TOMCAT2, it's not clear why.

Please consider the actual situation when you choose the configuration.

3. Modify the test project Testcluster
Modify the test.jsp, the contents are as follows

<%@ page contenttype= "text/html; CHARSET=GBK "%> <%@ page import=" java.util.* "%>

4. Configure session Replication
Under the Testcluster directory, create a new Web-inf directory, web-inf under the new Web.xml, the contents are as follows

<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version= "2.4" > <display-name>TomcatClusterDemo</display-name> <distributable/> <web-app>

That is to add the attribute in the web.xml which needs the cluster application, it is indicated that the application can be used for multiple streaming and can be replicated in session.

Copy the Testcluster to the WebApps directory of the TOMCAT1, TOMCAT2, and restart the APACHE,TOMCAT1,TOMCAT2

5. Test session Replication
With http://localhost/TestCluster/test.jsp access, enter the name, the value is Unmi, submit the query, refresh the browser window several times, You will see that the same SessionID and values are printed in the two tomcat windows, and the results will be the same after each refresh.

If you do not add the Web.xml to the application, and test the test.jsp page above, each refresh stream will produce a different sessionid on the same tomcat, with a different sessionid at the same time.

The more immediate experience is to be sure to configure it yourself and carefully observe the output of the two Tomcat controls. This article is a reference to easy implementation of APACHE,TOMCAT cluster and load balancing practice experience, the book has more mapping.

Postscript:
The WebSphere application Server ND version has been configured with vertical and horizontal clusters, but its own application under the cluster environment does not want to malcontent this behemoth. Now eager to experience is Quartz how to adapt to the cluster environment, the focus of the problem is: Quartz Scheduled tasks with the Web application to start, and Web applications deployed in a clustered environment, how to ensure that at the same time only one task instance of the same name running.

So consider using Apache+tomcat to configure a lightweight Web application cluster, which typically uses Apache for HTTP streaming, including the was cluster, and is rarely used by IIS. Although the balancer application of Tomcat can also be configured for load streaming, the performance should be no better.

Web application clusters configured with APACHE+TOMCAT are cumbersome to deploy, always keeping a double copy of the application, and the was cluster does not need to know what JBoss does as a Web application cluster.

Well, the next thing to do is to do, and finally want to write a tool to complete from download to installation configuration, start, stop, restart the full automation, as well as the user-friendly interface.

References: 1. Easy implementation of APACHE,TOMCAT cluster and load balancing
2. Apache+tomcat Cluster Practice (some strange configuration with the above method)
3. Tomcat cluster and load balancing (explanation of cluster and load balancing)
4. How to prepare the cluster/session replication (TOMCAT5 Chinese Translation with documents)
5. How to configure load balancing (TOMCAT5 Chinese translations with documents)

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.