参考:http://www.iteye.com/topic/1017961
第一步、下载apache
http://httpd.apache.org/download.cgi
选择某一版本,我下载的是: httpd-2.2.22-win32-x86-no_ssl.msi
一路next,安装完成后,http://localhost 测试是否成功
第二步、下载tomcat
我选择的是tomcat7版本 zip压缩包。解压复制两份。一个为tomcat1,一个为tomcat2
第三步、下载jk
http://apache.etoak.com//tomcat/tomcat-connectors/jk/binaries/
下载和apache一致的版本,我这里因为apache是2.2.22,所以下载
tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip 解压后,把mod_jk.so放到apache安装目录下的modules下。
第四步、配置apache的httpd.conf
打开apache安装目录下conf/httpd.conf,在最后一行增加:Include conf/mod_jk.conf
然后再conf目录下新建mod_jk.conf
mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkMount /*.jsp controller
workers.properties
#server controller对应mod_jk.conf中controller
worker.list = controller
#========tomcat1========
worker.tomcat1.port=9009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=8009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#controller 负载均衡处理器
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1
第五步、修改tomcat1,tomcat2中server.xml配置文件
1)修改端口号:8080改为8081 <Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
connectionTimeout="20000"
redirectPort="8443" />
2)修改AJP1.3端口号:8009改为9009
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" /> 9009对应workers.properties中9009
3)修改Engine :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> tomcat1对应workers.properties中tomcat1
4)去掉Cluster的注释,打开集群(个人理解)
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
同理:tomcat2配置同上,你可以自定义端口号。也可以不用修改用默认的端口号。
第六步、测试
在tomcat1,tomcat2的webapps新建目录test,在test中新建一个test2.jsp内容如下
第七步、启动apache,tomcat1,tomcat2。把webapps/ROOT的WEB-INF目录复制到test目录中。修改web.xml,增加
<distributable/>
<%@ 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>
输入http://localhost/test/test2.jsp
可实现session的复制。