Combining Apache and Tomcat for cluster and load balancing

Source: Internet
Author: User
Tags xmlns server port tomcat tomcat server websphere application server
In this paper, the basic reference from the easy implementation of APACHE,TOMCAT cluster and load balancing, 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 stickers in the original text, if you do not want to personally do not want to see the configuration effect, you can view the original text.

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 Download mod_jk-apache-2.0.55.so) and look like a Unix /linux under the dynamic library, the actual should be a Win32 DLL dynamic library, presumably to maintain the consistency of the different platform configuration, only use this extension.

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

1. Installing Apache,tomcat
I installed Apache in D:/apache group/apache2
Extract two points tomcat, respectively, in D:/apache Group/tomcat5_1,d:/apache group/tomcat5_2
If you cluster different versions of Tomcat, the directory will be available Tomcat4_3 (third Tomcat server with version 4.x), Tomcat6_4 (third Tomcat server version 6.x), which is the custom of Unmi.

2. Modify the Apache configuration file http.conf

Locate the http.conf in the Conf directory under the Apache installation directory and add the following sentence to the file at the end.

Include conf/mod_jk.conf

3. http.conf the new mod_jk.conf file in the same directory as the following #加载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 load distribution controllers specified in the Workers.propertise

If you also want to specify *.do also tap and add another line
Jkmount/*.do Controller

If you want to divert all your requests, just write the
Jkmount/* Controller

4. Create a new Workers.properties file in the http.conf directory with the following content (you may want to remove comments that do not appear 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 the IP address worker.tomcat1.type=ajp13 worker.tomcat1.lbfactor = 1 #server的加权比重, the higher the value, the more requests to be divided #======== 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 the IP address worker.tomcat2.type=ajp13 worker.tomcat2.lbfactor = 2 #server的加权比重, the higher the value, the more requests to be divided #======== Controller, Load balancer ======== worker.controller.type=lb WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2 # Specify the tomcat worker.controller.sticky_session=1 to share the request

5. Modify the Tomcat configuration file Server.xml
If you are a horizontal cluster, that is, install Tomcat,tomcat on a different computer with a number of installations, you do not have to modify the Tomcat configuration file. I'm here to install two Tomcat on the same computer, The implementation of the vertical cluster mode, so you must modify one of the settings to avoid port conflicts, according to the reference article is the original 9 port number to start with 9 port number, but on my machine if the port number starting with 9, such as 9080, 9082 will be with my WebSphere Application server configuration conflicts, so the strategy I'm taking here is to change the third bit of the original port number to 1, such as 8080 to 8180.

Open tomcat2/conf/server.xml file

1) Turn off Tomcat's listening port to change from 8005 to 8105
to
 <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 here 8080 to 8180

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

4) The HTTP Proxy port changed from 8082 to 8182 (this configuration is commented out by default, you can skip this step)
Find
<connector port= "8082"
Change here 8082 to 8182

5) write a test JSP
Create a directory testcluster with a new test.jsp, with the contents

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

6) Start APACHE,TOMCAT1,TOMCAT2, test
With http://localhost/TestCluster/test.jsp access, refresh a few more pages, view the windows of TOMCAT1 and TOMCAT2, and you'll see a line printed "========================== = ", and statistically speaking, approximately twice times the number of TOMCAT2 printed is in the TOMCAT1, you can see that the request will be tomcat1,tomcat2 according to the different weight of the shunt processing, to achieve load balancing.

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

Three: Configure the cluster
Only configure load balancing is not enough, but also to copy the session, that is, any one of the Tomcat added session is to be replicated to other Tomcat, the cluster of Tomcat has the same session

1. Modify the TOMCAT1, Tomcat2 Server.xml, the cluster part configuration, that is, the <Cluster> node in the comment box, and change the TOMCAT2 4001 port to 4002 to avoid conflict with Tomcat, of course, If it is two computers, do not change the port, remove the comment symbol 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 comment mark <!----, enable the configuration to achieve session replication between servers.

2. Add Jvmroute for TOMCAT1 and TOMCAT2 (skip this step first, have the energy to experiment)
In the TOMCAT1 and Tomcat2 server.xml files, locate
<engine name= "Catalina" defaulthost= "localhost" >
respectively, replace
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT1" >
And
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT2" >

However, the actual configuration of the time I can not add jvmroute properties, configuration, but there is a problem.
Refreshing the browser window is always in one of the Tomcat console output shapes such as

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

The results from the console print show that the SessionID is generated on which Tomcat, and 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 unclear why.

The configuration is subject to your choice.

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

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

4. Configure session Replication
Create a new Web-inf directory under the Testcluster directory, and create a new Web. XML under Web-inf with the following content

<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, adding attributes to Web. XML for applications that require clustering, which indicates that the application can be used for multiple shunt processing to replicate the session.

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

5. Test the session Copy
Through http://localhost/TestCluster/test.jsp access, enter the name of the username, the value is Unmi, submit the query, and 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 are the same after each refresh.

If you do not add to the application's Web. XML, and also test the test.jsp page above, each refresh stream to different Tomcat will produce a different sessionid, on the same Tomcat is also a gap between the SessionID.

A more personal experience is to be sure to configure it yourself and carefully observe the output of the two Tomcat controls. Because this article is a reference to the easy implementation of APACHE,TOMCAT cluster and load balancing practice experience, there are more stickers in this book.

Postscript:
The WebSphere Application Server ND Edition has been configured with vertical and horizontal clusters, but the applications under the cluster environment do not want to albeit this behemoth. What is eager to experience now is how Quartz adapts to the cluster environment, the focus of the problem is: Quartz timed tasks are launched with the Web application, and Web applications are deployed in a clustered environment to ensure that only one task instance with the same name runs at the same time.

So consider configuring a lightweight Web application cluster with Apache+tomcat, which is typically done using Apache, including the was cluster, and rarely with IIS. While the simple use of Tomcat's balancer application can be configured for offload, the performance should be no better.

A Web application cluster configured with Apache+tomcat is a hassle to deploy, always keeping a double copy of the application, and the was cluster does not need to know how JBoss does the Web application cluster.

OK, the next thing to do, and finally hope to write a tool can be completed from download to installation configuration, start, stop, restart the full automation, as well as the user interface.

Reference: 1. Easily implement Apache,tomcat clustering and load balancing
2. Apache+tomcat cluster practice (with some unusual configuration methods for the above method)
3. Tomcat cluster and load balancing (explanation of cluster and load balancing)
4. How to prepare the cluster/session replication (Tomcat5 the Chinese translation of the documentation)
5. How to configure load balancing (Chinese translation of TOMCAT5 's own 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.