日期:2014-05-16  浏览次数:20928 次

Apache和Tomcat实现负载均衡
Apache、Tomcat实现负载均衡
apache和tomcat的整合主要有三种方式(1、JK,2、http_proxy,3、ajp_proxy)
本文主要通过JK方式进行整合
JK是通过 AJP 协议与 Tomcat 服务器进行通讯,Tomcat 默认的 AJP Connector 的端口是8009。
1、修改Tomcat配置(假设有2个Tomcat,server.xml)
      a>配置Tomcat的关闭端口
            <Server port="8005" shutdown="SHUTDOWN"> 假设第一个8055、第二个位8065
      b>配置AJP通信端口
         <!-- Define an AJP 1.3 Connector on port 8009 -->
            <Connector port="8009"  enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
            两个tomcat端口分别为8019、8029
      c>配置Http端口
         <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
            <Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/>
               两个tomcat Http端口分别为8060、8070
      d>配置Engine jvmRoute 属性
         <!-- Define the top level container in our container hierarchy -->
            <Engine name="Catalina" defaultHost="localhost" jvmRoute="XXX">
               两个tomcat对应的jvmRoute分别为ts1、ts2
2、引入mod_jk模块修改apache配置(httpd.conf)
       a>引入mode_jk
            下载mode_jk.so并拷贝到modules\ 目录下
       b>新建workers.properties文件(Tomcat定义文件)位于conf目录下;如下示例
                #
                # workers.properties
                #
                # list the workers by name
                worker.list=DLOG4J, status
                # localhost server 1
                worker.ts1.port=8019    #ajp13 端口号,在tomcat下server.xml配置,默认8009
                worker.ts1.host=localhost   #tomcat的主机地址,如不为本机,请填写ip地址
                worker.ts1.type=ajp13
                worker.ts1.lbfactor=1   #server的加权比重,值越高,分得的请求越多
                worker.ts1.connection_pool_size=1500