Implementing Apache and Tomcat clusters and load balancing

Source: Internet
Author: User
Tags tomcat server

First, Apache, tomcat cluster and load balancing required resource software (with download address):

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

As shown in the following figure: Download add: http://download.csdn.net/detail/crazyiter_com/4178657

Second, Apache and Tomcat installation and precautions:

Load balance, that is, Apache customers request a balanced distribution to tomcat1,tomcat2 .... To deal with

1, the Apache installation path is: E:\ide\apache\Apache2 (specific according to their own letter can be determined)

2, Tomcat Place and name note:

Note: Apache folders and TOMCAT1, TOMCAT2 placed in the same directory, and: E:\ide.

Three, the Apache configuration and attention matters:

1, in the Apache installation directory in the Conf directory to find http.conf, at the end of the file plus the following sentence can be, the following figure:

Include "E:\ide\apache\Apache2\conf\mod_jk.conf"

2, put the mod_jk-apache-2.0.55.so file into the following folder:

E:\ide\apache\Apache2\modules

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" for the load allocation controller

specified in Workers.propertise Jkmount/* Controller

4, in http.conf the same directory under the new Workers.properties file, the contents are as follows:

Worker.list = Controller  #server list

#========tomcat1========

worker.tomcat1.port=8009         #ajp13端口号, Under Tomcat server.xml configuration, default 8009
worker.tomcat1.host=localhost  #tomcat的主机地址, if not the 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=9009       #ajp13端口号, server.xml configuration under Tomcat, default 8009
worker.tomcat2.host= localhost  #tomcat的主机地址, if not for this machine, please fill in the IP address
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1   # The weighted weight of the server, the higher the value, the more requests
#========controller, the load-balancing controller ========
worker.controller.type=lb
WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2   #指定分担请求的tomcat
worker.controller.sticky_session=1

Iv. configuration and precautions for Tomcat:

1, modify TOMCAT2 configuration file Server.xml:

The following figure:

Five, write a test JSP page:

1. Create a directory test. Inside a new test.jsp, the content is:

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

Put the test under the Tomcat1,tomcat2 WebApps, as shown in figure:

2, start Apache,tomcat1,tomcat2, to test:

Through http://localhost/test/test.jsp access, view the Tomcat1 window, you can see a printed line of "=========="

Refresh again, TOMCAT2 also print a, and then refresh, you can see the request will be tomcat1,tomcat2 in turn, to achieve a load balance

As shown in figure:

Six, tomcat cluster:

Note: Only configuring load balancing is not enough, but also session replication, that is, any one of the added sessions of Tomcat, is to replicate to other Tomcat, the cluster Tomcat has the same session.

1, remove the comments in the TOMCAT1 and TOMCAT2:

2, change the 4001 port in Tomcat to 4002:

3, increase JVMROUTE,TOMCAT1 and tomcat2 all need:

Note: Under the TOMCAT1 jvmroute= "TOMCAT1", Tomcat2 under Jvmroute= "TOMCAT3".

4. Modify Test items:

Modify test.jsp, as follows:

<%@ page contenttype= "text/html; CHARSET=GBK "%> <%@ page import= java.util.*"%> Server Info: <% out.println (request.getlocaladdr () + ":"

  + request.getlocalport () + "");%> <% out.println ("ID" + session.getid () + "");
  If there is a new session property 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); %> <form action= "index.jsp" method= "POST" > Name: <input type=text size=20 name= "Dataname" > &LT;BR&G
    T Value: <input type=text size=20 name= "DataValue" > &LT;BR&GT <input type=submit> </form> </body>  

Then in test new Web-inf directory, web-inf under the new Web.xml, the contents are as follows

<a href= "Http://down-admin.chinaz.com/zczamzk/NS" = "<a href= ns =" <web-app http:java.sun.com XML ns java EE? 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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version= "2.4" >
       < Display-name>tomcatdemo</display-name>
       
</web-app>

Note: Add to your application's Web.xml

OK, copy test to TOMCAT1,TOMCAT2 WebApps, restart Apache,tomcat1,tomcat2,

Enter URL http://localhost/test/test.jsp

Create a new name called Crazyiter, the value of Jiangge session, submit query, open a new IE window, and then submit the query, as shown in the figure, you can see that two Tomcat is load balanced, and session synchronization

Note: In the absence of networking, according to the above configuration to start Tomcat to error, the following figure:

According to my analysis, should be and configure the IP of these related, disconnected the situation of the use of ipconfig no IPv4 IP address.

Thanks Netizen: Rodechon, this article in the original Text foundation, after the author constructs the process to encounter the question, and in the original text ignores some details to make the adjustment.

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.