Apache Tomcat cluster and Server Load balancer

Source: Internet
Author: User
Tags apache tomcat

I. Environment and version.

1. apache2.2

2. Tomcat 6.0.8

3. mod_jk.so (: http://apache.justdn.org/tomcat/tomcat-connectors/jk/binaries/
) Select the appropriate platform and version.

4. System: Win XP

 

Ii. installation.

1. install Apache. Directory: D:/appserv/apache2.2 /. The following% Apache_home %
Replace

2. install Tomcat in two ways: Tomcat 1 and tomcat 2. The directory names are% Tomcat1_home %, % tomcat2_home %

3. Place mod_jk.so in% Apache_home %/modules/
For example, under D:/appserv/apache2.2/modules.

 

2. Configure the Cluster

This is just a simple cluster configuration le. The general principle is to first process all requests with Apache. When a. jsp page is encountered, the request is forwarded to Tomcat for processing.

 

1. Modify httpd. conf

Backup% Apache_home %/Conf
The httpd. conf file under the directory, open httpd. conf, add a line of code in the appropriate location, and load the configuration of mod_jk.

Include "D:/appserv/apache2.2/CONF/mod_jk.conf"

 

2. Create mod_jk.conf

 

Create mod_jk.conf in the same directory of httpd. conf

# Load mod_jk module <br/> loadmodule jk_module modules/mod_jk.so <br/> # specify workers. properties file path <br/> jkworkersfile CONF/workers. properties <br/> # specify the requests that are sent to Tomcat for processing. "controller" is specified in workers. the specified Server Load balancer controller in propertise <br/> jkmount /*. JSP controller <br/>

The general meaning of this file is,

First load jk_module, and then forward it according to the workers. properties file.

If the end of The. jsp file is reached, the friend controller processes the file. The Controller is defined in the following file.

3. Create workers. Properties

Worker. list = controller, worker at1, tomcat2 # Server LIST <br/>#========= worker AT1 =========< br/> worker. tomcat1.port = 8009 # ajp13 port number, in Tomcat server. xml configuration, 8009 by default <br/> worker. tomcat1.host = localhost # Tomcat host address. If not, enter the IP address <br/> worker. tomcat1.type = ajp13 <br/> worker. tomcat1.lbfactor = 1 # server weight, the higher the value, more requests are allocated. <br/>#======== tomcat2 =======< br/> worker. tomcat2.port = 9009 # ajp13 port number, in Tomcat server. xml configuration, 8009 by default <br/> worker. tomcat2.host = localhost # Tomcat host address. If not, enter the IP address <br/> worker. tomcat2.type = ajp13 <br/> worker. tomcat2.lbfactor = 1 # server weighting ratio. The higher the value, the more requests are allocated. <br/>#======= controller, server Load balancer controller ========< br/> worker. controller. type = LB <br/> worker. controller. balanced_workers = worker at1, tomcat2 # specify tomcat to share the request <br/> worker. controller. sticky_session = 1 <br/>

 

Apache configuration ends here. Review,

Added mod_jk.so to the modules directory.

Modify httpd. conf In the conf directory to create mod_jk.conf and workers. properties.

 

4. Modify Tomcat server. xml

Because two Tomcat servers are installed, You need to modify one of the ports. For example, tomcat2

Open% Tomcat2_home %/Conf
Server. XML in the directory (remember to back up)

A. Modify server port 8005 to 9005,

<Server port = "9005" shutdown = "shutdown">

B. Modify the access port 8080 to 9090,

<Connector Port = "9080" protocol = "HTTP/1.1" <br/> connectiontimeout = "20000" <br/> redirectport = "8443"/>

C. Modify the ajp13 port to 9009.

<Connector Port = "9009" protocol = "AJP/1.3" redirectport = "8443"/>

OK. The configuration is complete.

The following is a test.

Create a folder named test and put test. jsp in it.

<% @ Page contenttype = "text/html; charset = gb2312 "%> <br/> <HTML> <br/> <pead> <br/> <title> JSP test page </title> <br/> </ head> <br/> <body> <br/> <% Out. println ("<p> Hello world! </H1> "); %> <br/> <% <br/> system. out. println ("=========================== "); <br/> %> <br/> </body> <br/> </ptml>

Copy them to the webapps directory of tomcat1 and tomcat2.

 

Open Apache, tomcat1, and tomcat2.

Enter http: // localhost/test. jsp for access.

You can see that a row of bars is printed in one of the consoles.

Refresh, and you can see that it is printed on another tomcat console. This loop.

Load Balancing is achieved.

 

2. Configure cluster session Synchronization

According to the two Tomcat sessions in the previous step, the Tomcat cluster configuration is enabled to synchronize the sessions.

1. Modify the server. xml of tomcat1 and tomcat2.

Set<Cluster classname = "org. Apache. Catalina. Ha. tcp. simpletcpcluster"/>

.

2. Modify

<Engine name = "Catalina" defaulthost = "localhost" jvmroute = "tomcat1">

Add the jvmroute attribute.

 

3. Create a WEB-INF directory under the test directory and add web. xml

The Code 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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4"> <br/> <display-Name> tomcatdemo </display-Name> <br/> <distributable/> <br/> </Web-app> <br/>

4. Test

Create session. jsp

<% @ Page import = "Java. util. * "%> <br/> <HTML> <pead> <title> cluster app test </title> </pead> <br/> <body> <br/> Server info: <br/> <% <br/> out. println (request. getlocaladdr () + ":" + request. getlocalport () + "<br>"); %> <br/> <% <br/> out. println ("<br> ID" + session. GETID () + "<br>"); <br/> // if a new session attribute is set <br/> string dataname = request. getparameter ("dataname"); <br/> If (dataname! = NULL & dataname. length ()> 0) {<br/> string datavalue = request. getparameter ("datavalue"); <br/> session. setattribute (dataname, datavalue); <br/>}< br/> out. print ("<B> session List </B>"); <br/> enumeration E = session. getattributenames (); <br/> while (E. hasmoreelements () {<br/> string name = (string) E. nextelement (); <br/> string value = session. getattribute (name ). tostring (); <br/> out. println (name + "=" + value + "<br>"); <br/> system. out. println (name + "=" + value); <br/>}< br/>%> <br/> <form action = "session. JSP "method =" Post "> <br/> Name: <input type = text size = 20 name = "dataname"> <br/> value: <input type = text size = 20 name = "datavalue"> <br/> <input type = submit> <br/> </form> <br/> </body> <br/> </ptml> <br/>

 

Make sure that both Tomcat webapps directories have test

 

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

The refresh page displays the session IDs of tomcat1 and tomcat2, respectively. We found that the two have been 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.