续二:Apache 2.0.48 + Tomcat 5.0.18 + Load Balancing (Solaris 9 x86)
Tomcat 5的安装及与Apache的整合请看我的这里
http://bbs.chinaunix.net/forum/6/20040214/259794.html
http://bbs.chinaunix.net/forum/6/20040214/263220.html
I. 先安装第二个Tomcat,把已装好的复制一份就可以了
代码:
# cp -Rp /path/to/tomcat1 /path/to/tomcat2
II. 设置第二个Tomcat的启动脚本
如果用jsvc,修改CATALINA_HOME变量等于/path/to/tomcat2
代码:
# cd /etc/init.d
# cp tomcat tomcat2
# vi tomcat2
# cd ../rc3.d
# ln ../init.d/tomcat2 S99tomcat2
# cd ../rc0.d
# ln ../init.d/tomcat2 K03tomcat2
如果用自带的startup.sh,则修改bin/catalina.sh,在开始处加上
代码:
CATALINA_HOME=/path/to/tomcat2; export CATALINA_HOME
III. 修改两个Tomcat的配置文件server.xml
代码:
要修改的内容 Control Port
修改前 <Server port="8005"
修改后tomcat1 <Server port="11005"
修改后tomcat2 <Server port="12005"
要修改的内容 AJP13 Port
修改前 <Connector port="8009"
修改后tomcat1 <Connector port="11009"
修改后tomcat2 <Connector port="12009"
要修改的内容 Http Port
修改前 <Connector port="8080"
修改后tomcat1 <Connector port="11080"
修改后tomcat2 <Connector port="12080"
要修改的内容 Set jvmRoute
修改前 <Engine name="Catalina" defaultHost="localhost" debug=0">
修改后tomcat1 <Engine jvmRoute="tomcat1" name="Catalina" defaultHost="localhost" debug=0">
修改后tomcat2 <Engine jvmRoute="tomcat2" name="Catalina" defaultHost="localhost" debug=0">
IV. 配置workers2.proerties
代码:
#---- worker2.properties
[logger.apache2]
level=DEBUG
# Shared memory handling. Needs to be set.
[shm]
file=/var/apache/logs/shm.file
size=1048576
[channel.socket:localhost:11009]
tomcatId=localhost:11009
port=11009
host=127.0.0.1
lb_factor=1
group=lb_1
disabled=0
[channel.socket:localhost:12009]
tomcatId=localhost:12009
port=12009
host=127.0.0.1
lb_factor=1
group=lb_1
disabled=0
[status:status]
[lb:lb]
info=Default load balancer
debug=1
[lb:lb_1]
info=Second load balancer
debug=0
# Uri mapping
[uri:/jkstatus/*]
group=status:status
[uri:/*]
group=lb:lb_1
#---- end of workers2.properties
V. 创建两个测试用的jsp文件
/path/to/tomcat1/webapps/ROOT/test.jsp
代码:
<html>
body bgcolor="red"
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 1</h1>
</body>
</html>
/path/to/tomcat2/webapps/ROOT/test.jsp
代码:
<html>
body bgcolor="blue"
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 2</h1>
</body>
</html>
VI. 测试
启动tomcat1、tomcat2和apache
代码:
# /etc/init.d/tomcat start
# /etc/init.d/tomcat2 start
# /etc/init.d/apache start
用浏览器测试一下 http://yourserverip/test.jsp
红色页面说明访问的是tomcat1
蓝色的是tomcat2
VII. Links
http://raibledesigns.com/tomcat/
实现session的复制
I. 修改server.xml
取消server.xml文件中对下面这段代码的注释,还要修改tcpListenPort的值,注意不能冲突,可以设成4001和4002
代码:
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*.gif;.*.js;.*.jpg;.*.htm;.*.html;.*.txt;"/>
</Cluster>
II. 修改web.xml
修改演示Servlet的Web应用的web.xml文件,添加<distributable/>元素。
代码:
......
<display-name>Servlet 2.4 Examples</display-name>
<description>
Servlet 2.4 Examples.
</description>
<distributable/>
......
III. 测试
访问url: http://serverip/servlets-examples/servlet/SessionExample
1. 关闭tomcat1和tomcat2;
2. 启动tomcat1
3. 在浏览器中输入属性名tomcat1和属性值tomcat1再提交,返回的页面显示session中有刚刚输入的tomcat1属性;
4. 启动tomcat2;
5. 过一会后(等待tomcat2和tomcat1通信并复制信息)关闭tomcat1;
6. 在浏览器中输入属性名tomcat2和属性值tomcat2再提交,返回的页面显示session中有刚刚输入的tomcat2属性,还有先前输入的tomcat1属性;
7. 启动tomcat1;
8. 过一会后(等待tomcat2和tomcat1通信并复制信息)关闭tomcat2;
9. 在浏览器中输入属性名tomcat11和属性值tomcat11再提交,返回的页面显示session中有刚刚输入的tomcat11属性,还有先前输入的tomcat1和tomcat2属性;
IV. 参考文档
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/cluster-howto.html
http://www-900.ibm.com/developerWorks/cn/java/l-jetspeed/