`
wangcheng
  • 浏览: 1466721 次
  • 性别: 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  
这么好的东西怎么没人顶啊?过段时间试下!

相关推荐

    tomcat集群配置

    【标题】:“Tomcat集群配置” 在Web应用的高可用性和可扩展性需求日益增长的今天,Tomcat集群成为了一个重要的解决方案。Tomcat集群能够通过负载均衡和故障转移来提高服务的稳定性和性能。以下是对Tomcat集群配置...

    apache+tomcat集群配置

    Apache + Tomcat 集群配置 Apache + Tomcat 集群配置是指将 Apache 服务器与 Tomcat 服务器集成,以提高服务器的性能和可扩展性。本文将详细介绍如何配置 Apache + Tomcat 集群,包括安装 Apache 和 Tomcat 服务器...

    Apache+Tomcat集群配置

    Apache+Tomcat集群配置,Apache+Tomcat集群配置,Apache+Tomcat集群配置

    tomcat集群配置 程序以及文档

    当我们需要处理高并发、负载均衡或提高应用可用性时,单个Tomcat实例可能无法满足需求,这时就需要进行Tomcat集群配置。本资料包包含了“tomcat集群配置”的程序及文档,提供了现成的例子,帮助我们理解和实践Tomcat...

    tomcat集群配置文件

    当我们谈论“tomcat集群配置文件”时,我们指的是将多个Tomcat实例组织成一个集群,以提高应用程序的可用性和可伸缩性。在集群环境中,负载均衡、故障转移和资源共享是关键特性。 首先,要配置Tomcat集群,你需要...

    tomcat集群配置和配置软件

    **二、Tomcat集群配置步骤** 1. **安装多个Tomcat实例**:首先,你需要在不同的服务器上或同一服务器的不同端口上安装并启动多个Tomcat实例。 2. **配置共享目录**:为确保集群内的节点共享session信息,需要设置...

    Apache+Tomcat集群配置续

    Apache+Tomcat集群配置续Apache+Tomcat集群配置续Apache+Tomcat集群配置续Apache+Tomcat集群配置续

    nginx + tomcat 集群配置

    nginx + tomcat 集群配置 负载 nginx 下载地址: http://nginx.org/en/download.html 联合tomcat 实现集群 windows: Nginx.conf 配置 //见文件夹下面的nginx.conf 更改tomcat的配置: 第一处端口...

    Apache和Tomcat集群配置步骤(Apache2.2,Tomcat6.0).

    ### Apache和Tomcat集群配置详解 #### 一、软件准备 在进行Apache和Tomcat集群配置之前,首先需要准备所需的软件资源。 - **Apache 2.2**: - Windows平台: 从官方网站 [http://httpd.apache.org/download.cgi]...

    tomcat集群配置v2

    ### Tomcat集群配置详解 #### 1. 配置方案 在描述中提到了三种配置方案: 1. **使用Apache2.2中的mod_ajp集成Tomcat5.5**:这种方案通过Apache的mod_ajp模块,使Apache能够与Tomcat通过AJP协议通信,从而实现请求...

    Apache + Tomcat集群配置详解

    以下是对Apache与Tomcat集群配置的详细解析: 1. **Apache HTTP Server配置**: - Apache通常作为前端代理服务器,接收来自客户端的HTTP请求,并通过AJP协议将这些请求转发给Tomcat实例。 - 在`httpd.conf`配置...

    redis+tomcat集群配置session共享

    本篇文章将详细探讨如何在Redis+Tomcat集群配置中实现Session共享。 首先,我们需要理解什么是Session。Session是Web应用中用于存储用户状态的一种机制,通常用于保存用户的登录信息、购物车等数据。在单台服务器...

    apache+tomcat集群配置64位windows系统

    **二、Tomcat集群配置** 1. **安装Tomcat**:下载并安装两个Tomcat实例,分别监听不同的端口(如上述8080和8081)。 2. **复制共享session配置**:由于集群中的各个Tomcat实例需要共享session,所以需要在每个...

    Nginx与Tomcat集群配置 Redis配置

    以下将详细介绍Nginx与Tomcat集群配置以及Redis配置的相关知识点。 ### Nginx的编译与安装 1. **安装编译环境**:在安装Nginx前,需要确保系统已经安装了必要的编译工具,如GCC、Make等。同时,可能还需要安装pcre...

Global site tag (gtag.js) - Google Analytics