1.通过apache的官网下载httpd-2.2.22-win32-x86-no_ssl.msi和apache-tomcat-6.0.35-windows-x86.zip以及tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip
2.安装apache
3.解压缩tomcat,并且复制为两份,其中一份需要变更其中的server.xml文件中的端口,把8005,,8080,8009该为8006,8081,8010
4.把tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip解压缩,把其中的mod_jk.so拷贝的apache的modules目录下
5.在apache的conf目录下,修改httpd.conf文件,在文件的最后一行增加Include conf/mod_jk.conf
6.在apache的conf目录下,增加mod_jk.conf文件
#加入jk模块
LoadModule jk_module modules/mod_jk.so
#加载jk配置文件
JkWorkersFile "C:\\Apache2.2\\conf\\workers.properties"
#加载jk日志文件
JkLogFile "C:\\Apache2.2\\logs\\mod_jk.log"
#设置jk日志级别
JkLogLevel info
#设置jk日志格式
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
#loadbalancerJkMount /status mystatus
JkMount /examples/* loadbalancer
JkMount /status mystatus
7.在apache的conf目录下,增加workers.properties
worker.list=loadbalancer
worker.server1.type=ajp13
worker.server1.host=192.168.1.13
worker.server1.port=8009
worker.server1.lbfactor=1
worker.server1.socket_keepalive=1
worker.server1.socket_timeout=300
worker.server2.type=ajp13
worker.server2.host=192.168.1.13
worker.server2.port=8010
worker.server2.lbfactor=1
worker.server2.socket_keepalive=1
worker.server2.socket_timeout=300
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=server1,server2
worker.loadbalancer.sticky_session=false
worker.loadbalancer.sticky_session_force=true
worker.list=mystatus
worker.mystatus.type=status
注意:之前的8009和8010端口是tomcat的ajp监听器
8.修改tomcat的server.xml文件
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<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="auto"
port="5000"
selectorTimeout="100"
maxThreads="6"/>
<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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<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.ClusterSessionListener"/>
</Cluster>
9.启动apache和tomcat,访问apache的url,http://localhost:80/(注意:apache默认是80端口,可以通过修改apache的httpd.conf目录中的端口号)
10.在tomcat的webapps\examples\jsp的目录下新建一个session.jsp页面
<%@page contentType="text/html"%>
<HTML>
<HEAD><TITLE>Cluster -Ha JSP Sample </TITLE></HEAD>
<BODY>
<LI>serverd From Server: <b><%=request.getServerName()%></LI>
<LI>serverd Port Number: <b><%=request.getServerPort()%></LI>
<LI>serverd From Server: <b><%=java.net.InetAddress.getLocalHost().getHostName()%></LI>
<LI>serverd IP Address: <b><%=java.net.InetAddress.getLocalHost().getHostAddress()%></LI>
<LI>Sesion ID: <b><%=session.getId()%></LI>
<h3>Tomcat1</h3>
</BODY>
</HTML>
注意L其中的Tomcat1设置两个tomcat中为不同值,以区别连个tomcat之间的切换操作
11.修改webapps\examples\WEB-INF下的web.xml文件,增加 <distributable/>属性,(需要重启tomcat)
<display-name>Servlet and JSP Examples</display-name>
<distributable/>
12.通过apache文件该应用,可以看到Tomcat1和Tocmat2之间的切换,但是sessionid是不变的,url:http://localhost:80/examples/jsp/session.jsp
13.如果把步骤11的<distributable/>给去掉,重启tomcat,访问url:http://localhost:80/examples/jsp/session.jsp,可以看到是Tomcat1和Tocmat2之间的切换,sessionid是变化的