Apache2.2 Tomcat7 集群 session共享
下载:
Apache2.2、apache-tomcat-7.0.42.zip、tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip【mod_jk】
分别解压缩Apache2.2、apache-tomcat-7.0.42.zip、tomcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip
此处:C:\Apache2.2、C:\tomcat\tomcat71、C:\tomcat\tomcat72
copy从comcat-connectors-1.2.37-windows-i386-httpd-2.2.x.zip解压缩后的文件mod_jk.so至C:\Apache2.2\modules中。
在C:\Apache2.2\conf下新建文件workers.properties、内容如下:
#server 列表
worker.list = controller,tomcat1,tomcat2
#========tomcat1========
worker.tomcat1.port=8009 #注意这是 ajp1.3 端口号,在tomcat下server.xml配置,默认8009。不是tomcat的web端口
worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=9009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat
#session复制模式
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#sticky模式,即黏性会话模式
#worker.controller.sticky_session=1
在C:\Apache2.2\conf下新建文件mod_jk.conf、内容如下:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /*.jsp controller
修改C:\Apache2.2\conf\httpd.conf,在最后一行,添加:
include "C:\Apache2.2\conf\mod_jk.conf"
此时,apache修改完毕;
接下来修改tomcat、修改tomcat1的C:\tomcat\tomcat71\conf\server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> ... <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
修改tomcat2的server.xml文件
<Server port="9005" shutdown="SHUTDOWN"> 。。。 <Connector port="9090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 。。。 <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" /> 。。。 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> 。。。 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
主要是修改端口,使其同tomcat1的端口避免有冲突。
创建test文件夹,在其下新建test.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>"); // if have new Session attr set String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.println("<b>Session list</b><br>"); System.out.println("=============tomcat 1==============="); 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="test.jsp" method="POST"> name:<input type=text size=20 name="dataName"> <br> value:<input type=text size=20 name="dataValue"> <br> <input type=submit> </form> </body> </html>
将test文件夹分别copy至C:\tomcat\tomcat71\webapps、C:\tomcat\tomcat72\webapps下。
分别启动或重启tomcat1、tomcat2、apache、无先后顺序。
浏览器输入:
http://localhost/test/test.jsp
观察效果。
Done.
-----------------
Apache2.2 Tomcat7 集群 session共享 [windows]
原文:http://www.cnblogs.com/syc001/p/3232343.html