tomcat 集群配置

          长话短说,今天小配置了一个集群,是用apache做前段服务器,session管理方式用的session复制,然后把后缀名为 jsp和do的请求发送给后端tomcat

         首先的下载tomcat服务器 这里用的是:apache-tomcat-6.0.39-windows-x86  zip版,不需要配置环境变量,但必须设置好java环境变量

         Apache 服务器 httpd-2.0.65-win32-x86-openssl-0.9.8y 这个openssl和no_ssl都行

        插件:tomcat-connectors-1.2.40-windows-i386-httpd-2.0.x 注意tomcat 版本和apache版本

       上面三个东西都可以上网搜到,读者自行去搜索吧,可以锻炼一下读英文的能力

       配置集群,首先的让tomcat能在一台机器上运行,所以必须修改其端口号,使其不冲突其配置文件如下:


       

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8006" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  
  <GlobalNamingResources>
  
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">
  
   
    <Connector port="8081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8445" />
    
         
  
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8445" />


   

    
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

    
            
 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    
    

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

    
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        
       
     

      </Host>
    </Engine>
  </Service>
</Server>

tomcat2

<?xml version='1.0' encoding='utf-8'?>


<Server port="8007" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />


  <GlobalNamingResources>
  
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  
  <Service name="Catalina">
  
    
    

    <Connector port="8083" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8446" />
   
           
  
   

   
    <Connector port="8011" protocol="AJP/1.3" redirectPort="8446" />


  

  
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

      
	 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

    

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

     
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

     


      </Host>
    </Engine>
  </Service>
</Server>

然后配置apached

将tomcat-connectors-1.2.40-windows-i386-httpd-2.0.x解压,将里面的mod_jk.so 放到apached的modules下面

然后将conf 的httpd配置文件里面加上一句话:include "D:\Program Files (x86)\Apache Group\Apache2\conf\mod_jk.conf"

在conf里面 新建mod_jk.conf 将一下内容复制进去

LoadModule jk_module "D:/Program Files (x86)/Apache Group/Apache2/modules/mod_jk.so"
JkWorkersFile "conf/workers.properties"
JkMount /*.jsp controller
JkMount /*.do controller

上面内容是加载mod_jk模块,加载负均衡配置文件,将*.jsp 和*.do转发给tomcat

在conf里新建一个workers.properties 复制以下内容

#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=8010
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=8011
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
#worker.controller.sticky_session=1

至此,集群就配置好了,启动tomcat和apache

可以写个测试类试试,这是到网上copy的:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ 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="test.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>

如果没有web-inf 记得建立一个这样的文件夹,并且在web.xml里面的<web-aap>加上</description> 

如下效果图:


tomcat 集群配置_第1张图片

tomcat 集群配置_第2张图片

tomcat1和tomcat2都session的值,说明session复制成功了

当然这种集群方式只适合早起的企业应用系统,在集群之间session同步到其他机器,这种方案简单,但只适合较少量的web服务器,如果多了则服务器开销大都消耗在session的复制上去了,在用户很多的情况下,会出现服务器内存不够用的情况

你可能感兴趣的:(tomcat 集群配置)