Apache + Tomcat + Load Balancing

发表于:2007-06-08来源:作者:点击数: 标签:
续二:Apache2.0.48+Tomcat5.0.18+LoadBalancing(Solaris9x86) Tomcat5的安装及与Apache的整合请看我的这里 [b:9ce24cc1ca][url]http://bbs.chinaunix.net/forum/6/20040214/259794.html[/url][/b:9ce24cc1ca] [b:9ce24cc1ca][url]http://bbs.chinaunix.net/

续二:Apache 2.0.48 + Tomcat 5.0.18 + Load Balancing (Solaris 9 x86)

Tomcat 5的安装及与Apache的整合请看我的这里
[b:9ce24clearcase/" target="_blank" >cc1ca][url]http://bbs.chinaunix.net/forum/6/20040214/259794.html[/url][/b:9ce24cc1ca]
[b:9ce24cc1ca][url]http://bbs.chinaunix.net/forum/6/20040214/263220.html[/url][/b:9ce24cc1ca]

I. 先安装第二个Tomcat,把已装好的复制一份就可以了
[code:1:9ce24cc1ca]
# cp -Rp /path/to/tomcat1 /path/to/tomcat2
[/code:1:9ce24cc1ca]

II. 设置第二个Tomcat的启动脚本

如果用jsvc,修改CATALINA_HOME变量等于/path/to/tomcat2
[code:1:9ce24cc1ca]
# 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
[/code:1:9ce24cc1ca]

如果用自带的startup.sh,则修改bin/catalina.sh,在开始处加上
[code:1:9ce24cc1ca]
CATALINA_HOME=/path/to/tomcat2; export CATALINA_HOME
[/code:1:9ce24cc1ca]

III. 修改两个Tomcat的配置文件server.xml

[code:1:9ce24cc1ca]
要修改的内容  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"> 
[/code:1:9ce24cc1ca]

IV. 配置workers2.proerties

[code:1:9ce24cc1ca]
#---- 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

[/code:1:9ce24cc1ca]

V. 创建两个测试用的jsp文件
/path/to/tomcat1/webapps/ROOT/test.jsp
[code:1:9ce24cc1ca]

<html>
<body bgcolor="red">
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 1</h1>
</body>
</html>

[/code:1:9ce24cc1ca]

/path/to/tomcat2/webapps/ROOT/test.jsp
[code:1:9ce24cc1ca]

<html>
<body bgcolor="blue">
<center>
<%= request.getSession().getId() %>
<h1>Tomcat 2</h1>
</body>
</html>

[/code:1:9ce24cc1ca]

VI. 测试
启动tomcat1、tomcat2和apache
[code:1:9ce24cc1ca]
# /etc/init.d/tomcat start
# /etc/init.d/tomcat2 start
# /etc/init.d/apache start
[/code:1:9ce24cc1ca]
用浏览器测试一下 http://yourserverip/test.jsp
红色页面说明访问的是tomcat1
蓝色的是tomcat2

VII. Links
[url]http://raibledesigns.com/tomcat/[/url]

 wolfg 回复于:2004-02-20 17:03:53
实现session的复制

I. 修改server.xml
取消server.xml文件中对下面这段代码的注释,还要修改tcpListenPort的值,注意不能冲突,可以设成4001和4002
[code:1:ad428a7856]
<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>
[/code:1:ad428a7856]

II. 修改web.xml
修改演示Servlet的Web应用的web.xml文件,添加<distributable/>元素。
[code:1:ad428a7856]
......

<display-name>Servlet 2.4 Examples</display-name>
    <description>
      Servlet 2.4 Examples.
    </description>
    
    <distributable/>
    
    ......
[/code:1:ad428a7856]


III. 测试

访问url: http://serverip/servlets-examples/servlet/SessionExample 
[list=1:ad428a7856]
[*:ad428a7856]关闭tomcat1和tomcat2; 
[*:ad428a7856]启动tomcat1 
[*:ad428a7856]在浏览器中输入属性名tomcat1和属性值tomcat1再提交,返回的页面显示session中有刚刚输入的tomcat1属性; 
[*:ad428a7856]启动tomcat2; 
[*:ad428a7856]过一会后(等待tomcat2和tomcat1通信并复制信息)关闭tomcat1; 
[*:ad428a7856]在浏览器中输入属性名tomcat2和属性值tomcat2再提交,返回的页面显示session中有刚刚输入的tomcat2属性,还有先前输入的tomcat1属性; 
[*:ad428a7856]启动tomcat1; 
[*:ad428a7856]过一会后(等待tomcat2和tomcat1通信并复制信息)关闭tomcat2; 
[*:ad428a7856]在浏览器中输入属性名tomcat11和属性值tomcat11再提交,返回的页面显示session中有刚刚输入的tomcat11属性,还有先前输入的tomcat1和tomcat2属性; 
[/list:o:ad428a7856]


IV. 参考文档

[url]http://jakarta.apache.org/tomcat/tomcat-5.0-doc/cluster-howto.html[/url]
[url]http://www-900.ibm.com/developerWorks/cn/java/l-jetspeed/[/url]

 C.Arthur 回复于:2004-02-20 17:18:30
好文章:)

 我爱臭豆腐 回复于:2004-02-20 21:25:02
好文章。等我找一个机器学习学习:)

 kpflysky 回复于:2004-02-23 13:44:30
谢谢

原文转自:http://www.ltesting.net