更改apache下的httpd.conf
先把listen 80改成listen 81或者别的,大家都知道,80这个端口经常被占用的。include "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\mod_jk.conf"之后 在conf目录下再建立一个mod_jk.conf,写上
LoadModule jk_module modules/mod_jk.so JkWorkersFile conf/workers.properties #指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名 JkMount /*.jsp controller如果看完了我之前的那个博客,mod_jk的内容就很容易理解了。
#server worker.list = controller #========tomcat1======== worker.tomcat1.port=18009 worker.tomcat1.host=localhost worker.tomcat1.type=ajp13 worker.tomcat1.lbfactor = 1 #========tomcat2======== worker.tomcat2.port=28009 worker.tomcat2.host=localhost worker.tomcat2.type=ajp13 worker.tomcat2.lbfactor = 1 #========tomcat3======== worker.tomcat3.port=38009 worker.tomcat3.host=localhost worker.tomcat3.type=ajp13 worker.tomcat3.lbfactor = 1 #========controller,负载均衡控制器======== worker.controller.type=lb worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3 worker.controller.sticky_session=false worker.controller.sticky_session_force=1 #worker.controller.sticky_session=1OK搞定
lbfactor是干什么的?
如果tomcat1,tomcat2,tomcat3的lbfactor分别是2,5,4那么所有的请求中2/11会分配给tomcat1处理,5/11会给tomcat2....
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ....... <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> ....... <Server port="8005" shutdown="SHUTDOWN">一般情况下,我们改动的都是8080,也就是支持http1.1协议的这端口。
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
mytest.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="mytest.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>然后我们访问
以上为charyle的理解。具体评述见http://www.iteye.com/topic/1017961
LoadModule jk_module modules/mod_jk.so JkWorkersFile conf/workers.properties # 设置虚拟主机,定义端口为81 <VirtualHost 127.0.0.1:81> ServerName 127.0.0.1 DocumentRoot "E:/cluster/tomcat1/webapps" #定义站点项目所在路径,把路径指向 tomcat 中的默认网站目录 DirectoryIndex index.html index.htm index.jsp ErrorLog logs/shsc-error_log.txt #指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名 JkMount /*.jsp controller </VirtualHost> # 设置虚拟主机,定义端口为81 <VirtualHost 127.0.0.1:81> ServerName 127.0.0.1 #定义服务名称 E:/apache-tomcat-6.0.44-8500/webapps 是我的tomcat的位置 DocumentRoot "E:/cluster/tomcat1/webapps" #定义站点项目所在路径,把路径指向 tomcat 中的默认网站目录 DirectoryIndex index.html index.htm index.jsp ErrorLog logs/shsc-error_log.txt #指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名 JkMount /*.jsp controller </VirtualHost> # 设置虚拟主机,定义端口为81 <VirtualHost 127.0.0.1:81> ServerName 127.0.0.1 #定义服务名称 E:/apache-tomcat-6.0.44-8500/webapps 是我的tomcat的位置 DocumentRoot "E:/cluster/tomcat1/webapps" #定义站点项目所在路径,把路径指向 tomcat 中的默认网站目录 DirectoryIndex index.html index.htm index.jsp ErrorLog logs/shsc-error_log.txt #指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名 JkMount /*.jsp controller </VirtualHost>