Apache + Tomcat + mod_jk实现集群服务及session共享

实现效果:用apache 分发请求到tomcat中的对应的项目 

原理:

环境说明:

操作系统:win7   64位

Javajdk: 1.7 
Apache:httpd-2.2.25-win32-x86-no_ssl.msi    (本地安装路径:D:\Program Files (x86)\Apache2.2\)
Tomcat: 7.0.42  ( http://tomcat.apache.org/download-70.cgi ),如果在同一台机器上模拟,下载zip版本. 实例中展示了2个节点
mod_jk: mod_jk1.2.30_x64:  ( http://tomcat.apache.org/download-connectors.cgi )

安装步骤:

1.安装jdk
2.安装Apache2.2,使用默认设置,并且安装路径中不要空格.
3.解压tomcat
4.拷贝mod_jk.so到Apache安装路径的modules文件夹下

 

配置步骤

 

修改Apache配置:

 

关于修改涉及到的文件httpd.conf和workers.properties文件可以下载一份mod_jk的源码包参看

 

   1.修改Apache配置文件httpd.conf(笔者路径:D:\Program Files (x86)\Apache2.2\conf\httpd.conf), 在最后一行末尾添加:

 

include "D:\Program Files (x86)\Apache2.2\conf\mod_jk.conf"
2. 在httpd.conf 同目录下新建mod_jk.conf文件
#指定模块路径
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定哪些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /* controller

#指定jk的日志输出文件
JkLogFile logs/mod_jk.log
#指定日志级别,我这里设置的警告
JkLogLevel warn
#包含标准的mod_jk行为 (默认)
#info
#包含错误信息
#error
#用来配置log文件的日期/时间格式. 使用strftime()的格式化字符串,默认是[%a %b %d %H:%M:%S %Y]
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"    

#Options Description(选项的说明)
# %b 发送的字节, 不包括 HTTP headers (CLF format)
# %B 发送的字节, 不包括 HTTP headers
# %H 协议
# %m 请求方式(get/post)
# %p 服务器响应请求的规范端口.
# %q 查询字符串 (如果存在以?开头,否则是空串)
# %r 请求的第一行.
# %s HTTP状态码
# %T 请求间隔, 处理请求耗费的时间 秒.微秒
# %U 请求的url路径,不包含查询字符串.
# %v 响应请求的规范服务器名字
# %V 根据UseCanonicalName设置的服务器名字.
# %w Tomcat worker 名字
#JkRequestLogFormat 设置个人用户请求的log格式.
JkRequestLogFormat "%w %V %T"

3.在httpd.conf同目录下新建 workers.properties文件

#这里可以配置任意多个Tomcat,此处配置了2个Tomat服务器.
#host和port根据自己实际配置.实例配置的是本机两个tomcat,分别使用不同的端口.避免冲突
#如果Tomcat不再同一机器上,没必要改端口的。


#server 列表
worker.list=controller,status,tomcat1,tomcat2 

#========tomcat1========

worker.tomcat1.port=9988        #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=192.168.0.47        #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1  #server的加权比重,值越高,分得的请求越多
  

#========tomcat2========

worker.tomcat2.port=9999        #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=192.168.0.47        #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1        #server的加权比重,值越高,分得的请求越多 



#========controller,负载均衡控制器========
worker.controller.type=lb


#指定此负载平衡器负责的Tomcat应用节点。

worker.controller.balance_workers=tomcat1,tomcat2   #指定分担请求的tomcat

#此处指定集群是否需要会话复制,如果设为true,则表明为会话粘性,不进行会话复制,当某用户的请求第一次分发到哪台
#Tomcat后,后继的请求会一直分发到此Tomcat服务器上处理;如果设为false,则表明需求会话复制。
worker.controller.sticky_session=false
#描述是用于httpd自身状态监控的status
worker.status.type=status

4.在httpd.conf同目录下新建 uriworkermap.properties文件(未测试是否对集群有影响)

/*=controller                       
/jkstatus=status                   
!/*.gif=controller                  
 !/*.jpg=controller 
!/*.png=controller 
!/*.css=controller 
!/*.js=controller 
!/*.htm=controller 
!/*.html=controller

 修改Tomact配置:(tomcat 6.0.36  可以实现集群  ,但是session没有共享。原因未找到)(tomcat 7.0.42 实现集群及session共享)

 

以 tomcat 7.0.42  为实例

1.修改分发tomcat对应的service.xml文件,保证Apache对应的 workers.properties中的AJP13的connector的port. 

<!-- 定义一个AJP 1.3 连接端口为9988 ,默认值为8009,这里我们改成我们自己定义的9988端口 -->
<Connector port="9988" protocol="AJP/1.3" redirectPort="8443" />

2.增加jvmRoute的值,保证同workers.properties里边配置的值一致

<!--增加jvmRoute,值为在Apache中配置的list集群结点中的值,这里定义为tomcat1结点-->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

3.去掉默认注释掉的集群配置

<!--取消集群结点相关的注释,该句默认值注释掉的,我们需要配置集群所以去掉注释,让其起作用-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

 

4.实例中我们的两个tomcat结点在同一台机器上,所以还需要保证protocol="HTTP/1.1"的端口不一致.不然本地的两个tomcat会起冲突

下面为笔者实例中解决同一台机器上多个tomcat服务器之间端口冲突做的修改.

 

实例测试

   1.在web.xml文件中增加

<distributable/>

2.编写测试jsp代码 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.text.SimpleDateFormat"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Tomcat集群测试</title>
  </head>
  
  <body>
        服务器信息:

    <%
      String dtm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
      System.out.println("["+request.getLocalAddr()+":"+ request.getLocalPort()+"]" + dtm);
      out.println("<br>["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + dtm+"<br>"); 
    %>
    
    session分发:
    <%
        session.setAttribute("name","dennisit");
        System.out.println("[session分发] session id:"+session.getId());
        out.println("<br>[session分发] session id: " + session.getId()+"<br>");
    %>
  </body>
</html>

3.测试负载均衡与session分发

将项目部署到每个集群结点中,即实例中的tomcat_node1和tomcat_node2,依次移动Apache和tomcat服务器,tomcat服务器之间的启动顺序随意.这里Apache端口使用默认的80.

上面是在FireFox中运行项目后刷新3次执行的效果,可以看到2个tomcat分发结点之间轮流负载.而且两台服务器上的session值是一样的.说明session分发成功.

两台Tomcat服务器日志打印输出结果:

错误解决:

    (1)apache 服务器无法加载jk代理

                        原因:apache 和 mod_jk  版本差异导致。解决办法,将mod_jk.so替换为 mod_jk-1.2.31-httpd-2.2.3.so。

    (2)apache 服务器无法启动

                      原因:httpd.conf  配置文件有误。首次安装可以启动。你改动有误。

 其他

     附上tomcat  6   与tomcat  7   server.xml  对比代码:

tomcat  6  

<?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="8005" 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" />


  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <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>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container", 
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
  
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
        maxThreads="150" minSpareThreads="4"/>
    -->
    
    
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="9008" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    -->           
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9988" enableLookups="false" protocol="AJP/1.3" redirectPort="8443" />

    <!--For clustering-->
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<!--    <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
                 managerClassName="org.apache.catalina.cluster.session.DeltaManager"
                 expireSessionsOnShutdown="false"
                 useDirtyFlag="true"
                 notifyListenersOnReplication="true">

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

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

            <Sender
                className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
                replicationMode="pooled"
                ackTimeout="15000"
                waitForAck="true"/>

            <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
                   filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
                  
            <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
                      tempDir="/tmp/war-temp/"
                      deployDir="/tmp/war-deploy/"
                      watchDir="/tmp/war-listen/"
                      watchEnabled="false"/>
                     
            <ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
        </Cluster>


          
-->


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

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

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->        

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- 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"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
      <!-- <context docBase= "e:\webapps\tomcat_apche" path="/tomcat_apche"  debug="0" reloadable="true"/> -->
      <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->

      </Host>
    </Engine>
  </Service>
</Server>
View Code

tomcat  6  死活没弄出来session 共享  即使加上各位大神的如下代码。(无意冒犯,就事说事。)

http://www.linuxidc.com/Linux/2012-08/69311p2.htm

tomcat  7

<?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="8005" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--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" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <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>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="9008" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9988" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

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

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!-- -->
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
     

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- 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"/>
      </Realm>

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

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>
View Code

比较不错的大牛参考博文:

     Apache+Tomcat负载均衡两种session共享方式的设置:http://blog.sina.com.cn/s/blog_5f53615f0100p4fj.html

     Apache + Tomcat + mod_jk实现集群服务:http://www.linuxidc.com/Linux/2015-02/114231.htm

     Apache2+Tomcat7+mod_jk2.2.3集群负载均衡配置(目前最强悍)


 
  
 

 

posted @ 2015-12-18 16:11  Vip灬cnblog  阅读(4187)  评论(13编辑  收藏  举报