`
wangcheng
  • 浏览: 1454528 次
  • 性别: Icon_minigender_1
  • 来自: 青岛人在北京
社区版块
存档分类
最新评论

Tomcat 集群配置

    博客分类:
  • java
阅读更多
Apache + Tomcat 集群配置


一、 环境说明
Windows XP
apache_2.0.59-win32-x86-no_ssl.msi      http://httpd.apache.org/
mod_jk-apache-2.0.59.so                http://tomcat.apache.org/download-connectors.cgi
apache-tomcat-5.5.23.zip    http://tomcat.apache.org/download-55.cgi

我的程序分别安装到
D:\webserver\Apache Group\Apache2
D:\webserver\TomcatCluster\tomcat1
D:\webserver\TomcatCluster\tomcat2

Apache安装完后,在右下角状态栏中可以看到 Apache Service Monitor 可以控制Apache的状态。
验证Apache是否安装成功,可以访问http://localhost 如果能看到Apache的预制页面,说明安装成功,如果不行,可以访问http://localhost:8080 试试(可能因为IIS已经使用了80端口, 我的就是8080, 可以修改Apache的配置文件来修改)

二、 负载均衡
找到Apache安装目录下conf目录中的httpd.conf文件。
在文件最后添加一句:
include "D:\webserver\Apache Group\Apache2\conf\mod_jk.conf"

接着在conf目录中新建文件mod_jk.conf并添加下面的内容:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk-apache-2.0.59.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定哪些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /*.jsp controller

在conf目录下新建workers.properties文件并添加如下内容:
#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=11009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=12009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1

#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=1

(解释一下AJP13是 Apache JServ Protocol version 1.3)

将mod_jk-apache-2.0.59.so 复制到Apache的modules目录中。

接下来配置2个Tomcat
打开tomcat1\conf\ server.xml
将Server port 改为11005
<Server port="11005" shutdown="SHUTDOWN">

将Define Connector port改为11080
<Connector port="11080" maxHttpHeaderSize="8192"

将AJP13 Connector port改为11009
<Connector port="11009"  enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

打开tomcat2\conf\server.xml
将Server port 改为12005
<Server port="12005" shutdown="SHUTDOWN">

将Define Connector port改为12080
<Connector port="12080" maxHttpHeaderSize="8192"

将AJP13 Connector port改为12009
<Connector port="12009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

好了,现在建立一个测试程序
分别在两个Tomcat的webapps中建立test目录,并新建test.jsp文件,内容如下:
<%
System.out.println("===========");
%>

启动apache, tomcat1, tomcat2
访问http://localhost:8080/test/test.jsp (或者 http://localhost/test/test.jsp)不断刷新页面,可以在两个Tomcat的控制台中看到,交替输出"===========", 这样就实现了负载均衡。


三、 集群配置
集群除了负载均衡,另一个主要功能是Session Replication。
打开tomcat1\conf\ server.xml将<Cluster>部分的注释去掉。
再打开tomcat2\conf\ server.xml将<Cluster>部分的注释也去掉,并将<Cluster>中<Receiver>的tcpListenPort的值改为4002。以避免与Tomcat1冲突。

添加一个新的测试程序test2.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
  out.println("<br> ID " + session.getId()+"<br>");
  // 如果有新的 Session 属性设置
  String dataName = request.getParameter("dataName");
  if (dataName != null && dataName.length() > 0) {
     String dataValue = request.getParameter("dataValue");
     session.setAttribute(dataName, dataValue);
  }
  out.println("<b>Session 列表</b><br>");
  System.out.println("============================");
  Enumeration e = session.getAttributeNames();
  while (e.hasMoreElements()) {
     String name = (String)e.nextElement();
     String value = session.getAttribute(name).toString();
     out.println( name + " = " + value+"<br>");
         System.out.println( name + " = " + value);
   }
%>
  <form action="test2.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>

分别在2个tomcat的webapps\test中新建WEB-INF目录,在WEB-INF中添加web.xml内容如下:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
       <display-name>TomcatDemo</display-name>
       <distributable/>
</web-app>

主要是添加<distributable/>,distributable元素用来告诉servlet容器,程序将部署在分布式Web容器中。
重新启动tomcat1和tomcat2. 访问http://localhost:8080/test/test2.jsp (或http://localhost/test/test2.jsp)
随意添加key-value, 可以看到两个tomcat交替显示session中的值,各个tomcat的session是同步的。

再来修改tomcat1\conf\server.xml,找到
<Engine name="Catalina" defaultHost="localhost">
为其添加jvmRoute属性,值为apache的conf\workers.properties中配置的tomcat名字。
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

同样修改tomcat2\conf\server.xml的相同部分
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

jvmRoute是tomcat路由标示,由此区分两台tomcat主机。一次会话,就有一个sessionID,这个sessionID后面会跟上jvmRoute设置的值,这样一次会话,就只会让一个tomcat处理。

重新启动tomcat1, tomcat2
访问http://localhost:8080/test/test2.jsp 可以看到session.getId()的值在原session id后面多了jvmRoute的值。
ID 46A5843FF4A1E0A84338225AC02F6430.tomcat1
随意添加key-value,可以看到session信息只在tomcat1中输出。

再打开一个浏览器,并访问http://localhost:8080/test/test2.jsp 其session id可能变为
ID 11478E5BE5FE388E4845205B4133A30F.tomcat2
其值也只会在tomcat2中输出。

现在把tomcat1关闭,再次刷新访问tomcat1的那个浏览器,可以看到session信息输出到了tomcat2的控制台中,并且session信息仍然保留着。


参考文章
http://hi.baidu.com/luodaijun/blog/item/5bbe4cfb5ffef864034f56a1.html
http://www-128.ibm.com/developerworks/cn/java/l-jetspeed/
http://tomcat.apache.org/tomcat-5.0-doc/balancer-howto.html
http://tomcat.apache.org/tomcat-5.0-doc/cluster-howto.html
http://hi.baidu.com/abocai/blog/item/c18314f4d8e4a1ef7609d70f.html
http://hi.baidu.com/injava/blog/item/c8df882f136f21391e30896d.html

另外推荐一篇文章《Remotely monitor Tomcat clusters using MC4J》,使用JMX技术监测Tomcat集群。
中文名《用MC4J远程监测Tomcat集群-JMX技术助力Tomcat》
英文地址 http://www.javaworld.com/javaworld/jw-08-2005/jw-0801-jmx.html
中文地址 http://www.matrix.org.cn/resource/article/2005-09-01/Tomcat_MC4J_43706.html

  • tomcat集群配置.rar (107.1 KB)
  • 描述: tomcat集群配置.doc apache,tomcat1,tomcat2修改后的配置文件,及测试程序
  • 下载次数: 770
分享到:
评论
10 楼 putonyuer 2011-12-11  
好个屁  到处copy没一点技术含量
balance都写错了
9 楼 kaider_qiu110 2011-09-20  
   试过,不行地
8 楼 wangcheng 2007-09-17  
一般是Apache配的有问题
动态资源jsp,servlet由apache转发给tomcat处理
其他静态资源是由Apache处理的
我也遇到过类似情况,看下面
http://wangcheng.iteye.com/blog/103726
其中第3条可能是你需要的
7 楼 piaoyaohou 2007-09-17  
再补充一下:
apeche的地址172.20.56.75
tomcat1 :172.20.56.76
tomcat2 :172.20.56.77
6 楼 piaoyaohou 2007-09-17  
access.log
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/v-login.jsp HTTP/1.1" 200 3859
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/lookAndFeel/css/base.css HTTP/1.1" 404 325
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/lookAndFeel/css/component.css HTTP/1.1" 404 330
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/behavior/base.js HTTP/1.1" 404 317
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/component/tree/lookAndFeel/css/tree.css HTTP/1.1" 404 340
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/component/tree/behavior/tree.js HTTP/1.1" 404 332
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/component/grid/lookAndFeel/css/grid.css HTTP/1.1" 404 340
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/base/component/grid/behavior/grid.js HTTP/1.1" 404 332
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logonlookAndFeel/css//logon.css HTTP/1.1" 404 327
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/logon.js HTTP/1.1" 404 310
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/css/logon.css HTTP/1.1" 404 327
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/loginButton2.gif HTTP/1.1" 404 336
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/logon.js HTTP/1.1" 404 310
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/loginButton1.gif HTTP/1.1" 404 336
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/content_bg.gif HTTP/1.1" 404 334
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/header.jpg HTTP/1.1" 404 330
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/login_icon.gif HTTP/1.1" 404 334
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/password_icon.gif HTTP/1.1" 404 337
172.20.56.75 - - [17/Sep/2007:14:00:34 +0800] "GET /MyViki/logon/lookAndFeel/image/bottom.gif HTTP/1.1" 404 330
5 楼 piaoyaohou 2007-09-17  
mod_jk.log
[Mon Sep 17 14:00:27 2007] [2300:2880] [warn]  jk_map.c (379): The attribute 'worker.controller.balanced_workers' is deprecated - please check the documentation for the correct replacement.
[Mon Sep 17 14:00:27 2007] [3036:2024] [warn]  jk_map.c (379): The attribute 'worker.controller.balanced_workers' is deprecated - please check the documentation for the correct replacement.
[Mon Sep 17 14:00:27 2007] [3036:2024] [warn]  jk_map.c (379): The attribute 'worker.controller.balanced_workers' is deprecated - please check the documentation for the correct replacement.
4 楼 piaoyaohou 2007-09-17  
错误日志我帖出来
error.log
[Mon Sep 17 14:00:27 2007] [notice] Parent: Received restart signal -- Restarting the server.
[Mon Sep 17 14:00:27 2007] [notice] Child 3744: Exit event signaled. Child process is ending.
[Mon Sep 17 14:00:27 2007] [notice] Apache/2.0.59 (Win32) mod_jk/1.2.21 configured -- resuming normal operations
[Mon Sep 17 14:00:27 2007] [notice] Server built: Jul 27 2006 15:55:03
[Mon Sep 17 14:00:27 2007] [notice] Parent: Created child process 3036
[Mon Sep 17 14:00:27 2007] [notice] Child 3036: Child process is running
[Mon Sep 17 14:00:28 2007] [notice] Child 3744: Released the start mutex
[Mon Sep 17 14:00:28 2007] [notice] Child 3036: Acquired the start mutex.
[Mon Sep 17 14:00:28 2007] [notice] Child 3036: Starting 250 worker threads.
[Mon Sep 17 14:00:29 2007] [notice] Child 3744: Waiting for 250 worker threads to exit.
[Mon Sep 17 14:00:29 2007] [notice] Child 3744: All worker threads have exited.
[Mon Sep 17 14:00:29 2007] [notice] Child 3744: Child process is exiting
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
[Mon Sep 17 14:00:34 2007] [error] [client 172.20.56.75] File does not exist: D:/Apache Group/Apache2/htdocs/MyViki, referer: http://172.20.56.75/MyViki/logon/v-login.jsp
3 楼 piaoyaohou 2007-09-17  
我按照您的写法做的,怎么网站的图片地址访问不到啊?
我在三台机器上进行测试的,一台运行apeche,另两台运行tomcat,通过apeche 的机器访问网站地址
2 楼 cowboy811004 2007-09-12  
好东西,获益了!
1 楼 wlghd 2007-07-11  
这么好的东西怎么没人顶啊?过段时间试下!

相关推荐

Global site tag (gtag.js) - Google Analytics