[服务器]基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置(一)

5 篇文章 0 订阅
2 篇文章 0 订阅

第一章. 背景简介
对于大多数企业应用,都希望能做到7*24小时不间断运行。要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群。系统变为集群时,除了要求系统能够支持水平伸缩外,还要解决两个问题:
1, 如何均衡地访问到提供业务功能的机器。
2, 如何保证当机器出现问题时,用户能自动跳转到另外的机器,不影响使用。

常用的负载均衡技术有硬件和软件两种,本示例常用软件的技术实现。软件也有很多实现技术,如基于apache的mod_jk以及mod_proxy等。基于mod_jk的文章有不少,本文演示一下用mod_proxy的方式。
实现集群的应用最重要的是处理用户Session的问题,一般有三种策略:
1, Session复制
2, Session Sticky
3, 基于Cache的集中式Session

本文使用的是Tomcat 7.0.2应用服务器,用的方法是Session复制。

第二章. 配置环境
1, JDK1.6,请自行下载安装,搞Java的一般都装有的吧,哈哈。
2, Apache 2.2.16, (released 2010-07-25),现在为止应该是最新的稳定版本,下载地址: http://httpd.apache.org/download.cgi
3, Tomcat 7.0.2,目前也是最新的版本。Minimum Java Version1.6.下载地址:http://tomcat.apache.org/download-70.cgi
4, 安装过程略

第三章. 部署图(略)

第四章. Tomcat7集群配置
一、 就地取材,复制tomcat7/webapps下的examples,重命名为cluster应用,以后就用cluster做测试。
二、 详细配置参照tomcat7 \webapps\docs\cluster-howto.html 或者http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
三、 为了在Tomcat7中实现session复制,以下必需完成:
所有session属性必需实现 java.io.Serializable
• Uncomment the Cluster element in server.xml。把Cluster元素的注释去掉。参照四
• If you have defined custom cluster valves, make sure you have the ReplicationValve defined as well under the Cluster element in server.xml 。参照四
• If your Tomcat instances are running on the same machine, make sure the tcpListenPort attribute is unique for each instance, in most cases Tomcat is smart enough to resolve this on it's own by autodetecting available ports in the range 4000-4100。参照四< Receiver>中的注释
• Make sure your web.xml has the <distributable/> element or set at your <Context distributable="true" /> 参照四
• If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine <Engine name="Catalina" jvmRoute="node01" > and that the jvmRoute attribute value matches your worker name in workers.properties .用mod_jk的情况,我们可以不管。
• Make sure that all nodes have the same time and sync with NTP service! 当使用多台机器时,要保证不同机器时间的同步。原因为tomcat session复制的一些机制。具体原因看文档。
• Make sure that your loadbalancer is configured for sticky session mode. 保证负载均衡软件设置为session sticky模式。


四、 详细配置:
1. 修改tomcat7_a/conf/server.xml, 我们采用的是默认的配置,在<Engine>节点下添加:

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

          <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"
                      <!—tcpListenPort如果是同一机器部署两个tomcat7应用,则修改tomcat7_b为4001,以免冲突. 不同机器下,不用更改此项。—>
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      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"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

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


2. <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 修改,仅为了调试方便。
3. tomcat7_a\webapps\cluster\WEB-INF\web.xml中加入<distributable/>
4. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_a">,添加jvmRoute属性,此项为后面apache负载均衡用到。

五、 复制一份tomcat7_a应用,改名为tomcat7_b.只是为了图方便,实际应该复制的是前面的cluster工程。注意以下几点配置就ok.
1. 修改tomcat7_a/conf/server.xml 中的Server port属性<Server port="8006" shutdown="SHUTDOWN">,因为是同一台机器两个tomcat应用,所以改一下。
2. 修改<connector port="8082" protocol="HTTP/1.1" onnectionTimeout="20000" edirectPort="8443" />,同理,为了避免同一台机器端口号冲突。部置在不同的机器是不用管的。
3. 修改<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_b">,此项为后面apache负载均衡用到。
4. 修改<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto"port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
5. tomcat7_b\ebapps\cluster\WEB-INF\web.xml中加入<distributable/>


到此集群配置完成.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值