Use Apache and tomcat to achieve cluster and load balancing

Source: Internet
Author: User
Tags websphere application server
I. Software Environment
1. Apache: Apache 2.0.55 (downloaded from http://httpd.apache.org/) (click to download Apache 2.0.55)
2. Tomcat: Tomcat 5.5.25 (downloaded from http://tomcat.apache.org/) (click to download Tomcat 5.5.25 zip)
3. mod_jk: locate Tomcat under the page http://tomcat.apache.org/download title
Connect (
Click to download the mod_jk-apache-2.0.55.so), looks like a Unix/Linux dynamic library, the actual should be a Win32 DLL
The dynamic library probably uses this extension to maintain the consistency of configurations on different platforms.

Ii. Server Load balancer
Apache is used for traffic distribution, and requests are processed according to the weight and the load at that time, such as tomcat1 and tomcat2. ..

1. install Apache and tomcat
I installed Apache on D:/Apache Group/apache2.
Decompress tomcat in two steps: D:/Apache Group/tomcat5_1, D:/Apache Group/tomcat5_2
If you cluster Tomcat of different versions, the directory can be found at tomcat4_3 (Version 4. x's third Tomcat server), tomcat6_4 (version 6. x's third Tomcat server). This is the habit of unmi.

2. Modify the Apache configuration file HTTP. conf

Find HTTP. conf In the conf directory under the Apache installation directory, and add the following sentence to the end of the file.

Include CONF/mod_jk.conf

3. Create the mod_jk.conf file in the same directory as HTTP. conf. The content is as follows:

# Load mod_jk Module
Loadmodule jk_module modules/mod_jk-apache-2.0.55.so

# Specify the workers. properties file path
Jkworkersfile CONF/workers. Properties

# Specify the requests to Tomcat for processing. "controller" is the Server Load balancer controller specified in workers. propertise.
Jkmount/*. jsp Controller

If you want to specify *. Do as well, add another line.
Jkmount/*. Do Controller

If you want to distribute all requests, you only need to write
Jkmount/* Controller

4. Create the workers. properties file in the same directory as HTTP. conf. The content is as follows:

Worker. List = controller, worker at1, tomcat2 # Server LIST
#========= Export AT1 ========
Worker. tomcat1.port = 8009 # ajp13 port number, which is configured in server. xml under tomcat. The default value is 8009.
Worker. tomcat1.host = localhost # Tomcat host address. If not, enter the IP address
Worker. tomcat1.type = ajp13
Worker. tomcat1.lbfactor = 1 # server weighting ratio. The higher the value, the more requests are allocated.

#======= Tomcat2 =========
Worker. tomcat2.port = 8109 # ajp13 port number, which is configured in server. xml under tomcat. The default value is 8009.
Worker. tomcat2.host = localhost # Tomcat host address. If not, enter the IP address
Worker. tomcat2.type = ajp13
Worker. tomcat2.lbfactor = 2 # server weighting ratio. The higher the value, the more requests are allocated.

#======= Controller, Server Load balancer controller ========
Worker. Controller. type = LB
Worker. Controller. balanced_workers = worker at1, tomcat2 # specify Tomcat that shares the request
Worker. Controller. sticky_session = 1

5. Modify the tomcat configuration file server. xml
For example
If you are a horizontal cluster, that is, Tomcat is installed on different computers. The number of Tomcat installations is one, so you do not have to modify the tomcat configuration file. Here we install two
Tomcat implements the vertical cluster mode, so you must modify one of the settings to avoid port conflict. According to the reference article, the original port number starting with 9 is changed to the port number starting with 9, but in my
If the port numbers starting with 9, such as 9080 and 9082
Server Configuration conflict. Therefore, the policy adopted here is to change the third digit of the original port number to 1, for example, 8080 to 8180.

Open the tomcat2/CONF/server. xml file

1) Change the listening port for disabling Tomcat from 8005 to 8105.
That is

Change

2) Change the HTTP service port number from 8080 to 8180
Find


Change 8080 to 8180.

3) change the AJP port number from 8009 to 8109.
Find


Change 8009 to 8109.

4) change the HTTP Proxy port from 8082 to 8182 (this configuration is commented out by default. skip this step)
Find

Change 8082 to 8182.

5) Compile a test JSP
Create a directory named testcluster and create a test. jsp file. The content is

<%
System. Out. println ("========================= ");
%>

Put testcluster under the webapps of tomcat1 and tomcat2.

6) start Apache, tomcat1, and tomcat2 for testing.
Connect
Http: // localhost/testcluster/test. jsp
Visit and refresh the page several times. Check the tomcat1 and tomcat2 windows. You can see that a line is printed.
Line "=============================", and statistically speaking, about twice the number of tomcat2 prints.
The request will be distributed by tomcat1 and tomcat2 according to different weights to achieve load balancing.

For the following cluster configuration, change the weights of worker AT1 and worker at2 to the same in workers. properties so that requests are evenly distributed, so that the experiment results can be seen easily.

3. Configure the Cluster
Only configuring Server Load balancer does not work, but also requires session replication. That is to say, the session added by any Tomcat must be synchronized to other Tomcat servers. tomcat in the cluster has the same session.

1. Modify the server. xml of tomcat1 and tomcat2 to configure the Cluster Delete the node in the annotator and change the port 4001 of Tomcat 2 to port 4002 to avoid conflicts with Tomcat. Of course, if you are using two computers, you do not need to change the port. Remove the annotator.
Cancel the following

1
2 managerclassname = "org. Apache. Catalina. Cluster. session. deltamanager"
3 expiresessionsonshutdown = "false"
4
5
6
Comment mark before and after Enable this configuration to replicate sessions between servers.

2. Add jvmroute for Route 1 and tomcat 2 (skip this step first and try again with the energy)
In the tomcat1 and tomcat2 server. XML files, find

Change

And

However, the jvmroute attribute cannot be added during actual configuration, but the configuration is faulty.
Refreshing the browser window is always output in a tomcat console as shown in Figure

Sessionid: 154678fa6d4d0abd57658b750e7a3532. tomcat1 (in the tomcat1 window)
Or
Sessionid: 3800571a532aecea7280f45361861ad4. tomcat2 (in the tomcat2 window)

The result printed on the console shows the Tomcat on which the sessionid is generated. Subsequent requests for this session will always be processed by this tomcat.

It is also noted that the form of sessionid is one more suffix than in general. tomcat1 or. tomcat2, and it is still unclear why.

Make a choice based on the actual situation.

3. Modify the test project testcluster.
Modify test. jsp as follows:

<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. *" %>

<%
System. Out. println ("sessionid:" + session. GETID ());
%>
Server info:
<%
Out. println (request. getservername () + ":" + request. getserverport () +"
"); %>
<%
Out. println ("
ID "+ session. GETID () +"
"); // If a new session attribute is set
String dataname = request. getparameter ("dataname ");
If (dataname! = NULL & dataname. Length ()> 0 ){
String datavalue = request. getparameter ("datavalue ");
Session. setattribute (dataname, datavalue );
}
Out. Print ("Session list
");
Enumeration E = session. getattributenames ();
While (E. hasmoreelements ()){
String name = (string) E. nextelement ();
String value = session. getattribute (name). tostring ();
Out. println (name + "=" + value +"
");
System. Out. println (name + "=" + value );
}
%>

4. Configure session replication
Create a new WEB-INF directory under the testcluster directory and a new Web. xml under the WEB-INF with the following content:


Tomcatclusterdemo


That is, adding properties to the Web. xml of the cluster application indicates that the application can be distributed to multiple applications for session replication.

Copy testcluster to the webapps directory of tomcat1 and tomcat2, and restart Apache, tomcat1, and tomcat2.

5. Test session replication
Connect
Accessed through http: // localhost/testcluster/test. jsp. The input name is name and the value is
Unmi, submit the query, and refresh the browser window several times. You will see that the same sessionid and its value are printed in both Tomcat windows, and the results printed after each refresh are
Same.

If not. add XML, and test the test above. on the JSP page, different sessionids are generated each time the data is refreshed and distributed to different Tomcat servers, and different sessionids are generated at intervals on the same Tomcat server.

For a more personal experience, you must configure it by yourself and carefully observe the output on the control of the two Tomcat servers. This article describes how to easily implement apacjavascript: void (null); he, Tomcat cluster, and Server Load balancer. There are many textures in this article.

Postscript:
Use
WebSphere Application Server nd
The version has been configured with vertical and horizontal clusters, but the applications in the cluster environment have not tried to make such a huge deal. What I want to try out is quartz.
The focus of the problem is how to adapt to the cluster environment: the quartz scheduled task is started with the web application, while the Web
How to ensure that only one task instance with the same name is running when applications are deployed in the cluster environment.

Therefore, we will consider using Apache + Tomcat to configure a lightweight web application cluster. Apache is generally used for HTTP shunting, including was clusters, and IIS is rarely used. Although Tomcat's balancer application can also be configured for load balancing, the performance should not be good.

The Web application cluster configured with Apache + Tomcat is difficult to deploy. It always requires duplicate copies of applications, while the was cluster does not, I don't know how JBoss is working as a web application cluster.

Now, let's do what we should do. Finally, we hope we can write a tool to complete the full automation from download to installation configuration, startup, stop, restart, and user-friendly interface.

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.