最近由于工作需要,公司需要部署 tomcat 集群,忙活了几天,终于配置了好了,

做个笔记以后备用。

1. 下载 apache2 源码 http2.4.6


2. 配置 apache2

./configure --prefix=/usr/local/apache2 --enable-modules=shared --enable-mods-shared=all \
--enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http \
--enable-proxy-ajp --enable-proxy-balancer --enable-rewrite \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--with-pcre=/usr/local/pcre

其中 apr ,apr-util, pcre 是安装依赖包,需要提前安装

具体安装步骤见 http://now51jq.blog.51cto.com/3474143/1317581


3. 安装 apache2

root@qlserver03:/home/ryanwang/http2.4.6# make && make install


4 配置 load balance

修改 httpd.conf,文件末尾插入如下内容

ProxyRequests Off
<Proxy balancer://mycluster>
    BalancerMember http://192.168.1.202:8080  loadfactor=1 route=jvm1
    BalancerMember http://192.168.1.203:8080  loadfactor=1 route=jvm2
</Proxy>
ProxyPass / balancer://mycluster/
ProxyPa***everse / balancer://mycluster/

正向代理需要关闭,主要是为了安全,

ProxyPass / balancer://mycluster/” 表示所有请求都交给 mycluster 来处理。

ProxyPa***everse 防止内部请求重定向会绕过apache2,这句需要加上。


5 配置 tomcat7

修改 tomcat/conf/server.xml

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


另外一台tomcat 服务器配置成 jvmRoute="jvm2"


6 应用的web.xml 文件需要增加一个元素 <distributable/>

<web-app>
    <distributable/>
                                                                                                               
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>



到此 tomcat集群load balance 已经配置完成。

apache http server + tomcat 已经可以正常使用了,启动相关服务进行验证,

验证通过再进行下面的session 复制配置。

有可能会报错,不用当心,查看 apache2/logs/error_log 文件,里面会提示错误信息。

这是由于loadbalance 相关模块没有打开,修改 httpd.conf

LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so



二 下面配置 session 复制


<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
         channelSendOptions="8">
        <Manager className="org.apache.catalina.ha.session.BackupManager"
           expireSessionsOnShutdown="false"
           notifyListenersOnReplication="true"
           mapSendOptions="6"/>
  <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="192.168.1.202"
              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>


其中修改把 manager元素的

<Manager className="org.apache.catalina.ha.session.DeltaManager"

修改为

<Manager className="org.apache.catalina.ha.session.BackupManager"

表示只有部署了相同应用的tomcat之间才复制session。


修改 receiver IP 地址和端口。

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
          address="192.168.1.202"
          port="4000"
          autoBind="100"
          selectorTimeout="5000"
          maxThreads="6"/>



由于是安装完成了再写的博文,细节可能疏忽,欢迎大家批评指正。