Apache+tomcat cluster, load balancing

Source: Internet
Author: User

In the development test environment, may be insufficient hardware resources, how to implement the cluster, the following example:

Environment:

Win7

Tools:

    1. Apache One

    2. Tomcat two were named Tomcat1, TOMCAT2

    3. MOD_JK One (try the same version as Apache)

Installation, MOD_JK is not installed. This file is copied to the modules directory in the Apache directory.

Configuring the Tomcat Cluster

Open Tomcat under Conf/server.xml file, find <engine name= "Catalina" defaulthost= "localhost", and add Jvmroute properties to support AJP load balancing.

TOMCAT1 to <engine name= "Catalina" defaulthost= "localhost" jvmroute= "JVM" >

Tomcat2 to <engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1" >

Since our two tomcat runs on a single machine, it is necessary to modify one of the Tomcat ports to ensure that the two runs simultaneously without conflict (and of course, if two Tomcat is not running on a single machine, this step can be omitted). We modified here is TOMCAT2, open tomcat2 under the Conf/server.xml file, modified as follows:

<server port= "8005" shutdown= "Shutdown" >

Revision changed to

<server port= "9005" shutdown= "Shutdown" >

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>

Revision changed to

<connector port= "9080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "9443"/>.

<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>

Revision changed to

<connector port= "9009" protocol= "ajp/1.3" redirectport= "9443"/>

Port modification is complete, we need to configure session replication in a clustered environment, open tomcat under Conf/server.xml file, <Engine></Engine> Join in the node (Tomcat5 seems to have the following properties but is commented, Tomcat6 started without all the here to join) the following code:

<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"

channelsendoptions= "6" >

<manager classname= "Org.apache.catalina.ha.session.BackupManager"

Expiresessionsonshutdown= "false"

Notifylistenersonreplication= "true"

mapsendoptions= "6"/>

<!--

<manager classname= "Org.apache.catalina.ha.session.DeltaManager"

Expiresessionsonshutdown= "false"

Notifylistenersonreplication= "true"/>

-

<channel classname= "Org.apache.catalina.tribes.group.GroupChannel" >

<membership classname= "Org.apache.catalina.tribes.membership.McastService"

Address= "228.0.0.4"

Port= "45564"

Frequency= "500"

Droptime= "/>"

<receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"

address= "Auto"

Port= "4001"

selectortimeout= "100"

maxthreads= "6"/>

<sender classname= "Org.apache.catalina.tribes.transport.ReplicationTransmitter" >

<transport classname= "Org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<interceptor classname= "Org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<interceptor classname= "Org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

<interceptor classname= "Org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>

</Channel>

<valve classname= "Org.apache.catalina.ha.tcp.ReplicationValve"

Filter= ". *\.gif|. *\.js|. *\.jpeg|. *\.jpg|. *\.png|. *\.htm|. *\.html|. *\.css|. *\.txt "/>

<deployer classname= "Org.apache.catalina.ha.deploy.FarmWarDeployer"

Tempdir= "/tmp/war-temp/"

Deploydir= "/tmp/war-deploy/"

Watchdir= "/tmp/war-listen/"

Watchenabled= "false"/>

<clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>

The TCP port on the same machine can not conflict, so in the above configuration, for TOMCAT2 need to modify the node Culster->channel->reciver port property, TOMCAT1 used 4001, Then change this port property of Tomcat2 to 4002.

Once these configurations are complete, we can then add a simple application to the two tomcat to actually test the cluster effect.

Testing the Tomcat cluster

Under Tomcat/webapps, create a new folder for the Test,test folder under the Web-inf directory, create a standard Web. xml file, the file must be added <distributable/> tags to support distributed applications.

The Web. xml file reads as follows:

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:   schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0" metadata-complete= "true" >

<display-name>welcome to Test Tomcat6 cluster</display-name>

<description>welcome to Test Tomcat6 cluster</description>

<distributable/>

</web-app>

Create the test.jsp file under the test folder with the following file contents:

<%@ page contenttype= "text/html; Charset=utf-8 "%>

<%@ 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.println ("<b>session list </b><br>");

System.out.println ("============================");

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= "test.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>

This simple JSP can save the submitted value to the session and display it at the same time.

Start the TOMCAT1, and then start the TOMCAT2 after TOMCAT1 startup is complete. First visit the TOMCAT1 corresponding test Project http://localhost:8080/test/test.jsp page, enter a value and submit, and then transfer the link to the TOMCAT2 corresponding test project http://localhost:9080/ test/test.jsp page to see if the session content is the same, if the same will indicate the success of the session replication, that is, the cluster success.

Next we add an Apache HTTP server to the two tomcat front ends, and all access requests are forwarded through this Http server, depending on the busy situation of TOMCAT1 or 2, and forwarding to TOMCAT1 or TOMCAT2 to process the user request.

Load Balancing Configuration

1. Set up in the same directory as the http.conf file The contents of the workers.properties file are as follows:

#下面是分发控制器 be careful not to put a tomcat instance. If only the load does not cluster. Here are the Tomcat instances to put up. Separated by commas
worker.list = LoadBalancer #server list to match the blue font in the http.conf file looks #Tomcat1实例配置 This is consistent with the jvmroute of the Tomcat configuration file Service.xml
Worker.jvm.host=localhost
worker.jvm.port=8009
Worker.tomjvmcat1.type=ajp13
The higher the #分发权重 value, the greater the load
Worker.jvm.lbfactor = 1

#Tomcat2实例配置
Worker.jvm1.host=localhost
worker.tomjvm1cat2.port=9009
Worker.tomjvm1cat2.type=ajp13
The higher the #分发权重 value, the greater the load
Worker.jvm1.lbfactor = 1

#负载均衡分发控制器 have a session copy.
Worker.loadbalancer.type=lb
Worker.loadbalancer.balance_workers=jvm,jvm1 #这里在的jvm, jvm1 are consistent with aliases that need to be set on top Tomcat worker.loadbalancer.sticky_session =true #如果是只追求速度. Can be set to False
Worker.loadbalancer.sticky_session_force=true #如果是只追求速度. Can be set to False


2: Modify the http.conf file under conf under Apache Root, and add the following code at the end of the file

#以下为tomcat集成配置部分
#加载mod_jk Module, the red part is consistent with the file name you downloaded and copied to the modules directory
LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.3.so

#指定 workers.properties file path, the red part is the file name next to the same directory as the http.conf file
Jkworkersfile conf/workers.properties

# Where to put JK logs
Jklogfile Logs/mod_jk.log

# Set the JK log level [Debug/error/info]
Jkloglevel Info

# Select the log format
Jklogstampformat "[%a%b%d%h:%m:%s%Y]"

# jkoptions indicate to send SSL KEY SIZE,
Jkoptions +forwardkeysize +forwarduricompat-forwarddirectories

# Jkrequestlogformat Set the request format
Jkrequestlogformat "%w%V%T"

#指定那些请求交给tomcat处理, "loadbalancer " is the load allocation controller specified in Workers.propertise _jkmount/* loadbalancer

#这里应该注意一下. A red part of the address to put in the resource file required to publish the project. Like some photos and JS and css ... And so on. The project structure must be the same.
DocumentRoot "D:\Program Files (x86) \apache software Foundation\apache2.2\htdocs "

Jkmount/*.jsp LoadBalancer
Jkmount/*.do LoadBalancer
jkmount/servlet/* LoadBalancer
#关掉主机Lookup, if on, it affects performance and can have a delay of more than 10 seconds.
Hostnamelookups OFF

Finally, start the Tomcat and Apache service separately, then enter http://localhos/test/in the address bar test.jsp, input and submit, you can see the output on a tomcat console, then turn off a print tomcat, the address is still valid, but the printed information appears on the other Tomcat.


Apache+tomcat cluster, load balancing

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.