由于集团开始实施个人任务管理模块,内部业务支撑平台的使用人数直线上升,每个月的月初在线人数达到顶峰,原有的一个Tomcat已经胜任不了,俺决定通过负载均衡来解决。
之前没搞过负载均衡,于是乎,在各大搜索网站搜索文档无数。从原理着手,先理解一下原理,有助于接下来的实施,然后再找找有哪些技术和方案,最后决定用Apache+Tomcat配置负载均衡来解决
接下来就开始在虚拟机上搞实验,俺的是笔记本,开两个虚拟机,卡得我想吐。喝了半怀咖啡,鼠标才给我动两下,伤不起呀。果断关掉一个虚拟机,决定在一个虚拟机内开两个Tomcat来测试:
1、首先需要下载相关的文件:
Apache: httpd-2.2.17-win32-x86-no_ssl.msi
Tomcat: apache-tomcat-6.0.35-windows-x86.zip
模块 : tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip
JDK : jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe
2、安装JDK文件,配置JDK环境
3、安装Apache服务器,按照网上查的资料,需要配置以下文件
修改http.conf,在文件最后加上下面一句话就可以了:include "安装目录\Apache Software Foundation\Apache2.2\conf\mod_jk.conf"
复制mod_jk.so到安装目录\Apache Software Foundation\Apache2.2\modules
http.conf同目录下新建mod_jk.conf文件,内容如下:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /*.jsp controller
在http.conf同目录下新建 workers.properties文件,内容如下
#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=11009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=12009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========tomcat3========
#worker.tomcat3.port=13009
#worker.tomcat3.host=192.168.0.80 //在我的虚拟机中的,可以算远程的吧 #worker.tomcat3.type=ajp13
#worker.tomcat3.lbfactor = 1
#========controller,负载均衡控制器========
worker.controller.type=lb
#worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3 worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false worker.controller.sticky_session_force=1 #worker.controller.sticky_session=1
4、安装Tomcat,并配置如下:
修改SERVER.XML文件
1)、<Connector port="11009" protocol="AJP/1.3" redirectPort="8443" />
2)、 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
3)、<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<!--
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="8"/> -->
<!-- -->
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="localhost"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<!-- timeout="60000"-->
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
编写测试页面test2.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.println("<b>Session 列表</b><br>");
System.out.println("============================");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test2.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
修改项目下面的web.xml,加入<distributable/>
测试成功,Session同步复制