Tomcat集群基础实现
一.apache部分
++在http.conf中加入 Include conf/mod_jk.conf
++在vhost中加入 Include conf/urimap.properties
//mod_jk
++# cat mod_jk.conf
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,”controller”为在workers.propertise里指定的负载分配控制器
#JkLogFile logs/mod_jk.log
#JkLogLevel debug
AddType application/x-jsp .jsp
++#cat workers.properties
worker.list = controller,tomcat1,tomcat2 #server 列表
#========tomcat1========
worker.tomcat1.port=8009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=9009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat
worker.controller.sticky_session=1
++#cat urimap.properties
#JkMount /* controller
JkMount /m/* controller
JkMount /manager/* controller
JkMount /*.jsp controller
JkMount /*.do controller
JkMount /*.action controller
//ajp
RewriteEngine On
ProxyPreserveHost On
RewriteRule ^/route/(.*.jsp)$ balancer://myCluster/$1 [P,L,NC]
#ProxyPass / balancer://myCluster/ [lbmethod=bytraffic]
#192.168.25.128
BalancerMember ajp://192.168.25.128:8009 loadfactor=1
BalancerMember ajp://192.168.25.129:8009 loadfactor=5
#loadfactor=1没有loadfactor=5分配的多
二.Tomcat部分
++如果tomcat都在同一台服务器上,需要修改一些端口,8005–>9005, 8080–>9080, 8009–>9009,8082–>9082
++编写一个测试jsp,放到两个tomcat的webapps下
#cat test.jsp
<%
System.out.println(”===========================”);
%>
Tomcat集群环境下的session复制
++修改各个tomcat的server.xml,将集群部分配置的在注释符删掉,如果tomcat在同一台机器,还需要将tomcat2的4001端口改为非4001端口
++在各个tomcat的server.xml中增加
#jvmRoute值与上面的配置要一致,tomcat1/tomcat2
++新建测试文件index.jsp
1. cat index.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.print(”<b>Session 列表</b>”);
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=”index.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>
2.然后在同级目录新建WEB-INF目录,并在WEB-INF下新建web.xml,内容如下
<web-app xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd” version=”2.4″>
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
3.重启各tomcat进行测试,应该在两个tomcat的日志中看到session复制