准备工作
192.168.220.123 TomcatA
192.168.220.126 TomcatB
192.168.220.126 Nginx
添加session共享配置(TomcatA和TomcatB 相同)
<Engine name="Catalina" defaultHost="localhost">
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;.*\.jsp"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<!-- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
-->
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
- 部署测试应用-Tomcat A
$TOMCAT_HOME/webapps/testapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>Tomcat A</title></head>
<body>
<h1><font color="blue">Tomcat A </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
- 部署测试应用-Tomcat B
$TOMCAT_HOME/webapps/testapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
- 在测试应用中的web.xml 加节点
路径: $TOMCAT_HOME/webapps/testapp/WEB-INF/web.xml
<distributable /> - 配置Nginx
upstream site {
server 192.168.220.126:9099;
server 192.168.220.123:9099;
}
server {
listen 9999;
server_name 192.168.220.126;
location / {
root html;
index index.html index.htm;
proxy_pass http://site;
-
测试
启动tomcat A、tomcat B、Nginx
通过Nginx访问index.jsp,不断刷页面,Tomcat 已换,但Session ID没变。
总结:看文档时一头雾水,觉得很难,实践起来却很简单,真像文档中所有 给出的基本配置满足大多数人的工作要求。
虽花了很多时间,印象深刻,下次继续这种模式。(理解原理-实现-理解原理)