tomcat集群和负载均衡的实现(session同步
(一)环境说明
(1)服务器有4台,一台安装apache,三台安装tomcat
(2)apache2.0.55、tomcat5.5.15、jk2.0.4、jdk1.5.6或jdk1.4.2
(3)ip配置,一台安装apache的ip为192.168.0.88,三台安装tomcat的服务器ip分别为192.168.0.1/2/4
(二)安装过程
(1)在三台要安装tomcat的服务器上先安装jdk
(2)配置jdk的安装路径,在环境变量path中加入jdk的bin路径,新建环境变量JAVA_HOME指向jdk的安装路径
(3)在三台要安装tomcat的服务器上分别安装tomcat,调试三个tomcat到能够正常启动
(4)tomcat的默认WEB服务端口是8080,默认的模式是单独服务,我的三个tomcat的WEB服务端口修改为7080/8888/9999
修改位置为tomcat的安装目录下的conf/server.xml
修改前的配置为
<Connectorport="8080"maxHttpHeaderSize="8192"
maxThreads="150"minSpareThreads="25"maxSpareThreads="75"
enableLookups="false"redirectPort="8443"acceptCount="100"
connectionTimeout="20000"disableUploadTimeout="true"/>
修改后的配置为
<Connectorport="7080"maxHttpHeaderSize="8192"
maxThreads="150"minSpareThreads="25"maxSpareThreads="75"
enableLookups="false"redirectPort="8443"acceptCount="100"
connectionTimeout="20000"disableUploadTimeout="true"/>
依次修改每个tomcat的监听端口(7080/8888/9999)
(5)分别测试每个tomcat的启动是否正常
http://192.168.0.1:7080
http://192.168.0.2:8888
http://192.168.0.4:9999
(三)负载均衡配置过程
(1)在那台要安装apache的服务器上安装apache2.0.55,我的安装路径为默认C:\ProgramFiles\ApacheGroup\Apache2
(2)安装后测试apache能否正常启动,调试到能够正常启动http://192.168.0.88
(3)下载jk2.0.4后解压缩文件
(4)将解压缩后的目录中的modules目录中的mod_jk2.so文件复制到apache的安装目录下的modules目录中,我的为C:\ProgramFiles\ApacheGroup\Apache2\modules
(5)修改apache的安装目录中的conf目录的配置文件httpd.conf,在文件中加LoadModule模块配置信息的最后加上一句LoadModulejk2_modulemodules/mod_jk2.so
(6)分别修改三个tomcat的配置文件conf/server.xml,修改内容如下
修改前
<!--AnEnginerepresentstheentrypoint(withinCatalina)thatprocesses
everyrequest.TheEngineimplementationforTomcatstandalone
analyzestheHTTPheadersincludedwiththerequest,andpassesthem
ontotheappropriateHost(virtualhost).-->
<!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:
<Enginename="Standalone"defaultHost="localhost"jvmRoute="jvm1">
-->
<!--Definethetoplevelcontainerinourcontainerhierarchy-->
<Enginename="Catalina"defaultHost="localhost">
修改后
<!--AnEnginerepresentstheentrypoint(withinCatalina)thatprocesses
everyrequest.TheEngineimplementationforTomcatstandalone
analyzestheHTTPheadersincludedwiththerequest,andpassesthem
ontotheappropriateHost(virtualhost).-->
<!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:-->
<Enginename="Standalone"defaultHost="localhost"jvmRoute="tomcat1">
<!--Definethetoplevelcontainerinourcontainerhierarchy
<Enginename="Catalina"defaultHost="localhost">
-->
将其中的jvmRoute="jvm1"分别修改为jvmRoute="tomcat1"和jvmRoute="tomcat2"和jvmRoute="tomcat3"
(7)然后重启三个tomcat,调试能够正常启动。
(8)在apache的安装目录中的conf目录下创建文件workers2.propertie,写入文件内容如下
#finethecommunicationchannel
[channel.socket:192.168.0.1:8009]
info=Ajp13forwardingoversocket
#配置第一个服务器
tomcatId=tomcat1#要和tomcat的配置文件server.xml中的jvmRoute="tomcat1"名称一致
debug=0
lb_factor=1#负载平衡因子,数字越大请求被分配的几率越高
#Definethecommunicationchannel
[channel.socket:192.168.0.2:8009]
info=Ajp13forwardingoversocket
tomcatId=tomcat2
debug=0
lb_factor=1
#Definethecommunicationchannel
[channel.socket:192.168.0.4:8009]
info=Ajp13forwardingoversocket
tomcatId=tomcat3
debug=0
lb_factor=1
[status:]
info=Statusworker,displaysruntimeinformation.
[uri:/jkstatus.jsp]
info=Displaystatusinformationandcheckstheconfigfileforchanges.
group=status:
[uri:/*]
info=Mapthewholewebapp
debug=0
(9)在三个tomcat的安装目录中的webapps建立相同的应用,我和应用目录名为TomcatDemo,在三个应用目录中建立相同WEB-INF目录和页面index.jsp,index.jsp的页面内容如下
<%@pagecontentType="text/html;charset=GBK"%>
<%@pageimport="java.util.*"%>
<html><head><title>ClusterAppTest</title></head>
<body>
ServerInfo:
<%
out.println(request.getLocalAddr()+":"+request.getLocalPort()+"<br>");%>
<%
out.println("<br>ID"+session.getId()+"<br>");
//如果有新的Session属性设置
StringdataName=request.getParameter("dataName");
if(dataName!=null&&dataName.length()>0){
StringdataValue=request.getParameter("dataValue");
session.setAttribute(dataName,dataValue);
}
out.print("<b>Session列表</b>");
Enumeratione=session.getAttributeNames();
while(e.hasMoreElements()){
Stringname=(String)e.nextElement();
Stringvalue=session.getAttribute(name).toString();
out.println(name+"="+value+"<br>");
System.out.println(name+"="+value);
}
%>
<formaction="index.jsp"method="POST">
名称:<inputtype=textsize=20name="dataName">
<br>
值:<inputtype=textsize=20name="dataValue">
<br>
<inputtype=submit>
</form>
</body>
</html>
(10)重启apache服务器和三个tomcat服务器,到此负载均衡已配置完成。测试负载均衡先测试apache,访问http://192.168.0.88/jkstatus.jsp
能否正常访问,并查询其中的内容,有三个tomcat的相关配置信息和负载说明,访问http://192.168.0.88/TomcatDemo/index.jsp看能够运行,
能运行,则已建立负载均衡。
(四)tomcat集群配置
(1)负载均衡配置的条件下配置tomcat集群
(2)分别修改三个tomcat的配置文件conf/server.xml,修改内容如下
修改前
<!--
<Clusterclassname="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassname="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
classname="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
classname="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
classname="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="5000"/>
<Valveclassname="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployerclassname="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassname="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
-->
修改后
<!--modifybywhh-->
<Clusterclassname="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassname="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
classname="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
classname="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
classname="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="5000"/>
<Valveclassname="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployerclassname="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListenerclassname="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
<!--modifybywhh-->
将集群配置选项的注释放开即可,如上。
(3)重启三个tomcat。到此tomcat的集群已配置完成。
(五)应用配置
对于要进行负载和集群的的tomcat目录下的webapps中的应用中的WEB-INF中的web.xml文件要添加如下一句配置
<distributable/>
配置前
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4">
<display-name>TomcatDemo</display-name>
</web-app>
配置后
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4">
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
tomcat集群和负载均衡的实现(session同步)补充
因为tomcat的session同步功能需要用到组播,windows默认情况下是开通组播服务的,但是linux默认情况下并没有开通,可以通过指令打开routeadd-net224.0.0.0netmask240.0.0.0deveth0,如果需要服务器启动时即开通组播需在/etc/sysconfig/static-routes文件内加入eht0net224.0.0.0netmask240.0.0.0。具体组播概念请查阅CCNP相关内容。