`
kirk1127
  • 浏览: 28837 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

apache2.2.14 + Tomcat6.0.14/Tomcat6.0.20配置负载均衡

阅读更多
apache2.2.14 + Tomcat6.0.14/Tomcat6.0.20配置负载均衡
关键字: apache tomcat 负载均衡
Apache 做为 HttpServer ,后面连接多个 tomcat 应用实例,并进行负载均衡

1.安装JDK6.0
2.安装Tomcat6.0.14和Tomcat6.0.20
3.下载并安装Apache HTTP Server2.2.14,下载地址:http://hitech.onlinedown.net/down/apache_2.2.14-win32-x86-no_ssl.zip
    此处注意,如果你本机已经装了iis并且占用80端口,务必先在服务中将iis停止或禁用。软件默认安装即可。安装过程中设置Network Domain和Server Name为localhost,设置你的email地址,下方会让你选择only for the current user或for all users。默认选择for all users。这样Apache就会占用80端口,并且做为一个系统服务开机自运行。
  安装完成之后,你在浏览器中输入http://localhost,将会看到Apache的成功页面“It works!”
4..配置Apache:
修改Apache配置文件\Apache2.2\conf\httpd.conf
1)将以下6个Module的注释去掉:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
apache2.x以前配置apache和tomcat可能需要用到mod_jk.so进行apache和tomcat的链接。apache2.x中集成了mod_jk.so的功能,只需简单的把上面6个module的注释打开。
2)在DirectoryIndex后面增加index.jsp,改完以后是:
Xml代码
<IfModule dir_module> 
    DirectoryIndex index.html index.jsp  
</IfModule> 

<IfModule dir_module>
    DirectoryIndex index.html index.jsp
</IfModule>
3)在文件httpd.conf的最后加入以下内容:
Xml代码
ProxyRequests Off  
<proxy balancer://cluster> 
BalancerMember ajp://127.0.0.1:8010 loadfactor=1 route=jvm1 
BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm2 
</proxy> 

ProxyRequests Off
<proxy balancer://cluster>
BalancerMember ajp://127.0.0.1:8010 loadfactor=1 route=jvm1
BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm2
</proxy>
4)设置apache2.2虚拟主机
a>.先在conf\httpd.conf中引入配置虚拟主机的控制文件,将配置文件中默认注释掉的配置信息放开就可以了:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
b>.修改conf/extra/httpd-vhosts.conf文件,在最下面加入虚拟主机的配置信息
Xml代码
<VirtualHost *:80> 
         ServerAdmin bjyzxxds@iteye.com  
         ServerName localhost  
         ServerAlias localhost  
         ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On 
         ProxyPassReverse / balancer://cluster/  
</VirtualHost> 

<VirtualHost *:80>
         ServerAdmin bjyzxxds@iteye.com
         ServerName localhost
         ServerAlias localhost
         ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On
         ProxyPassReverse / balancer://cluster/
</VirtualHost>
其中的域名和路径的值依照安装apache http server2.2时设置的域名和路径,保持一致即可。

5.配置tomcat
1).配置tomcat的启动和关闭端口
若是在同一台机器上同时运行多个tomcat,则需要修改tomcat 的启动端口和关闭端口。
a>.启动端口修改:将每个tomcat的server.xml文件作如下修改
Xml代码
<Connector port="8080" protocol="HTTP/1.1"   
               connectionTimeout="20000"   
               redirectPort="8443" /> 

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
改为
Xml代码
<Connector port="xxxx" protocol="HTTP/1.1"   
               connectionTimeout="20000"   
               redirectPort="8443" /> 

<Connector port="xxxx" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
如果需要用到tomcat的共享线程池,其端口值也设置为xxxx,保持一致(线程池缺省是被注释掉的):
Xml代码
<Connector executor="tomcatThreadPool" 
               port="xxxx" protocol="HTTP/1.1"   
               connectionTimeout="20000"   
               redirectPort="8443" /> 

<Connector executor="tomcatThreadPool"
               port="xxxx" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
b>.设置tomcat的关闭端口:
将<Server port="8005" shutdown="SHUTDOWN">修改为
<Server port="yyyy" shutdown="SHUTDOWN">每个tomcat的端口yyyy不同就行。

2).配置engine
tomcat默认的engine配置为:
<Engine name="Catalina" defaultHost="localhost">
为了通过AJP来支持负载均衡,需要配置engine的jvmRoute属性。将server.xml的默认engine配置注释掉,按给出的jvmRoute示例做如下配置:
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
注:关于engine的name属性值的设置,不同版本的tomcat里面server.xml中给出的配置例子不同,比如tomcat6.0.14中的例子为:
   Xml代码
<!-- You should set jvmRoute to support load-balancing via AJP ie :  
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">           
    --> 

<!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">        
    -->
tomcat6.0.20中例子为:
    Xml代码
<!-- You should set jvmRoute to support load-balancing via AJP ie :  
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">           
    --> 

<!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">        
    -->
一般按照tomcat中给的例子的name属性值设置即可(Catalina或Standalone或aaa,任意都行),新安装的tomcat在第一次启动后,将在tomcat的\conf\目录下创建一个与engine中name属性值一样的文件夹。
不过在我配置时发现6.0.14版本的tomcat在启动后并没有在\conf\目录下新建文件夹,而且查看启动日志也没发现异常。6.0.14版的tomcat是安装exe的形式注册服务的,6.0.20版的tomcat是解压然后手动注册服务的,安装形式不同,可能这是这个地方存在差异的原因吧。


3)配置AJP Connector
将server.xml中的缺省配置
Xml代码
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改为
Xml代码
<Connector port="zzzz" protocol="AJP/1.3" redirectPort="8443" /> 

<Connector port="zzzz" protocol="AJP/1.3" redirectPort="8443" />
port的值zzzz与apache2.2中的文件httpd.conf中<proxy balancer://cluster></proxy>之间配置的均衡器成员的端口一致,一一对应;上面在配置的jvmRoute的值(如jvm1)应该与httpd.conf中<proxy balancer://cluster></proxy>之间配置的均衡器成员的route属性的值一致,且必须唯一。

4)配置Cluster
打开tomcat的server.xml,默认的Cluster配置如下,缺省被注释掉了:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
如何配置<Cluster,可以参考\webapps\docs\cluster-howto.html,直接将说明文档中“Configuration Example”后面的一段复制过来,即可满足基本的使用,内容如下:
Xml代码
<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="5000" 
                      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> 

<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="5000"
                      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.JvmRouteSessionIDBinderListener"/>  

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
若启动时出现这样的错误提示:
2009-11-17 13:02:55 org.apache.catalina.ha.deploy.FarmWarDeployer start
严重: FarmWarDeployer can only work as host cluster subelement!
则将以下部分注释掉即可:
Xml代码
<!--  
          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" 
                    tempDir="/tmp/war-temp/" 
                    deployDir="/tmp/war-deploy/" 
                    watchDir="/tmp/war-listen/" 
                    watchEnabled="false"/> 
          --> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics