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

Apache + Tomcat 负载均衡与集群配置
Apache + Tomcat 负载均衡与集群配置
-------------------------------------

1. 准备
下载 Apache Http Server

http://labs.renren.com/apache-mirror//httpd/binaries/win32/httpd-2.2.19-win32-x86-no_ssl.msi

下载 Tomcat

http://apache.etoak.com/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32-windows-x86.zip

2. 安装 Apache Http Server

安装完成后, 配置 conf/httpd.conf
2.1 配置HTTP端口:
Listen 80


2.2 配置负载均衡, 去掉注释符#:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.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_scgi_module modules/mod_proxy_scgi.so


2.3 配置虚拟主机, 去掉注释符#:
Include conf/extra/httpd-vhosts.conf

2.3 配置首页
<IfModule dir_module>
    DirectoryIndex index.htm index.html index.jsp
</IfModule>


2.4 配置负载均衡, 在最后面加上:
ProxyRequests Off
<proxy balancer://cluster>
	BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat1
	BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=tomcat2
	ProxySet lbmethod=byrequests timeout=15
</proxy>


2.5 配置 conf/extra/httpd-vhosts.conf
2.5.1 注释所有虚拟主机节点:
2.5.2 添加虚拟主机, 注意格式:
<VirtualHost *:80>
         ServerAdmin amos_tl@126.com
         ServerName localhost
         ServerAlias localhost
         ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=Off
         ProxyPassReverse / balancer://cluster/
</VirtualHost>


3. 安装 Tomcat
解压后, 将目录取名 tomcat1, 再复制一份取名tomcat2.

3.1 配置tomcat1:
配置 tomcat1/conf/server.xml
3.1.1 配置负载均衡路由, 增加 jvmRoute="tomcat1" :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">


3.1.2 配置集群, 去掉集群节点的注释符
3.1.3 配置端口, 去掉注释符:
<!-- See proxy documentation for more information about using this. -->
    <Connector port="8082" 
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" acceptCount="100" connectionTimeout="20000"
               proxyPort="80" disableUploadTimeout="true" />



3.2 配置tomcat2:
配置 tomcat2/conf/server.xml
3.2.1 配置负载均衡路由, 增加 jvmRoute="tomcat2" :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">


3.2.2 配置集群, 去掉集群节点的注释符, 修改Cluster|Receiver|tcpListenPort为4002.
<Receiver 
                className="org.apache.catalina.cluster.tcp.ReplicationListener"
                tcpListenAddress="auto"
                tcpListenPort="4002"
                tcpSelectorTimeout="100"
                tcpThreadCount="6"/>


3.2.3 修改相关端口,避免与tomcat1 端口冲突:
<Server port="9005" shutdown="SHUTDOWN">

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="9080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" />

<!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9009" 
               enableLookups="false" redirectPort="9443" protocol="AJP/1.3" />

    <!-- Define a Proxied HTTP/1.1 Connector on port 8082 -->
    <!-- See proxy documentation for more information abo