转载

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

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

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 name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">             //指明这是TomcatA主机

在19.74上:

vim /usr/local/tomcat/conf/server.xml -----------------------------------------------> <Engine name="Catalina" 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 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.提供额外的httpd模块配置文件:

vim /etc/httpd/conf.d/httpd-jk.conf ------------------------------------------------------> # Load the mod_jk LoadModule  jk_module  modules/mod_jk.so                //加载mod_jk.so模块 JkWorkersFile  /etc/httpd/conf.d/workers.properties     //此模块自己要用到的配置文件 JkLogFile  logs/mod_jk.log JkLogLevel  debug JkMount  /*  lb1                                        //访问根下的所有内容都发到集群lb1中来 JkMount  /status/  stat1

4.配置mod_jk模块的配置文件workers.properties:

vim /etc/httpd/conf.d/workers.properties --------------------------------------------------------> worker.list = lb1,stat1 worker.TomcatA.type = ajp13                        //httpd连接tomcat所用的模式 worker.TomcatA.host = 192.168.19.66 worker.TomcatA.port = 8009 worker.TomcatA.lbfactor = 1                        //权重 worker.TomcatB.type = ajp13 worker.TomcatB.host = 192.168.19.74 worker.TomcatB.port = 8009 worker.TomcatB.lbfactor = 1 worker.lb1.type = lb                              //调度方式 worker.lb1.sticky_session = 0                     //是否使用session绑定 worker.lb1.balance_workers = TomcatA, TomcatB worker.stat1.type = status

5.启动测试:

service httpd start

用浏览器打开http://192.168.19.66/test,我们不断刷新,可以看到实验效果。

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

四、利用mod_proxy模块对tomcat进行负载均衡

1.仍然在19.66上编辑即可,使用http协议:

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     //基于权重将统计请求个数进行调度 </Proxy> ProxyPass / balancer://lb1/ ProxyPassReverse / balancer://lb1/ <-------------------------------------------------------------- service httpd restart 

2.使用ajp协议:

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 

以上两种办法都可以实现负载均衡,刷新页面仍然可以看到效果,这里就不在给出图片了。至此,实验全部结束,谢谢!如有问题,请联系我,QQ:82800452.

正文到此结束
Loading...