利用httpd对Tomcat进行负载均衡配置

实验系统:CentOS 6.6_x86_64

实验前提:提前准备好编译环境,防火墙和selinux都关闭

实验说明:本实验共有2台主机,IP分配如拓扑

实验软件:jdk-8u60-linux-x64  apache-tomcat-8.0.24  tomcat-connectors-1.2.41  httpd-2.2.15  httpd-devel-2.2.15

实验拓扑:

利用httpd对Tomcat进行负载均衡配置

一、两台机器安装Java环境

1.安装JAVA:

wget 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.配置环境变量:

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

二、两台机器安装tomcat

1.安装tomcat:

wget
tar xf apache-tomcat-8.0.24.tar.gz -C /usr/local/
cd /usr/local/
ln -sv apache-tomcat-8.0.24 tomcat

2.配置环境变量:

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.查看tomcat及JAVA状态:

catalina.sh version

利用httpd对Tomcat进行负载均衡配置

4.提供启动脚本:

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.编辑tomcat配置文件,只添加jvmRoute参数:

  在19.66上:

vim /usr/local/tomcat/conf/server.xml
----------------------------------------------->
<Engine defaultHost="localhost" jvmRoute="TomcatA">            //指明这是TomcatA主机

  在19.74上:

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

6.提供测试页面:

    在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

  在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

三、利用mod_jk模块对tomcat进行负载均衡

利用httpd反向代理tomcat时有两种方法,分别要用到mod_proxy和mod_jk这两个模块。mod_jk需要额外编译安装,而mod_proxy需要用到httpd的proxy_module、proxy_balancer_module、proxy_http_module、proxy_ajp_module模块,确保安装时这些模块都被安装了。从httpd2.2开始,这些模块默认就是安装的,所以这里我们直接用yum安装即可。

下面先演示利用mod_jk模块对tomcat进行负载均衡。此模块只需要在一台机器上安装,我们这里在19.66上安装。

1.安装httpd:

yum -y install httpd httpd-devel          //安装httpd-devel为了提供apxs

2.安装mod_jk.so模块:

wget
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.提供额外的httpd模块配置文件:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/94c6d301833011f2cf5aea42318e8e60.html