一 安装
下载和安装 JRE 1.5 最新版 (1.5.07)
下载和安装 Tomcat 最新版 (5.5.23),假定安装目录为 D:\Tomcat5.5
D:\Tomcat5.5 目录下的全部内容复制到 D:\Tomcat5.5.2
二 启动脚本
1 D:\Tomcat5.5\bin\startup.bat
"D:\Program\Java\jdk1.5.0_07\bin\java.exe" -jar .\bootstrap.jar -Dcatalina.home="D:\Tomcat5.5" -Dcatalina.base="D:\Tomcat5.5" -Djava.endorsed.dirs="D:\Tomcat5.5\common\endorsed" -Djava.io.tmpdir="D:\Tomcat5.5\temp" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="D:\Tomcat5.5\conf\logging.properties" start
2 D:\Tomcat5.5.2\bin\startup.bat
"D:\Program\Java\jdk1.5.0_07\bin\java.exe" -jar .\bootstrap.jar -Dcatalina.home="D:\Tomcat5.5.2" -Dcatalina.base="D:\Tomcat5.5.2" -Djava.endorsed.dirs="D:\Tomcat5.5\common\endorsed" -Djava.io.tmpdir="D:\Tomcat5.5.2\temp" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="D:\Tomcat5.5.2\conf\logging.properties" start
三 修改server.xml
1 两台服务器将<Engine name="Catalina" defaultHost="localhost">注视掉并改为
<!--Engine name="Catalina" defaultHost="localhost"-->
2 把 Server/Service/Engine/Host 下的 Cluster 结点前后的注释符(<!-- 和 -->)删除;
3 端口更改
修改新实例的服务器配置文件 (D:\Tomcat5.5.2\conf\server.xml),把其中的 Server/Service/Connector 的端口更改,以避免和第一个冲突(8005 -> 9005 8080 -> 9080 8009 -> 9009 8443 -> 9443)
修改 Cluster/Receiver 结点的 tcpListenPort 端口(此端口用于 Session 复制),以避免和第一个冲突(4001 -> 4002)
注:Tomcat Cluster 默认使用多播来建立成员关系,即两个实例的多播的地址和端口一样,就可以互相认为是同一个 Cluster 的成员
四 默认tomcat访问路径为D:\Tomcat5.5\webapps\ROOT\index.jsp
修改D:\Tomcat5.5\webapps\ROOT\WEB-INF\web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"; you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlnssi="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/>
<!-- JSPC servlet mappings start -->
<!-- JSPC servlet mappings end -->
</web-app>
另外一台服务器配置一样
五 修改D:\Tomcat5.5\webapps\ROOT\index.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.util.*"%>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info: <%out.print(request.getLocalAddr() + " : " + request.getLocalPort());%>
<%
out.println("<br> ID " + session.getId());
// 如果有新的 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);
}
%>
<form action="index.jsp" method="OST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
六 启动两台Tomcat服务器,分别双击
D:\Tomcat5.5\bin\startup.bat
D:\Tomcat5.5.2\bin\startup.bat
七 访问并测试
在 IE 中打开第一个实例中的 TomcatDemo (http://localhost:8080/),在 Form 里随便设置一个 Session 属性,如 MyName = foo
将 IE 地址栏中的地址修改为第二个实例中的 TomcatDemo(http://localhost:9080/),刷新,就可以看到注意到 SessionID 不变, MyName 的属性值已有了