专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » tomcat负载均衡:实战中整理出tomcat集群和负载均衡 »正文

tomcat负载均衡:实战中整理出tomcat集群和负载均衡

来源: 发布时间:星期五, 2008年12月19日 浏览:52次 评论:0
    ()环境介绍说明     (1)服务器有4台台安装apache 3台安装tomcat

    (2)apache2.0.55、tomcat5.5.15、jk2.0.4、jdk1.5.6或jdk1.4.2

    (3)ip配置台安装apacheip为192.168.0.88 3台安装tomcat服务器ip分别为192.168.0.1/2/4

    ( 2)安装过程

    (1)在 3台要安装tomcat服务器上先安装jdk

    (2)配置jdk安装路径在环境变量path中加入jdkbin路径新建环境变量JAVA_HOME指向jdk安装路径

    (3)在 3台要安装tomcat服务器上分别安装tomcat调试 3个tomcat到能够正常启动

    (4)tomcat默认WEB服务端口是8080默认模式是单独服务 3个tomcatWEB服务端口修改为7080/8888/9999

v修改位置为tomcat安装目录下conf/server.xml

    修改前配置为

<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />



    修改后配置为

<Connector port="7080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />



    依次修改每个tomcat监听端口(7080/8888/9999)

    (5)分别测试每个tomcat启动是否正常

http://192.168.0.1:7080
http://192.168.0.2:8888
http://192.168.0.4:9999



    ( 3)负载均衡配置过程     (1)在那台要安装apache服务器上安装apache2.0.55安装路径为默认C:\Program Files\Apache Group\Apache2

    (2)安装后测试能否正常启动调试到能够正常启动http://192.168.0.88

    (3)下载jk2.0.4后解压缩文件

    (4)将解压缩后目录中modules目录中mod_jk2.so文件复制到apache安装目录下modules目录中为C:\Program Files\Apache Group\Apache2\modules

    (5)修改apache安装目录中conf目录配置文件httpd.conf在文件中加LoadModule模块配置信息最后加上句LoadModule jk2_module modules/mod_jk2.so

    (6)分别修改 3个tomcat配置文件conf/server.xml修改内容如下修改前

<!-- An Engine represents the entry po (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers d with the request, and passes them
on to the appropriate Host (virtual host). -->

<!-- You should jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
-->

<!-- Define the top level container in our container hierarchy -->
<Engine name="Catalina" defaultHost="localhost">



    修改后

<!-- An Engine represents the entry po (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers d with the request, and passes them
on to the appropriate Host (virtual host). -->

<!-- You should jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">


<!-- Define the top level container in our container hierarchy
<Engine name="Catalina" defaultHost="localhost">
-->



    将其中jvmRoute="jvm1"分别修改为jvmRoute="tomcat1"和jvmRoute="tomcat2"和jvmRoute="tomcat3"

    (7)然后重启 3个tomcat调试能够正常启动

    (8)在apache安装目录中conf目录下创建文件workers2.propertie写入文件内容如下

# fine the communication channel
[channel.:192.168.0.1:8009]
info=Ajp13 forwarding over



    配置第个服务器tomcatId=tomcat1 #要和tomcat配置文件server.xml中jvmRoute="tomcat1"名称致debug=0 lb_factor=1 #负载平衡因子数字越大请求被分配几率越高

# Define the communication channel
[channel.:192.168.0.2:8009]
info=Ajp13 forwarding over
tomcatId=tomcat2
debug=0
lb_factor=1

# Define the communication channel
[channel.:192.168.0.4:8009]
info=Ajp13 forwarding over
tomcatId=tomcat3
debug=0
lb_factor=1

[status:]
info=Status worker, displays runtime information.

[uri:/jkstatus.jsp]
info=Display status information and checks the config file for changes.
group=status:

[uri:/*]
info=Map the whole webapp
debug=0



    (9)在 3个tomcat安装目录中webapps建立相同应用我和应用目录名为TomcatDemo在 3个应用目录中建立相同 WEB-INF目录和页面index.jspindex.jsp页面内容如下

<%@ page contentType="text/html; char=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.prln(request.getLocalAddr + " : " + request.getLocalPort+"<br>");%>
<%
out.prln("<br> ID " + session.getId+"<br>");

// 如果有新 Session 属性设置
String dataName = request.getParameter("dataName");
(dataName != null && dataName.length > 0) {
String dataValue = request.getParameter("dataValue");
session.Attribute(dataName, dataValue);
}

out.pr("<b>Session 列表</b>");

Enumeration e = session.getAttributeNames;
while (e.hasMoreElements) {
String name = (String)e.nextElement;
String value = session.getAttribute(name).toString;
out.prln( name + " = " + value+"<br>");
.out.prln( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>



    (10)重启apache服务器和 3个tomcat服务器到此负载 均衡已配置完成测试负载均衡先测试apache访问http://192.168.0.88/jkstatus.jsp能否正常访问并查询其中内容有 3个tomcat相关配置信息和负载介绍说明访问http://192.168.0.88/TomcatDemo/index.jsp看能够运行能运行则已建立负载均衡

    ( 4)tomcat集群配置

    (1)负载均衡配置条件下配置tomcat集群

    (2)分别修改 3个tomcat配置文件conf/server.xml修改内容如下修改前

<!--
<Cluster Name="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notyListenersOnReplication="true">

<Membership
Name="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>

<Receiver
Name="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>

<Sender
Name="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="5000"/>

<Valve Name="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.g;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

<Deployer Name="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>

<ClusterListener Name="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
-->



    修改后

<!-- mody by whh -->
<Cluster Name="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notyListenersOnReplication="true">

<Membership
Name="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>

<Receiver
Name="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>

<Sender
Name="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="5000"/>

<Valve Name="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.g;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

<Deployer Name="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>

<ClusterListener Name="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
<!-- mody by whh -->



    将集群配置选项注释放开即可如上

    (3)重启 3个tomcat.到此tomcat集群已配置完成

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: