首页 > 代码库 > CentOS-6.4-minimal版中Apache-2.2.29与Tomcat-6.0.41实现负载均衡

CentOS-6.4-minimal版中Apache-2.2.29与Tomcat-6.0.41实现负载均衡

/**
 * CentOS-6.4-minimal版中Apache-2.2.29与Tomcat-6.0.41实现负载均衡
 * ----------------------------------------------------------------------------------------------------------------------
 * 关于负载均衡(Load Balancer)与集群(Cluster)的区别,以及配置方式的不同,稍后更新
 * ----------------------------------------------------------------------------------------------------------------------
 * 配置负载均衡
 * 本文建立在Apache-2.2.29与Tomcat-6.0.41整合的基础上,整合过程详见http://blog.csdn.net/jadyer/article/details/39613523
 * 1)修改端口(由于我是在一台机器上复制多个tomcat,所以需要修改端口,如果是不同的机器就可以跳过这一步)
 *   [root@CentOS64 app]# vi tomcat/conf/server.xml  (修改8005为-1,原因详见http://blog.csdn.net/jadyer/article/details/39613771)
 *   [root@CentOS64 app]# cp -r tomcat tomcat1
 *   [root@CentOS64 app]# cp -r tomcat tomcat2
 *   [root@CentOS64 app]# cp -r tomcat tomcat3
 *   [root@CentOS64 app]# vi tomcat1/conf/server.xml (修改ssl端口对ajp端口为8543和8109,即分别+100)
 *   [root@CentOS64 app]# vi tomcat2/conf/server.xml (修改ssl端口对ajp端口为8643和8209,即分别+100)
 *   [root@CentOS64 app]# vi tomcat3/conf/server.xml (修改ssl端口对ajp端口为8743和8309,即分别+100)
 * 2)修改/app/apache/conf/workers.properties,修改后的内容如下
 *   worker.list=status,tomcatlb
 *   worker.status.type=status
 *   worker.tomcat1.port=8109
 *   worker.tomcat1.host=127.0.0.1
 *   worker.tomcat1.type=ajp13
 *   worker.tomcat1.lbfactor=1
 *   worker.tomcat1.connection_pool_timeout=750
 *   worker.tomcat1.socket_timeout=300000
 *   worker.tomcat1.connect_timeout=10000
 *   worker.tomcat1.reply_timeout=330000
 *   worker.tomcat2.port=8209
 *   worker.tomcat2.host=127.0.0.1
 *   worker.tomcat2.type=ajp13
 *   worker.tomcat2.lbfactor=1
 *   worker.tomcat2.connection_pool_timeout=750
 *   worker.tomcat2.socket_timeout=300000
 *   worker.tomcat2.connect_timeout=10000
 *   worker.tomcat2.reply_timeout=330000
 *   worker.tomcat3.port=8309
 *   worker.tomcat3.host=127.0.0.1
 *   worker.tomcat3.type=ajp13
 *   worker.tomcat3.lbfactor=1                                 #负载权重,值越高,被分发请求的概率越大
 *   worker.tomcat3.connection_pool_timeout=750                #在连接池中维护的非活动连接,持续多少秒后被释放,其默认值为0,即不释放
 *   worker.tomcat3.socket_timeout=300000                      #JK与Web服务器连接超时的秒数,超出此秒数将产生一个错误并再次重试,其默认值为0,即JK会一直等下去
 *   worker.tomcat3.connect_timeout=10000                      #发送请求后,直到获取到应答期间的延迟毫秒数,其默认值为0,即一直等
 *   worker.tomcatlb.type=lb                                   #可选值ajp13,ajp14,jni,lb or status
 *   worker.retries=6                                          #通迅失败的的重式次数,默认为2
 *   worker.tomcatlb.balanced_workers=tomcat1,tomcat2,tomcat3  #参与负载均衡的Web服务器
 *   worker.tomcatlb.sticky_session=1
 * 3)修改/app/apache/conf/extra/httpd-vhosts.conf
 *   将默认的tomcat改为tomcatlb,即指定所有请求交由tomcatlb处理
 * ----------------------------------------------------------------------------------------------------------------------
 * 测试负载均衡
 * 1)启动apache和三个tomcat后,就可以通过jkstatus看到参与负载均衡的3个tomcat以及其它参数,我们也可以通过jkstatus修改负载均衡参数
 *   关于jkstatus的配置和使用,详见http://blog.csdn.net/jadyer/article/details/39613553
 * 2)接下来测试一下负载均衡访问,测试代码已在下方贴出
 *   访问页面我们会发现,每次刷新页面,通过后台日志都可以看到请求是被随机分配给3个tomcat的,说明负载均衡成功
 *   并且,由于这里并没有配置集群,故每次刷新页面时,页面打印的SessionID都是变化的
 * ----------------------------------------------------------------------------------------------------------------------
 * 关于workers.properties的更多参数说明,可参考以下两个网址
 * http://tomcat.apache.org/connectors-doc/reference/workers.html
 * http://blog.csdn.net/chumeng411/article/details/7541767
 * ----------------------------------------------------------------------------------------------------------------------
 * @create Sep 27, 2014 6:29:49 PM
 * @author 玄玉<http://blog.csdn.net/jadyer>
 */


<%@ page language="java" pageEncoding="UTF-8"%>

<%
System.out.println("本机地址:" + request.getLocalAddr() + ":" + request.getLocalPort());
out.println("本机地址:" + request.getLocalAddr() + ":" + request.getLocalPort() + "<br>");
%>

<%
out.println("<br>Session ID : " + session.getId() + "<br>");
session.setAttribute("myname", "session");
String dataName = request.getParameter("dataName");
if(null!=dataName && dataName.length()>0){
	String dataValue = http://www.mamicode.com/request.getParameter("dataValue");>
jkstatus效果图

CentOS-6.4-minimal版中Apache-2.2.29与Tomcat-6.0.41实现负载均衡