Apache and tomcat load balancing and clusters

Source: Internet
Author: User

I. Prepare the environment

1. Software Download

A) apache_2.0.55-win32-x86-no_ssl.msi;
B) apache-tomcat-5.5.17.rar
C) mod_jk-apache-2.0.55.so

: Http://download.csdn.net/detail/crazyiter_com/4178657



2. Software Installation

A) Apache installation path: e: \ ide \ apache \ apache2 (depending on your drive letter)


B) Tomcat location and name:


Note: Apache folders and tomcat 1 and tomcat 2 are stored in the same directory, for example, E: \ IDE.


Ii. Apache configuration

1. Find HTTP. conf In the conf directory under the Apache installation directory, and add the following sentence at the end of the file, for example:

Include "E: \ ide \ apache \ apache2 \ conf \ mod_jk.conf"
 
2. Put the mod_jk-apache-2.0.55.so file in the following folder:
E: \ ide \ apache \ apache2 \ modules


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

# Load mod_jk moduleloadmodule jk_module modules/mod_jk-apache-2.0.55.so # specify workers. properties file path jkworkersfile CONF/workers. properties # specify that the request is sent to Tomcat for processing. "controller" is in workers. the specified Server Load balancer controller jkmount/* In propertise /*. JSP 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 #======= worker AT1 ======= worker. tomcat1.port = 8009 # ajp13 port number, in Tomcat server. xml configuration. The default value is 8009worker. tomcat1.host = localhost # Tomcat host address. If not, enter the IP address worker. tomcat1.type = ajp13worker. tomcat1.lbfactor = 1 # server's weighted weight. The higher the value, the more requests are allocated. #========= tomcat2 ======= worker. tomcat2.port = 9009 # ajp13 port number, in Tomcat server. xml configuration. The default value is 8009worker. tomcat2.host = localhost # Tomcat host address. If not, enter the IP address worker. tomcat2.type = ajp13worker. tomcat2.lbfactor = 1 # server weighting ratio. The higher the value, the more requests are allocated. #======= controller, server Load balancer controller ======= worker. controller. type = lbworker. controller. balanced_workers = worker at1, tomcat2 # specify the worker that shares the request. controller. sticky_session = 1

Iii. tomcat configuration

1. Modify the tomcat2 configuration file server. XML, for example:







Iv. Test Server Load balancer

1. Create a new test. jsp file in the test directory. The content is as follows:
<%
System. Out. println ("========================= ");
%>
Put test under tomcat1 and tomcat2 webapps,


2. Start Apache, tomcat1, and tomcat2 for testing:
Access through http: // localhost/test. jsp and view the tomcat1 window. A line of "========" is printed"
Refresh again. tomcat2 also prints a refresh. We can see that the request will be processed by tomcat1 and tomcat2 in turn to achieve load balancing,



5. Test the Cluster

Note: 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. Remove the comments in tomcat1 and tomcat2:


2. Modify the port 4001 in Tomcat to 4002:


3. Add jvmroute, tomcat1, and tomcat2:

Note: Under tomcat1, jvmroute = "tomcat1", under tomcat2, jvmroute = "tomcat2 ".

4. Modify the test project:
Modify test. jsp as follows:

Modify test. JSP, content: <% @ page contenttype = "text/html; charset = GBK" %> <% @ page import = "Java. util. * "%> <HTML> 

5. Create a WEB-INF directory in test, and create web. xml under the WEB-INF, the content is 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>TomcatDemo</display-name>       <distributable/></web-app>
Note: Add <distributable/> to the Web. xml file of your application.
OK. Copy test to the webapps of tomcat1 and tomcat2, restart Apache, tomcat1, tomcat2,
Enter the URL http: // localhost/test. jsp
Create a session named zhangsan with a value of 123, submit the query, open an IE window, and then submit the query,

, We can see that the two Tomcat servers are Server Load balancer and the session is synchronized:


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.