Apache+tomcat集群配置

张军 4473 0

1         Apache+tomcat集群配置

1.1        分布式实现原理

张军博客

如上图所示,主要通过Apache-Server作为中转服务器,实现多个tomcat服务器之间的分布式处理,用户直接请求Apache-Server,然后Apache-Server会将请求分发到具体的tomcat-server,之后tomcat-server响应客户请求并返回结果到Apache-Server,最后Apache-Server返回结果给用户

1.2        负载均衡

文件说明:

mod_jk.conf

主要定义mod_jk模块的位置以及mod_jk模块的连接日志设置,还有定义worker.properties文件的位置。

worker.properties

定义worker的参数,主要是连接tomcat主机的地址和端口信息。如果Tomcatapache不在同一台机器上,或者需要做多台机器上tomcat的负载均衡只需要更改workers.properties文件中的相应定义即可。

APACHE_HOME%为你的安装目录

环境说明:

主要使用了一个Apache   Server和两个Tomcat,在同一台电脑上进行测试。

1.3        准备软件

Jdk1.6

下载地址:http://java.sun.com

tomcat6.0.37

下载地址:http://jakarta.apache.org

httpd-2.2.19-win64

下载地址:http://httpd.apache.org/download.cgi

mod_jk1.2.30_x64(主要作用是建立Apache ServerTomcat之间的连接)

下载地址:http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/

说明:apache-server安装完成后,可以在浏览器中输入http://localhost/来测试,如果出现” It works!”则表示安装成功。

1.4        Windows64Apache服务器的安装

首先需要说明的是,Apaceh服务器没有官方的64位版本,只有民间的高手自己制作的。下面是具体的安装步骤。

1.         https://blackdot.be/download-archive/ 下载64位的Apache服务器。目前的版本是2.2.19,下载之后的压缩包名为httpd-2.2.19-win64.rar

2.         将该包解压至一个目录,如我的目录为E:\Apache2.2.19

3.         修改conf文件夹下的httpd.conf文件:
  将文件中的
/httpd-2.2-x64全部替换为自己的解压目录,如E:/Apache2.2.19,注意这里的路径中必须使用斜杠/
  将
Listen 80改为Listen 8080,若已开启IIS,则80端口就不能用了;
  将
#LoadModule rewrite_module modules/mod_rewrite.so这一行最前面的#去掉。

4.         执行安装命令。通过命令行进入自己的安装目录下的bin文件夹,如E:\Apache2.2.19\bin,执行命令httpd.exe -k   install,这样就把apache加入了系统服务,默认服务名为Apache2.2。启动服务的命令为httpd.exe -k start

5.       测试安装是否成功。开启服务,运行Apache服务器后,打开浏览器输入http://localhost:8080/,如果页面显示It works!,则表示安装成功,Apache服务器可以正常使用。

要实现session复制,还需要在context.xml添加属性distributable="true",如下:

<Context distributable="true"/>

6.         web.xml

<distributable/>

 

 

1.5        修改tomcat端口号和安装mod_jk连接模块

1.5.1        修改tomcat8990配置文件server.xml

<?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="8079"   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

      -->

      <!--更改成80,则不用写端口号-->

      <Connector port="8990"

               maxThreads="150"   minSpareThreads="25" maxSpareThreads="75"

                 enableLookups="false" redirectPort="8991"   acceptCount="100"

                 connectionTimeout="20000"   disableUploadTimeout="true" URIEncoding="UTF-8"/>

      <!-- 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="8992"   protocol="AJP/1.3" redirectPort="8991" />

 

 

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

 

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

        -->     

<!—集群配置--> 

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

 

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

          -->

          <!--

              E:\versionManager\sources\java\zj-utils\message_solr_1\WebRoot

              -->

              <!--   reloadable="true"-->

              <!--配置java-web项目与tomcat整合-->

              <!--

              <Context   path="/solr" docBase="E:\versionManager\sources\java\zj-web\zjsystem-searcher\zj-solr-app-4.7.1\WebRoot"   reloadable="true" crossContext="true"  override="true">

              </Context>-->

              <!--

              <Context   path="/msg_solr"   docBase="E:\versionManager\sources\java\zj-web\zjsystem-my\zj-msg-solr\WebRoot"   debug="0" reloadable="true">

              </Context>

              <Context   path="/solr"   docBase="E:\bigfile\services\java\apache-lucene-solr\service\solr"   debug="0" reloadable="true">

                     <Environment   name="solr/home" type="java.lang.String"   value="E:\bigfile\services\java\apache-lucene-solr\home"   override="true"/>

              </Context>

              -->

              <!--配置solr项目与tomcat整合path="/solr"写成path="",则默认根目录-->

              <!--

          <Context path="/solr"   docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr"   reloadable="true">

                     <Environment   name="/solr/home" type="java.lang.String"   value="E:\bigfile\services\java\apache-lucene-solr4\home"   override="true"/>

          </Context>

       -->

      

      

        <!--<Context path="/iqc"   docBase="e:\MyEclipse-8.6.1\iqc\WebRoot"   crossContext="true" reloadable="false"/>

        <Context path="/iqc"   docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\shanghai-bank-iqc\WebRoot"   crossContext="true" reloadable="true"></Context>

        

        -->

              <!--<Context   path="/iqc" docBase="E:\MyEclipse8.6.1\iqc-ccb\WebRoot"   crossContext="true" reloadable="false" />

      

              <Context   path="/solr" docBase="e:\solr\server"   reloadable="false">

                            <Environment   name="solr/home" type="java.lang.String"   value="e:\solr\home"   override="true"></Environment>

                     </Context>-->

 

              <!--<Context   path="/iqc" docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\jianshe-bank-iqc\WebRoot"   crossContext="true"   reloadable="false"></Context>-->

                            <!--<Context   path="/solr"   docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr"  reloadable="false">

                                   <Environment   name="solr/home" type="java.lang.String"   value="E:\bigfile\services\java\apache-lucene-solr4\home"   override="true"/>

                            </Context>-->

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

1.5.2        修改tomcat8990配置文件context.xml

<?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.

-->

<!-- The contents of this file will be   loaded for each web application -->

<!--同步session-->

<Context   distributable="true">

 

      <!-- Default set of monitored resources -->

      <WatchedResource>WEB-INF/web.xml</WatchedResource>

              <!--<Loader   delegate="true" /> -->       

      <!-- Uncomment this to disable session persistence across Tomcat   restarts -->

      <!--

      <Manager pathname="" />

      -->

 

      <!-- Uncomment this to enable Comet connection tacking (provides   events

           on session expiration as well as webapp lifecycle) -->

      <!--

      <Valve   className="org.apache.catalina.valves.CometConnectionManagerValve"   />

      -->

 

</Context>

 

1.5.3        修改tomcat8999配置文件server.xml

<?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="8998"   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

      -->

      <!--更改成80,则不用写端口号-->

      <Connector port="8999"

               maxThreads="150"   minSpareThreads="25" maxSpareThreads="75"

                 enableLookups="false" redirectPort="8997"   acceptCount="100"

                 connectionTimeout="20000" disableUploadTimeout="true"   URIEncoding="UTF-8"/>

      <!-- 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="8996" protocol="AJP/1.3"   redirectPort="8997" />

 

 

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

 

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

        -->       

<!—集群配置-->

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

 

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

          -->

          <!--

              E:\versionManager\sources\java\zj-utils\message_solr_1\WebRoot

              -->

              <!--   reloadable="true"-->

              <!--配置java-web项目与tomcat整合-->

              <!--

              <Context   path="/solr"   docBase="E:\versionManager\sources\java\zj-web\zjsystem-searcher\zj-solr-app-4.7.1\WebRoot"   reloadable="true" crossContext="true"  override="true">

              </Context>-->

              <!--

              <Context   path="/msg_solr"   docBase="E:\versionManager\sources\java\zj-web\zjsystem-my\zj-msg-solr\WebRoot"   debug="0" reloadable="true">

              </Context>

              <Context   path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr\service\solr"   debug="0" reloadable="true">

                     <Environment   name="solr/home" type="java.lang.String"   value="E:\bigfile\services\java\apache-lucene-solr\home"   override="true"/>

              </Context>

              -->

              <!--配置solr项目与tomcat整合path="/solr"写成path="",则默认根目录-->

              <!--

          <Context path="/solr"   docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr"   reloadable="true">

                     <Environment   name="/solr/home" type="java.lang.String"   value="E:\bigfile\services\java\apache-lucene-solr4\home"   override="true"/>

          </Context>

       -->

      

      

        <!--<Context path="/iqc"   docBase="e:\MyEclipse-8.6.1\iqc\WebRoot"   crossContext="true" reloadable="false"/>

        <Context path="/iqc"   docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\shanghai-bank-iqc\WebRoot"   crossContext="true" reloadable="true"></Context>

        

        -->

              <!--<Context   path="/iqc" docBase="E:\MyEclipse8.6.1\iqc-ccb\WebRoot"   crossContext="true" reloadable="false" />

      

              <Context   path="/solr" docBase="e:\solr\server"   reloadable="false">

                            <Environment   name="solr/home" type="java.lang.String"   value="e:\solr\home"   override="true"></Environment>

                     </Context>-->

 

              <!--<Context   path="/iqc"   docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\jianshe-bank-iqc\WebRoot"   crossContext="true"   reloadable="false"></Context>-->

                            <!--<Context   path="/solr"   docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr"  reloadable="false">

                                   <Environment   name="solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr4\home"   override="true"/>

                            </Context>-->

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

1.5.4        修改tomcat8999配置文件context.xml

<?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.

-->

<!-- The contents of this file will be   loaded for each web application -->

<!--同步session-->

<Context   distributable="true">

 

      <!-- Default set of monitored resources -->

      <WatchedResource>WEB-INF/web.xml</WatchedResource>

              <!--<Loader   delegate="true" /> -->       

      <!-- Uncomment this to disable session persistence across Tomcat   restarts -->

      <!--

      <Manager pathname="" />

      -->

 

      <!-- Uncomment this to enable Comet connection tacking (provides   events

           on session expiration as well as webapp lifecycle) -->

      <!--

      <Valve className="org.apache.catalina.valves.CometConnectionManagerValve"   />

      -->

 

</Context>

 

 

 

 

1.5.5        创建文件

安装好Jdktomcatapache,加入mod_jk连接模块,把mod_jk1.2.30_x64\modules\ mod_jk.so文件拷贝到%APACHE_HOME\modules下,把jk模块的配置放到单独的文件中来,在%APACHE_HOME\conf目录新建workers.properties文件。

1.5.6        httpd.conf最后加上

#加载mod_jk Module

LoadModule jk_module modules/mod_jk.so

#指定 workers.properties文件路径

JkWorkersFile conf/workers.properties

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器

JkMount /*.jsp controller

创建mod_jk.conf文件,也可用Include conf/mod_jk.conf包含

1.5.7        修改workers.properties文件

#server 列表

worker.list = controller,tomcat1,tomcat2

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

#ajp13 端口号,在tomcatserver.xml配置,默认8009

worker.tomcat1.port=8992

#tomcat的主机地址,如不为本机,请填写ip地址

worker.tomcat1.host=localhost

worker.tomcat1.type=ajp13

#server的加权比重,值越高,分得的请求越多

worker.tomcat1.lbfactor=1

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

#ajp13 端口号,在tomcatserver.xml配置,默认8009

worker.tomcat2.port=8996

#tomcat的主机地址,如不为本机,请填写ip地址

worker.tomcat2.host=   localhost

worker.tomcat2.type=ajp13

#server的加权比重,值越高,分得的请求越多

worker.tomcat2.lbfactor=1

 

#========controller,负载均衡控制器========

worker.controller.type=lb

#指定分担请求的tomcat

worker.controller.balanced_workers=tomcat1,tomcat2

worker.controller.sticky_session=1

1.6        编写一个测试页面testsession.jsp

<%@ page language="java"   import="java.util.*" pageEncoding="UTF-8"%>

<%

Object mark =   session.getAttribute("mark");

if (mark==null){

       session.setAttribute("mark",new   Date());

}

System.out.println("hello:"+mark);

%>

控制台打印端口号(89908999

hello

hello:Thu Aug   13 00:48:18 CST 2015

2         Ehcache分布式缓存

2.1        手动进行成员发现

进行手动成员配置要知道每个监听器的IP地址和端口。成员不能在运行时动态地添加

和移除。在技术上很难使用广播的情况下就可以手动成员发现,例如在集群的服务器之间有

一个不能传送广播报文的路由器。你也可以用手动成员发现进行单向的数据复制,只让

server2知道server1server1不知道server

配置手动成员发现,需要指定ehcache配置文件中cacheManagerPeerProviderFactory

properties属性,像下面这样:

peerDiscovery=manual   rmiUrls=//server:port/cacheName, ...

rmiUrls配置的是服务器cache peers的列表。注意不要重复配置

Server1节点IP:localhost:8990

Server2节点IP:localhost:8999

2.2        示例

假设你在集群中有两台服务器。你要同步sampleCache1sampleCache2。下面是每个

服务器需要的配置

2.2.1        配置server1ehcache.xml

#各节点对应不同的ehcache.xml文件

cons.cache.file.path=E:/bigfile/services/java/cluster/cache-files/8990/ehcache.xml

<?xml version="1.0"   encoding="UTF-8"?>

<ehcache   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"   dynamicConfig="false">

       <diskStore   path="E:/bigfile/services/java/cluster/cache-files/8990/cache"   />

       <!--   <diskStore path="java.io.tmpdir" /> -->

       <cacheManagerPeerProviderFactory   class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"   properties="peerDiscovery=manual,   rmiUrls=//localhost:40002/zj.page.User|//localhost:40002/userCache"   />

       <cacheManagerPeerListenerFactory   class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"   properties="hostName=localhost,port=40001,   socketTimeoutMillis=3000" />

       <defaultCache   maxElementsInMemory="10000" eternal="true"   timeToIdleSeconds="0" timeToLiveSeconds="0"   overflowToDisk="true" diskExpiryThreadIntervalSeconds="0"   />

       <cache   name="zj.page.User" diskPersistent="true"   maxElementsInMemory="10000" eternal="true"   timeToIdleSeconds="0" timeToLiveSeconds="0"   overflowToDisk="true" diskExpiryThreadIntervalSeconds="0">

              <cacheEventListenerFactory   class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"   properties="replicateAsynchronously=false, replicatePuts=true,   replicatePutsViaCopy=true, replicateUpdates=true,   replicateUpdatesViaCopy=true, replicateRemovals=true,   asynchronousReplicationIntervalMillis=1000" />

              <bootstrapCacheLoaderFactory   class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"   />

       </cache>

       <cache   name="userCache" diskPersistent="true"   maxElementsInMemory="10000" eternal="true"   timeToIdleSeconds="0" timeToLiveSeconds="0"   overflowToDisk="true"   diskExpiryThreadIntervalSeconds="0">

              <cacheEventListenerFactory   class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"   properties="replicateAsynchronously=false, replicatePuts=true,   replicatePutsViaCopy=true, replicateUpdates=true,   replicateUpdatesViaCopy=true, replicateRemovals=true,   asynchronousReplicationIntervalMillis=1000" />

              <bootstrapCacheLoaderFactory   class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"   />

       </cache>

</ehcache>

2.2.2        配置server2ehcache.xml

#各节点对应不同的ehcache.xml文件

cons.cache.file.path=E:/bigfile/services/java/cluster/cache-files/8999/ehcache.xml

<?xml version="1.0"   encoding="UTF-8"?>

<ehcache   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"   dynamicConfig="false">

       <diskStore   path="E:/bigfile/services/java/cluster/cache-files/8999/cache"   />

       <!--   <diskStore path="java.io.tmpdir" /> -->

       <cacheManagerPeerProviderFactory   class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"   properties="peerDiscovery=manual,   rmiUrls=//localhost:40001/zj.page.User|//localhost:40001/userCache"   />

       <cacheManagerPeerListenerFactory   class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"   properties="hostName=localhost,port=40002,   socketTimeoutMillis=3000" />

       <defaultCache   maxElementsInMemory="10000" eternal="true"   timeToIdleSeconds="0" timeToLiveSeconds="0"   overflowToDisk="true" diskExpiryThreadIntervalSeconds="0"   />

       <cache   name="zj.page.User" diskPersistent="true"   maxElementsInMemory="10000" eternal="true"   timeToIdleSeconds="0" timeToLiveSeconds="0"   overflowToDisk="true" diskExpiryThreadIntervalSeconds="0">

              <cacheEventListenerFactory   class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"   properties="replicateAsynchronously=false, replicatePuts=true,   replicatePutsViaCopy=true, replicateUpdates=true,   replicateUpdatesViaCopy=true, replicateRemovals=true,   asynchronousReplicationIntervalMillis=1000" />

               <bootstrapCacheLoaderFactory   class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"   />

       </cache>

       <cache   name="userCache" diskPersistent="true"   maxElementsInMemory="10000" eternal="true"   timeToIdleSeconds="0" timeToLiveSeconds="0"   overflowToDisk="true"   diskExpiryThreadIntervalSeconds="0">

              <cacheEventListenerFactory   class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"   properties="replicateAsynchronously=false, replicatePuts=true,   replicatePutsViaCopy=true, replicateUpdates=true,   replicateUpdatesViaCopy=true, replicateRemovals=true,   asynchronousReplicationIntervalMillis=1000" />

               <bootstrapCacheLoaderFactory   class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"   />

       </cache>

</ehcache>

2.2.3        不同地址访问页面效果如下


张军博客

张军博客

 

 



更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论