当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

实例讲解配置之——TOMCAT集群配置

我的运行环境:Windows2003 Server SP4 + J2SDK1.5.0 +Tomcat5.5.9

准备软件:Tomcat 5.5.9   JDK1.5.0

一.配置过程

1、安装JDK1.5.0。采用默认安装就可以。

2、安装tomcat到C:\ tomcat 50,采用完全安装,该程序用于实现负载均衡功能。

3、将tomcat50的内容进行完全复制,生成C:\ tomcat 51、C:\ tomcat 52、C:\ tomcat 53,分别用做集群中的节点。

4、修改负载均衡规则,使其遵循轮循算法(RoundRobin)。

4.1将testLB.jsp复制到c:\tomcat50\webapps\balancer文件夹中

4.2将文件夹classes复制到c:\tomcat50\webapps\balancer\WEB-INF文件夹中

4.3修改c:\web\tomcat50\webapps\balancer\WEB-INF\web.xml文件如下:
  
    BalancerFilter
    /LoadBalancer
 

5、在集群中每个节点下,部署clusterapp应用。Clusterapp包含sessiondata.jsp,test.jsp,脚本。test.jsp是用来验证节点状态的页面;sessiondata.jsp是用来响应用户所发送的请求,同时记录会话ID,会话的起始和最后时间,提供用户增加、修改、删除会话的属性字段和属性值,可以通过此来判断会话的持续与否。

6、将log4j的log4j-1.2.9.jar复制到每个%tomcat%/common/ lib下,使其将会话日志统一存储到指定的文件中。

7、修改每个tomcat的server.xml配置文件,参数如下表所示。

配置              Instance 1    Instance 2       Instance 3    Instance 4
Instance Type    Load Balancer    Node 1       Node 2             Node 3
Code name             TC-LB             TC01       TC02             TC03
Home Directory    c:/tomcat50    c:/tomcat51      c:/tomcat52    c:/tomcat53
Server Port    8005          9005      10005            11005
Connector             8080         9080               10080            11080
JK2 AJP Connector    8009         9009               10009            11009
Cluster mcastAddr    228.0.0.4         228.0.0.4    228.0.0.4           228.0.0.4
Cluster mcastPort    45564        45564              45564           45564
tcpListenAddress    127.0.0.1        127.0.0.1    127.0.0.1           127.0.0.1
Cluster tcpListenPort 4000        4001             4002           4003

8、修改c:\web\tomcat50\webapps\balancer\WEB-INF\config\ruler.xml文件如下:


   
            serverInstance="1"
        maxServerInstances="3"
        tcpListenAddress="127.0.0.1"
        tcpListenPort="4001"
        testWebPage="http://localhost:9080/clusterapp/test.jsp"
        redirectUrl="http://localhost:9080/clusterapp/sessiondata.jsp" />
            serverInstance="2"
        maxServerInstances="3"
        tcpListenAddress="127.0.0.1"
        tcpListenPort="4002"
        testWebPage="http://localhost:10080/clusterapp/test.jsp"
    redirectUrl="http://localhost:10080/clusterapp/sessiondata.jsp" />

            serverInstance="3"
        maxServerInstances="3"
        tcpListenAddress="127.0.0.1"
        tcpListenPort="4003"
        testWebPage="http://localhost:11080/clusterapp/test.jsp"
    redirectUrl="http://localhost:11080/clusterapp/sessiondata.jsp" />

   
            redirectUrl="http://localhost:8080/balancer/testLB.jsp" />

9、启动tomcat的应用。手动双击每个tomcat/bin的startup.bat程序。

二.Web请求集群环境下流程

1、启动访问起始页(http://localhost:8080/balancer/testLB.jsp)


2、JSP重定向请求到负载均衡过滤文件(http://localhost:8080/balancer/LoadBalancer)

3、负载均衡的tomcat接受请求,根据制定的负载均衡算法,重定向到可用的集群节点(TC01、TC02、TC03)

4、对应集群中节点的sessiondata.jsp(位于clusterapp应用下)页面将启动。

5、sesiondata.jsp将在web上显示会话的详细信息(如会话ID,最后访问时间)
在测试的过程中采用RoundRobin算法,通过对Instance 1发起多个http://localhost:8080/balancer/testLB.jsp测试请求,发现每个请求返回页面的端口号不完全一致,在9080、10080、11080端口不规则的出现,即客户端的访问请求按照RoundRobin算法被重定向到不同的服务器上进行处理,说明该负载均衡规则在这个测试中得到正确的体现。在关闭集群中一个节点后再启用它,能够自动发现该节点,并为该节点分配请求。

相关内容
赞助商链接