Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a load balancing environment for Apache+tomcat+

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The following brings you how to build an environment for Apache+tomcat+ load balancing, hoping to give you some help in practical application. Load balancing involves many things, not many theories, and there are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

Apache2.0 is a standards-compliant, fast, mature web cloud server that specializes in providing static content, such as static HTML pages and images. Tomcat web servers are useful for handling Java server Pages and Java servlet, but not as fast as Apache in providing static content. Therefore, if you want to build a fast, scalable web application, you need an Apache service that provides JSP and servlet request services for multiple tomcat processing, through the use of an Apache module, mod_jk (load balancing through session association, or "sticky" session). The session association explains that when the client browser requests a JSP page for the first time and the request is received by Apache, the load balancer redirects the request to one of the two tomcat servers; further requests from clients with the same rules are automatically forwarded to the same tomcat server to retrieve the user's session data.

This document describes how to configure Apache 2.x to dispatch JSP and servlet requests to two Tomcat 4.x via port listening. This setting is done on the Linux system, your situation may be different.

1. Download the required software

Apache 2.x Binaries (If you plan to use ssl, get the source)

Tomcat 4.x Binaries

The JK module

2. Compile, Install and Configure Apache2.1 Install Apache.

Linux: gunzip the * .gz you downloaded, untar and run install-bindist.sh

For * nux, to install Apache 2.0.42 with mod_sll installed, you will need to compile from source:

I used http://httpd.apache.org/docs-2.0/install.html as a reference.

$lynx http://www.apache.org/dist/httpd/httpd-2.0.42.tar.gz

$gzip-d httpd-2.0.42.tar.gz

$tar xvf httpd-2.0.42.tar

$. / configure-enable-mods-shared=most-enable-ssl=shared

$make

$make install

Then download mod_jk-2.0.42.so and put it into your modules directory and rename it mod_jk.so.

Windows: Execute the downloaded executable and install.

2.2 Configure the JK Module in httpd.conf

Edit the Apache server's configuration file httpd.conf which is located in the / usr/local/apache2/conf directory.

2.2.1 Below "# LoadModule foo_module modules/mod_foo.so", insert the following lines:

# # Load mod_jk#LoadModule jk_module modules/mod_jk.so## Configure mod_jk#JkWorkersFile conf/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel info

NOTE: You will need to change mod_jk.so to mod_jk.dll for Windows.

2.2.2 Below the "DocumentRoot" line, insert the following two lines:

JkMount / * .jsp loadbalancerJkMount / servlet/* loadbalancer2.3 Create the workers.properties file

Now we will create a file called worker.properties, and we will place it under / usr/local/apache2/conf. The worker.properties file tells Apache about the various Tomcat servers that are running, and on which port they are listening.

In my setup, I installed the two Tomcat servers in different directories, on the same machine as Apache. Feel free to put your Tomcat servers on different machines.

I made the first Tomcat server's AJP13 connector listen on port 11009 instead of the default port which is 8009, and the second one listens on port 12009.

I have decided to name my tomcat servers tomcat1 and tomcat2. This is purely my choice.

Create the file exactly like this:

# # workers.properties # # In Unix, we use forward slashes:ps=/# list the workers by nameworker.list=tomcat1, tomcat2 Loadbalancer#-# First tomcat server#-worker.tomcat1.port=11009worker.tomcat1.host=localhostworker.tomcat1.type=ajp13# Specify the size of the open connection cache.#worker.tomcat1.cachesize## Specifies the loadbalance factor when used with# a load balancing worker.# Note:#-> lbfactor must be > lbfactor must be- -> Low lbfactor means less work done by the worker.worker.tomcat1.lbfactor=100#-# Second tomcat server#-worker.tomcat2.port=12009worker.tomcat2.host=localhostworker.tomcat2.type=ajp13# Specify the size of the open connection cache.#worker.tomcat2.cachesize## Specifies the load balance factor when used with# a load balancing worker. # Note:#-> lbfactor must be > lbfactor must be-> Low lbfactor means less work done by the worker.worker.tomcat2.lbfactor=100#-# Load Balancer worker#-# # The loadbalancer (type lb) worker performs weighted round-robin# load balancing with sticky sessions.# Note:#-> If a worker dies The load balancer will check its state# once in a while. Until then all work is redirected to peer# worker.worker.loadbalancer.type=lbworker.loadbalancer.balanced_workers=tomcat1, tomcat2## END workers.properties#

That's it, we're done with Apache.

3. Install and Configure the Tomcat Servers

Now let's suppose that Java 1.4.x is installed under / usr/local/jdk1.4.x/. Create two Tomcat 4.x servers and install them under / usr/local/:

Tar fvxz jakarta-tomcat-4.x.tar.gz mv jakarta-tomcat-4.x / usr/local/tomcat1 cp-R / usr/local/tomcat1 / usr/local/tomcat2

In both / usr/local/tomcat1 and / usr/local/tomcat2, the same files will be modified. I here by present the modifications made to the files contained in the / usr/local/tomcat1 directory tree structure. You should also apply the same changes to the corresponding files located under the / usr/local/tomcat2 directory tree structure.

3.1 Modify catalina.sh

In my many years of consulting, I have learned not to rely on environment variables which can be unset by ignorant or malicious people. This is why I explicitely set the JAVA_HOME and CATALINA_HOME variables directly in the catalina.sh file.

At line 32, before the "#-Verify and Set Required Environment Variables" line, insert the following two lines:

JAVA_HOME=/usr/local/jdk1.4; export JAVA_HOME CATALINA_HOME=/usr/local/tomcat1; export CATALINA_HOME

(Set CATALINA_HOME to / usr/local/tomcat2 in / usr/local/tomcat2/conf/catalina.sh)

3.2 Modify conf/server.xml

3.2.1 Add a unique jvmRoute to the Catalina engine

Near line 100, replace:

With:

For tomcat2, put jvmRoute= "tomcat2".

3.2.2 Change the control port

At line 13, replace:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report