您的位置:首页 > 运维架构 > Tomcat

配置Apache2.2.10负载平衡 + mod_jk-1.2.28 +Tomcat6集群(linux)

2009-07-31 17:03 399 查看
1. 安装apache

 

      Apache2.2.11 安装配置  

       1  解压缩Apache,通读httpd目录里的INSTALL文件  

        # tar -zxvf httpd-2.2.11.tar.gz  

        可以把得到的文件夹移动到安装目录 /usr/local  

        # cd httpd-2.2.11 

        # ./configure   

        # ./configure --prefix=/usr/local/apache2 --enable-mods-shared=all  

        # make         

        # make install  

        # cd /usr/local/apache2/bin  

        # ./apachectl start  

 

        在地址栏输入http://localhost/,看到 it works欢迎页表示成功  

        参照:http://httpd.apache.org/docs/2.2/install.html  

 

        2  配置apache/conf/http.conf  

 

        我这里项目放在  /usr/local/apache2/htdocs   下(apache的默认目录)

添加:(让apache不访问WEB-INF目录)

 

        <Directory  "/usr/local/apache2/htdocs/WEB-INF">  

           Order allow,deny  

        </Directory>  

 

        打开注释配置语言,可以设置默认中文  

        Include conf/extra/httpd-languages.conf  

 

在其下加入:日支持日文  

        DefaultLanguage ja-Jp  

        AddDefaultCharset utf-8  

 

 

2.   安装jk_mod,整合Apache与Tomcat     

    1  把下载到的二进制文件mod_jk-1.2.28-httpd-2.2.x.so复制到apache的modules目录下  

    下载地址: http://apache.mirror.phpchina.com/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.28/  

 

    2  在Apache的conf目录新建立两个文件,mod_jk.conf和workers.properties文件,  

        mod_jk.conf的内容是jk的配置文件,包括装载模块和日志信息以及指定解析的工作器和目录,workers.properties是Tomcat wokers的配置文件。  

 

    3  在mod_jk.conf里加入以下内容(mod_jk.conf文件是方便管理,而不需要把配置都写在httpd.conf里)  

 

        #加载jk模块,名字要对应  

        LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.x.so 

        # 加载刚才新建的workers.properties文件  

        JkWorkersFile conf/workers.properties  

 

        # Jk日志文件  

        JkLogFile logs/mod_jk.log  

 

        # Jk日志的级别,参数有[debug/error/info]  

        JkShmFile logs/mod_jk.shm  

        JkLogLevel info  

 

        # Jk日志的数据格式  

        JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "  

 

        # Jk的一些配置选项 indicate to send SSL KEY SIZE,  

        JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories 

        # Jk请求日志的格式  

        JkRequestLogFormat "%w %V %T"  

 

        #JSP do Servlet文件交由Tomcat处理  

        JkMount /servlet/* ajp13      

        JkMount /*.jsp ajp13  

        JkMount /*.do ajp13    

        # JkMount 表示使用Tomcat来解析, JkUnMount则相反  

 

         #  一般是jsp和servlet程序交由tomcat处理就可以了,静态文件图片等仍然由Apache处理  

 

        #JkAutoAlias /usr/local/apache2/htdocs*   

        #该文件夹下所有的静态文件自动交由Apache处理  

 

        JkMount /servlet/* ajp13     

        #/test/servlet/下所有的文件也交给Tomcat处理  

 

    4  编辑Apache的httpd.conf文件,把jk_mod.conf文件装载进来:  

        Include conf/mod_jk.conf  

 

    5  编辑workers.properties文件,添加以下内容:  

        workers.tomcat_home=/usr/local/tomcat/  

        workers.java_home=/usr/java/jdk/  

        ps=/  

        worker.list=ajp13  

        worker.ajp13.port=8009  

        worker.ajp13.host=localhost  

        worker.ajp13.type=ajp13  

        worker.ajp13.lbfactor=1  

        #配置参考请见http://tomcat.apache.org/connectors-doc/reference/workers.html         

 

 

    7   重启Apache测试  

        # ./apachectl restart  

        打开浏览器,分别通过以下两个地址访问jsp文件,如果均能正常解析表示整合成功,根据之前Apache和Tomcat的配置,test指向同一个目录  

        http://localhost/   

        http://localhost:8080/

 

        这样,Apache与Tomcat加Jk_mod就整理完成了    

 

3. 配置   workers.properties

 

worker.list=controller,myworker1,myworker2,myworker3
worker.myworker1.port=8009
worker.myworker1.host=192.168.0.10
worker.myworker1.type=ajp13
worker.myworker1.lbfactor=1
worker.list=myworker2
worker.myworker2.port=8010
worker.myworker2.host=192.168.0.10
worker.myworker2.type=ajp13
worker.myworker2.lbfactor=1
worker.list=myworker3
worker.myworker3.port=8011
worker.myworker3.host=192.168.0.10
worker.myworker3.type=ajp13
worker.myworker3.lbfactor=1
worker.controller.type=lb
worker.controller.balanced_workers=myworker1,myworker2,myworker3
worker.controller.sticky_session=1
 

 

4. 配置mod_jk

LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.X.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkShmFile logs/mod_jk.shm
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkRequestLogFormat "%w %V %T"
#JkMount /*.jsp myworker1
#JkMount /*.do myworker1
#JkAutoAlias /usr/local/apache2/htdocs*
#JkMount /servlet/* myworker1
JkMount /* controller
 

 

5. 安装多个tomcat,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="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" />
<!-- 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="8080" 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="8009" 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="jvm1">
-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="controller">
<!--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"
channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
-->
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4006"
selectorTimeout="100"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
<!-- 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"/>
-->
</Host>
</Engine>
</Service>
</Server>
 

 

6. 注意事项

 

(1) tomcat中的

 

<Engine name="Standalone" defaultHost="localhost" jvmRoute="controller">

 

是 Standalone  jvmRoute 是负载均衡的控制器

 

(2)

 

  项目的web.xml 中最上面必须加 <distributable/> 或者or set at your
<Context distributable="true" />


 

(3)

 

 

 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

                      address="auto"

                      port="4006"

                      selectorTimeout="100"

                      maxThreads="6"/>

 

 

的port 的范围在 4000-4100 

 

(4)确保均衡管理器中配置  sticky session mode.

 

worker.controller.sticky_session=1

 

(5)worker.ajp13.port=8009    端口需要和 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 对应

 

(6)worker.myworker3.lbfactor=1 是配置均衡分配的概率系数,越大出现次数越大

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息