Easily implement Apache,tomcat clustering and load balancing

Source: Internet
Author: User
Tags xmlns zip tomcat tomcat server
For the first attempt to configure cluster and load balancing, follow this article to configure the success, Memo.
Transferred from: http://hi.baidu.com/luodaijun/blog/item/5bbe4cfb5ffef864034f56a1.html

Author: Rodeix are ldj_work#126.com, reproduced please maintain integrity

0, environmental description

apache:apache_2.0.55 A

tomcat:apache-tomcat-5.5.17 (Zip version) of 2

MOD_JK:: mod_jk-apache-2.0.55.so One

First part: Load Balancing

Load balancing, that is, Apache to the customer request balanced distribution to tomcat1,tomcat2 .... To deal with

1. Installing Apche,tomcat

http://httpd.apache.org/Download Apache 2.0.55

http://tomcat.apache.org/download-55.cgi Download tomcat5.5 zip version (unzip, green version)

http://apache.justdn.org/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.15/download MOD_JK, note and Apache version match

According to the JDK, my path is: e:/ide/apache/apache2

Extract two copies of Tomcat with the path E:/IDE/TOMCAT1,E:/IDE/TOMCAT2

Download MOD_JK

2. Modify the Apache configuration file http.conf

Found in the Conf directory under the Apache installation directory http.conf

Add the following sentence to the end of the document.

Include "e:/ide/apache/apache2/conf/mod_jk.conf"

2. 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" is the load distribution controller specified in Workers.propertise jkmount/*.jsp Controller3. Create a new workers.properties file in the http.conf directory with the following contentworker.list = controller,tomcat1,tomcat2 #server list #========tomcat1======== worker.tomcat1.port=8009 #ajp13 port number, Under Tomcat server.xml configuration, default 8009
Worker.tomcat1.host=localhost #tomcat的主机地址, if not for this machine, please fill in the IP address
Worker.tomcat1.type=ajp13
Worker.tomcat1.lbfactor = 1 #server的加权比重, the higher the value, the more requests are #========tomcat2======== worker.tomcat2.port=9009 #ajp13 port number, Under Tomcat server.xml configuration, 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 #server的加权比重, the higher the value, the more requests to be divided
#========controller, Load Balancer controller ========
Worker.controller.type=lb
WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2 #指定分担请求的tomcat
Worker.controller.sticky_session=14. Modify the Tomcat configuration file Server.xmlIf you install Tomcat,tomcat on a different PC with a number of installs, you may not have to modify the Tomcat profile I am here to install two Tomcat on the same computer, so you need to change one of the settings to open tomcat2/conf/ Server.xml file




5. Write a test JSP to create a directory test. Inside a new test.jsp, content is <%
System.out.println ("===========================");
%>
Put the test in Tomcat1,tomcat2 's WebApps . 6. Start the APACHE,TOMCAT1,TOMCAT2, test through http://localhost/test/test.jsp access, View TOMCAT1 window, you can see the print a line "==========" and then refresh again, TOMCAT2 also print a, and then refresh, you can see the request will be TOMCAT1,TOMCAT2 take turns processing, to achieve load balancing


In the second part, configuring the cluster to configure only 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 the Server.xml, the cluster part of the configuration in the comment is deleted, and the TOMCAT2 4001 port to 4002 to avoid conflict with Tomcat, of course, if it is two computers, do not change the port, You can remove the comment symbol





2. Modify Test ProjectModify test.jsp, the content is as follows <%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.util.*"%>
<body>
Server Info:
<%
Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br>");%>
<%
Out.println ("<br> ID" + session.getid () + "<br>"); 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 ("<b>session list </b>"); Enumeration E = Session.getattributenames ();
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Session.getattribute (name). toString ();
OUT.PRINTLN (name + "=" + value+ "<br>");
SYSTEM.OUT.PRINTLN (name + "=" + value);
}
%>
<form action= "index.jsp" method= "POST" >
Name: <input type=text size=20 name= "Dataname" >
<br>
Value: <input type=text size=20 name= "DataValue" >
<br>
<input type=submit>
</form>
</body>
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app> Note: Add Web. XML to your app<distributable/>OK, tell the test copy to Tomcat1,tomcat2 WebApps, restart Apache,tomcat1,tomcat2, enter the URL http://localhost/test/test.jsp a new name called Xiaoluo, the value of Cdut session, submit a query, a new IE window, and then submit the query, as shown, two Tomcat is load balanced, and session synchronization

It is important to note that when you configure the cluster to set Jvmroute, the original engine is commented out and the comment on the line above is changed to
<!--should set Jvmroute to support load-balancing via JK/JK2 ie:-
<engine name= "Standalone" defaulthost= "localhost" debug= "0" jvmroute= "TOMCAT1" >

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.