Use httpd to configure Server Load balancer for Tomcat

Source: Internet
Author: User
Tags apache tomcat

Use httpd to configure Server Load balancer for Tomcat

Experimental System: CentOS 6.6 _ x86_64

Prerequisites: Prepare the compiling environment in advance, and disable both firewall and selinux.

Tutorial Description: There are two hosts in this experiment, and IP addresses are allocated as topology.

Experimental software: jdk-8u60-linux-x64 apache-tomcat-8.0.24 tomcat-connectors-1.2.41 httpd-2.2.15 httpd-devel-2.2.15

Tutorial topology:

I. Install the JAVA environment on two machines

1. install JAVA:

Wget http://download.Oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz
Tar xf jdk-8u60-linux-x64.tar.gz-C/usr/local/
Cd/usr/local/
Ln-sv jdk1.8.0 _ 60 jdk

2. Configure environment variables:

Vim/etc/profile. d/jdk. sh
--------------------------------------------------->
JAVA_HOME =/usr/local/jdk
PATH = $ JAVA_HOME/bin: $ PATH
Export JAVA_HOME PATH
<---------------------------------------------------
./Etc/profile. d/jdk. sh

2. install tomcat on two machines

1. install tomcat:

Wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.24/bin/apache-tomcat-8.0.24.tar.gz
Tar xf apache-tomcat-8.0.24.tar.gz-C/usr/local/
Cd/usr/local/
Ln-sv apache-tomcat-8.0.24 tomcat

2. Configure environment variables:

Vim/etc/profile. d/tomcat. sh
--------------------------------------------------->
CATALINA_BASE =/usr/local/tomcat
PATH = $ CATALINA_BASE/bin: $ PATH
Export PATH CATALINA_BASE
<---------------------------------------------------
./Etc/profile. d/tomcat. sh

3. View tomcat and JAVA status:

Catalina. sh version

4. Provide the startup script:

Vim/etc/init. d/tomcat
-------------------------------------------------------->
#! /Bin/sh
# Tomcat init script for Linux.
#
# Chkconfig: 2345 96 14
# Description: The Apache Tomcat servlet/JSP container.
# JAVA_OPTS = '-Xms64m-xmx128m'
JAVA_HOME =/usr/local/jdk
CATALINA_HOME =/usr/local/tomcat
Export JAVA_HOME CATALINA_HOME

Case $1 in
Start)
Exec $ CATALINA_HOME/bin/catalina. sh start ;;
Stop)
Exec $ CATALINA_HOME/bin/catalina. sh stop ;;
Restart)
$ CATALINA_HOME/bin/catalina. sh stop
Sleep 2
Exec $ CATALINA_HOME/bin/catalina. sh start ;;
*)
Echo "Usage: 'basename $ 0' {start | stop | restart }"
Exit 1
;;
Esac
<--------------------------------------------------------
Chmod + x/etc/init. d/tomcat
Chkconfig -- add tomcat

5. Edit the tomcat configuration file and add only the jvmRoute parameter:

On 19.66:

Vim/usr/local/tomcat/conf/server. xml
----------------------------------------------->
<Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "TomcatA"> // specifies that this is a ata host.

On 19.74:

Vim/usr/local/tomcat/conf/server. xml
----------------------------------------------->
<Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "TomcatB">

6. Provide the test page:

On 19.66:

Mkdir-pv/usr/local/tomcat/webapps/test/WEB-INF/{classes, lib}
Vim/usr/local/tomcat/webapps/test/index. jsp
------------------------------------------------------------------------>
<% @ Page language = "java" %>
<% @ Page import = "java. util. *" %>
<Html>
<Head>
<Title> test </title>
</Head>
<Body>
<%
Out. println ("This is TomcatA ");
%>
</Body>
</Html>
------------------------------------------------------------------------>
Service tomcat start

On 19.74:

Mkdir-pv/usr/local/tomcat/webapps/test/WEB-INF/{classes, lib}
Vim/usr/local/tomcat/webapps/test/index. jsp
------------------------------------------------------------------------>
<% @ Page language = "java" %>
<% @ Page import = "java. util. *" %>
<Html>
<Head>
<Title> test </title>
</Head>
<Body>
<%
Out. println ("This is TomcatB ");
%>
</Body>
</Html>
------------------------------------------------------------------------>
Service tomcat start

Iii. Load Balancing tomcat using the mod_jk Module

There are two ways to use httpd reverse proxy tomcat: mod_proxy and mod_jk. Mod_jk requires additional compilation and installation, while mod_proxy requires httpd's proxy_module, proxy_balancer_module, proxy_http_module, and proxy_ajp_module to ensure that these modules are installed during installation. From httpd2.2, these modules are installed by default, so we can install them directly using yum.

The following describes how to use the mod_jk module to load balance tomcat. This module only needs to be installed on one machine. Here we will install it on 19.66.

1. Install httpd:

Yum-y install httpd-devel // install httpd-devel to provide apxs

2. Install the mod_jk.so module:

Wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
Tar xf tomcat-connectors-1.2.41-src.tar.gz
Cd tomcat-connectors-1.2.41-src/native/
./Configure -- with-apxs =/usr/sbin/apxs
Make & make install

3. Provide additional httpd module configuration files:

Vim/etc/httpd/conf. d/httpd-jk.conf
------------------------------------------------------>
# Load the mod_jk
LoadModule jk_module modules/mod_jk.so // load the mod_jk.so Module
JkWorkersFile/etc/httpd/conf. d/workers. properties // configuration file used by this module
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount/* lb1 // All contents under the Access root are sent to the cluster lb1.
JkMount/status/stat1

4. Configure the configuration file workers. properties of the mod_jk module:

Vim/etc/httpd/conf. d/workers. properties
-------------------------------------------------------->
Worker. list = lb1, stat1
Worker. TomcatA. type = ajp13 // The mode used by httpd to connect to tomcat
Worker. TomcatA. host = 192.168.19.66
Worker. TomcatA. port = 8009
Worker. TomcatA. lbfactor = 1 // weight
Worker. TomcatB. type = ajp13
Worker. TomcatB. host = 192.168.19.74
Worker. TomcatB. port = 8009
Worker. TomcatB. lbfactor = 1
Worker. lb1.type = lb // Scheduling Method
Worker. lb1.sticky _ session = 0 // whether to use session binding
Worker. lb1.balance _ workers = TomcatA, TomcatB
Worker. stat1.type = status

5. Start the test:

Service httpd start

Open http: // 192.168.19.66/test in the browser. Refresh the page to see the experiment results.

4. Load Balancing tomcat using the mod_proxy module

1. You can still edit it on 19.66 using the http protocol:

Cd/etc/httpd/conf. d/
Mv httpd-jk.conf httpd-jk.conf.bak
Vim httpd-proxy.conf.
-------------------------------------------------------------->
<Proxy balancer: // lb1>
BalancerMember http: // 192.168.19.66: 8080 loadfactor = 1 route = TomcatA
BalancerMember http: // 192.168.19.74: 8080 loadfactor = 1 route = TomcatB
ProxySet lbmethod = byrequests // schedule statistics on the number of requests based on Weights
</Proxy>

ProxyPass/balancer: // lb1/
ProxyPassReverse/balancer: // lb1/
<--------------------------------------------------------------
Service httpd restart

2. Use the ajp protocol:

Cd/etc/httpd/conf. d/
Mv httpd-jk.conf httpd-jk.conf.bak
Vim httpd-proxy.conf.
-------------------------------------------------------------->
<Proxy balancer: // lb1>
BalancerMember ajp: // 192.168.19.66: 8009 loadfactor = 1 route = TomcatA
BalancerMember ajp: // 192.168.19.74: 8009 loadfactor = 1 route = TomcatB
ProxySet lbmethod = byrequests
</Proxy>

ProxyPass/balancer: // lb1/
ProxyPassReverse/balancer: // lb1/
<--------------------------------------------------------------
Service httpd restart

The above two methods can achieve load balancing, refresh the page can still see the effect, here is not to give the picture. Now, all labs are finished. Thank you!

For more Tomcat tutorials, see the following:

Install and configure the Tomcat environment in CentOS 6.6

Install JDK + Tomcat in RedHat Linux 5.5 and deploy Java Projects

Tomcat authoritative guide (second edition) (Chinese/English hd pdf + bookmarks)

Tomcat Security Configuration and Performance Optimization

How to Use Xshell to view Tomcat real-time logs with Chinese garbled characters in Linux

Install JDK and Tomcat in CentOS 64-bit and set the Tomcat Startup Procedure

Install Tomcat in CentOS 6.5

Tomcat details: click here
Tomcat: click here

This article permanently updates the link address:

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.