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.gztar 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/jdkPATH=$JAVA_HOME/bin:$PATHexport 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.gztar 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/tomcatPATH=$CATALINA_BASE/bin:$PATHexport 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/jdkCATALINA_HOME=/usr/local/tomcatexport JAVA_HOME CATALINA_HOMEcase $1 instart) 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/tomcatchkconfig --add tomcat

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

On 19.66:

Vim/usr/local/tomcat/conf/server. xml hosted> <Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "TomcatA"> // specifies that this is the TomcatA 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.*" %>

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.*" %>

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.gztar xf tomcat-connectors-1.2.41-src.tar.gzcd tomcat-connectors-1.2.41-src/native/./configure --with-apxs=/usr/sbin/apxsmake && make install

3. Provide additional httpd module configuration files:

Vim/etc/httpd/conf. d/httpd-jk.conf --------------------------------------------------> # Load the mod_jkLoadModule jk_module modules/mod_jk.so // Load mod_jk.so module JkWorkersFile/etc/httpd/conf. d/workers. properties // the configuration file JkLogFile logs/mod_jk.logJkLogLevel debugJkMount/* 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, stat1worker. tomcatA. type = ajp13 // The worker mode used by httpd to connect to tomcat. tomcatA. host = 192.168.19.66worker.TomcatA.port = 8009worker. tomcatA. lbfactor = 1 // weight worker. tomcatB. type = ajp13worker. tomcatB. host = 192.168.19.74worker.TomcatB.port = 8009worker. tomcatB. lbfactor = 1worker. lb1.type = lb // worker for scheduling. lb1.sticky _ session = 0 // whether to use session to bind a worker. lb1.balance _ workers = TomcatA, TomcatBworker. 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.bakvim httpd-proxy.conf route> <Proxy balancer: // lb1> BalancerMember http: // 192.168.19.66: 8080 loadfactor = 1 route = ATA ata BalancerMember http: // 192.168.19.74: 8080 loadfactor = 1 route = TomcatB ProxySet lbmethod = byrequests // schedule the Count 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.bakvim 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! If you have any questions, contact me at QQ: 82800452.

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.