Apache+tomcat配置集群(1)

参考文档:http://www.iteye.com/topic/1017961

Apache+tomcat配置集群http://www.blogjava.net/pengo/archive/2011/04/15/348331.html

以下是个人搭建的tomcat集群

1:软件环境

 HTTP Server:apache http server 2.2

 Tomcat:apache-tomcat-7.0.55-windows-x64

 JK:tomcat-connectors-1.2.40-windows-i386-httpd-2.2.x(jk只是其中的一种连接方式,还有http_proxy,ajp_proxy)

以上主要版本的兼容性

2:配置Tomcat

将下载的tomcat拷贝2份,分别命名tomcat1,tomcat2。

修改server文件,修改内容

 A:服务端口<Server port="8005" shutdown="SHUTDOWN">,将其中一份改为<Server port="9005" shutdown="SHUTDOWN">

 B:访问端口<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"  redirectPort="8443" />,将其中一份改为

                <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

 C:修改AJP协议端口<Connector port="11009" protocol="AJP/1.3" redirectPort="8443" />,将其中一份改为<Connector port="12009" protocol="AJP/1.3" redirectPort="8443">

 D:添加以下两句:

  <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

其中一个 jvmRoute="tomcat2"

如果tomcat服务器分别在不懂的服务器,A和B可以省略

3:配置Apache

 在Apache的安装目录D:\*\Apache2.2\conf下,修改httpd.conf文件

 A:在文件的结尾添加以下内容

  LoadModule jk_module modules/mod_jk.so
  JkWorkersFile conf/workers.properties
  JkLogFile logs/mod_jk.log
  JkLogLevel debug
  JkMount  /*.do controller
  JkMount  /*.jsp controller

 B:在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=false

worker.controller.sticky_session_force=1

4:将下载的tomcat-connectors-1.2.40-windows-i386-httpd-2.2.x解压,得到mod_jk.so文件,拷贝到和conf同级的modules目录下

5:启动apache服务器,搭建完成。

6:测试搭建的集群,在两份tomcat的服务器下发布一个测试项目test,这个测试项目很简单,只有一个test.jsp文件,WEB-INF文件夹,web.xml

7:test.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>
View Code

8:在web.xml的<web-app>标签下添加<distributable/>,以实现几个tomcat服务器下测试项目test的session共享。
9:分别启动两个tomcat服务器,访问test项目,http://localhost/test/test.jsp(注意这里的localhost是依据当初安装apache是的配置,安装参数见install.log)

10:如果访问成功,刷新页面,会发现交替访问tomcat1和tomcat2服务器,这是说明集群搭建成功

你可能感兴趣的:(apache)