Apache+tomcat 负载集群配置
 1、httpd安装

    tar zxvf httpd-2.2.11.tar.gz
    cd httpd-2.2.11
    cd srclib/apr
    ./configure --prefix=/usr/local/apr
    make
    make install
    cd ../apr-util/
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    make
    make install
    cd ../../
    ./configure \
     --prefix=/usr/local/apache2 \
     --with-apr=/usr/local/apr \
     --with-apr-util=/usr/local/apr-util \
     --enable-rewrite \
     --enable-dav \
     --enable-so \
     --enable-mods-shared=all \
     --enable-ssl \
     --with-ssl=/usr/local/ssl \
     --enable-proxy=share \
     --enable-proxy-ajp=share \
     --enable-proxy-balancer=share \
     --enable-suexec \
     --with-suexec-caller=webusr \
     --with-suexec-docroot=/var/www \
     --enable-usertrack \
     --enable-http
    make
    make install
 2、 tomcat安装
 
   tar apache-tomcat-6.0.20.tar.gz
   cp  apache-tomcat-6.0.20 /usr/local/tomcat6020
   vi  tomcat6020/conf/catalina.sh
   添加:
    
    CATALINA_HOME=/usr/local/tomcat6020
    CATALINA_TMPDIR=/usr/local/tomcat6020/tmp
    JAVA_HOME=/usr/local/jdk
    JAVA_OPTS="-Xms256m -Xmx1024m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m"
   
 3、tomcat-connectors 安装 jk方式整合apache和tomcat
   
    a、JK 安装 (整合apache tomcat)
    # tar zxvf tomcat-connectors-1.2.28-src.tar.gz
    #cd tomcat-connectors-1.2.28-src/native
    # ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-java-home=/usr/local/jdk
    # make && make install
    
    在/usr/local/apache2/modules/ 下会产生mod_jk.so
    修改apache配置文件:
    LoadModule jk_module modules/mod_jk.so
    
    JkWorkersFile /usr/local/tomcat/conf/jk/workers.properties   JK配置文件 负载的配置
    JkShmFile logs/mod_jk.shm
    JkLogFile /usr/local/tomcat/logs/mod_jk.log  相关日志的设置
    JkLogLevel info
    JkMount /*.jsp controller  将jsp的请求转发给 controller(负载均衡控制器)
    
    修改DoucmentRoot 与tomcat的目录一致。
    
    b、调度器JK的配置
    建立JK配置文件:
    #vi /usr/local/tomcat/conf/jk/workers.properties
    workers.tomcat_home=/usr/local/tomcat    #指定tomcat的目录――如果2个tomcat在同一台机器该选项去掉
    workers.java_home=/usr/local/jdk         #指定jdk的目录
    ps=/                                    
    worker.list=controller                   #指定负载名,这个可以随便起,与下面的 worker.controller.type=lb要对应
    
    #========tomcat1======== 第一台tomcat的配置
    worker.tomcat1.port=8009
    worker.tomcat1.host=localhost   #这里也可以写IP
    worker.tomcat1.type=ajp13      
    worker.tomcat1.lbfactor=1       #权量 数值越大,分配的机率就最小
    
    #========tomcat2===========
    worker.tomcat2.port=9009
    worker.tomcat2.host=localhost  (tomcat2的IP)
    worker.tomcat2.type=ajp13
    worker.tomcat2.lbfactor=1
    
    #========controller,负载均衡器=======
    worker.controller.type=lb
    worker.controller.balance_workers=tomcat1,tomcat2  # server.xml配置文件里的jvmRoute="tomcat1"指定的名字
    worker.controller.sticky_session=1

 4、mod_proxy 方式整合apache和tomcat
 
    1.1、httpd.conf配置
    修改APACHE的配置文件 \conf\httpd.conf
    将以下Module的注释去掉,这里并没有使用mod_jk.so进行apache和tomcat的链接,从2.X以后apache自身已集成了mod_jk.so的功能。只需简单的把下面几行去掉注释,就相当于以前用mod_jk.so比较繁琐的配置了。这里主要采用了代理的方法,就这么简单。
    LoadModule proxy_module modules/mod_proxy.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
    LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    再找到<IfModule dir_module></IfModule>加上index.jsp修改成
    <IfModule dir_module>
    DirectoryIndex index.html index.jsp
    </IfModule>
    1.1.1、  在最下面加入
    ProxyRequests Off
    <proxy balancer://cluster>
    BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=jvm1
    BalancerMember ajp:// 127.0.0.1:9009 loadfactor=1 route=jvm2
    </proxy>
    上面的BalancerMember成员是我们配置的tomcat集群。
    1.2、httpd-vhosts.conf设置
    接下来进行虚拟主机的设置。APACHE的虚拟主机设置如下:
    首先要修改 conf/httpd.conf找到(#Include conf/extra/httpd-vhosts.conf)
    把注释去掉。
    # Virtual hosts
    Include conf/extra/httpd-vhosts.conf
    在文件(extra/httpd-vhosts.conf)最下面加入
    <VirtualHost *:80>
             ServerAdmin user@somehost.com
             ServerName localhost
             ServerAlias localhost
             ProxyPass /common !
             ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On
             ProxyPa***everse / balancer://cluster/
    </VirtualHost>
    其中的域名和路径根据你自己情况设置
    然后再设置TOMCAT虚拟主机
    
    
三、tomcat集群配置,session 同步配置:
    1、修改Engine节点信息:
     例如:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

    2、配置Cluster(每个tomcat中都要修改)
    原来的配置。
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    修改为以下的代码:<Receiver port=”XX”/>port也要保证唯一性。
    <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="5001"
    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>
    这个设置是主要用以tomcat的集群。
 
tomcat集群和负载均衡的实现(session同步)补充
因为tomcat的session同步功能需要用到组播,windows默认情况下是开通组播服务的,但是linux默认情况下并没有开通,可以通过指令打开route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0,如果需要服务器启动时即开通组播需在/etc/sysconfig/static-routes文件内加入eht0 net 224.0.0.0 netmask 240.0.0.0。具体组播概念请查阅CCNP相关内容。
 
tomcat 常用配置
 
    <Connector port="1800" protocol="HTTP/1.1"
               connectionTimeout="20000"
                maxThreads="500"
                minSpareThreads="16"
                maxSpareThreads="128"
                enableLookups="false"
                acceptCount="500" debug="0"
               redirectPort="8443"  URIEncoding="utf-8"/>