q989 发表于 2017-1-8 12:00:12

apache+tomcat 负载均衡+session绑定 配置集群环境

  其中负载均衡由Apache2.2.4担任 内部服务器为Tomcat 6
  实验中本机运行1个Apache与两个Tomcat 6
  当然 目前只知道怎么配置 能大概理解部分配置文件属性 具体配置文件的所有含义还不明白……
  安装好Apache与Tomcat后开始配置
  
1 下载Tomcat Connector(jk.1.2.6)
  Tomcat Connector下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.26/
此处注意一定要下载与Apache版本匹配的jk。jk文件名后半部分名称反应Apache的版本,如:mod_jk-1.2.26-httpd-2.2.4.so其匹配的Apache为2.2.4,与本次安装的Apache版本一致。
 
  
2 Apache配置
 
       把下载的 mod_jk-1.2.28-httpd-2.2.3.so 改名为 mod_jk2.so 放到apache的modules目录下(不改名不行么?不知道为什么我的没改名,就出问题了,当然我的配置文件中也是mod_jk-1.2.28-httpd-2.2.3.so 但是就是不知道为什么会有问题),修改apache的conf目录下的httpd.conf,在文件最后加入一行include conf\mod_jk2.conf,在conf目录下创建mod_jk2.conf文件,内容如下:
  # Load mod_jk2 module   
  LoadModule jk_module modules/mod_jk2.so   
  
  # Where to find workers.properties   
  JkWorkersFile conf/workers.properties    
  
  # Where to put jk logs   
  JkLogFile logs/mod_jk2.log   
  
  # Set the jk log level    
  JkLogLevel info   
  
  # Select the log format   
  JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "  
  
  # JkOptions indicate to send SSL KEY SIZE,   
  JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories   
  
  # JkRequestLogFormat set the request format   
  JkRequestLogFormat "%w %V %T"  
  
  # Send JSPs for context / to worker named loadBalancer   
  JkMount /*.jsp loadBalancer  
  
2  jk2配置
   jk2的配置全在一个配置文件中,文件名为workers.properties,和apache 的httpd.conf放在同一个目录下。以下是这个文件的内容:
#  
# workers.properties  
#  
 
# list the workers by name  
 
worker.list=tomcat1, tomcat2, loadBalancer  
 
# ------------------------  
# First tomcat server  
# ------------------------  
worker.tomcat1.port=8009 
worker.tomcat1.host=192.168.2.203 
worker.tomcat1.type=ajp13  
 
# Specify the size of the open connection cache.  
#worker.tomcat1.cachesize  
 
#  
# Specifies the load balance factor when used with  
# a load balancing worker.  
# Note:  
# ----> lbfactor must be > 0 
# ----> Low lbfactor means less work done by the worker.  
#分发权重 值越大负载越大 测试时可以通过控制分发权值定负载顺序 测试session绑定是否成功
worker.tomcat1.lbfactor=9 
 
 
# ------------------------  
# Second tomcat server  
# ------------------------  
worker.tomcat2.port=9009 
worker.tomcat2.host=192.168.2.203
worker.tomcat2.type=ajp13  
 
# Specify the size of the open connection cache.  
#worker.tomcat2.cachesize  
 
#  
# Specifies the load balance factor when used with  
# a load balancing worker.  
# Note:  
# ----> lbfactor must be > 0 
# ----> Low lbfactor means less work done by the worker.  
worker.tomcat2.lbfactor=100 
 
 
# ------------------------  
# Load Balancer worker  
# ------------------------  
 
#  
# The loadbalancer (type lb) worker performs weighted round-robin  
# load balancing with sticky sessions.  
# Note:  
# ----> If a worker dies, the load balancer will check its state  
# once in a while. Until then all work is redirected to peer  
# worker.  
worker.loadBalancer.type=lb  
worker.loadBalancer.balanced_workers=tomcat1, tomcat2  
 
#  
# END workers.properties  

 
  
3 配置tomcat(绑定session)
Tomcat配置文件Service.xml主要注意两个地方,一个是Engine节点需要增加节点标识jvmRoute,一个是将原本注释掉的Session复制节点改为有效。具体如下:
 
<!-- Define the top level container in our container hierarchy -->
<!--jvmRoute在各个Tomcat配置中不能重复且要与worker.properties文件中的名称一致-->
<Engine name="Catalina" defaultHost=" saas "  jvmRoute=" tomcat1">
<!—每个Tomcat的Hostname必须一样-->
<Host name="saas" appBase="webapps "
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
 
到此为止,负载均衡配置完毕
 
这里省略N多注释
 
将下面的注释变去掉(session复制需做以下操作)
<!--
        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    
              <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
-->
增加:
<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" 
                      port="4000" 
                      autoBind="100" 
                      selectorTimeout="5000" 
                      maxThreads="6"/>  
            <!-- timeout="60000"-->  
            <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=""/>  
          <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>
 
 
<!—Host节点增加一下内容表示站点根路径-->
        <Context path="/sc" docBase="." privileged="true"/>
 
 
我们分别将两个Tomcat配置文件中的jvmRoute设置为tomcat1、tomcat2,Server节点 端口分别配置为8005和9005, Connector节点端口分别配置为8080和9090,AJPConnector端口分别配置为8009和9009,Connector端口配置参照单主机多站点场景。请注意两个Tomcat配置文件Host节点的域名配置必须一样,Server.xml中的jvmRoute名称必须和worker.properties中的tomcat实例名称一致,不然无法实现session_stricky。
 
Host节点的域名配置方法:修改C:\WINDOWS\system32\drivers\etc 下的 hosts文件增加一行
127.0.0.1       saas
 
*****************************************************************************
 实现session 复制 需要在web.xml 中加上<distributable/>
<?xml version="1.0" encoding="ISO-8859-1"?>
省略N多代码。。。。。。
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
<distributable/>
</web-app>
*****************************************************************************
页: [1]
查看完整版本: apache+tomcat 负载均衡+session绑定 配置集群环境