Apache+Tomcat集群

 一、Apache和Tomcat安装

1. 下载Apache集群服务器

地址:http://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-openssl-0.9.8y.msi

2. 安装Apache集群服务器

Apache+Tomcat集群


Apache+Tomcat集群
 
Apache+Tomcat集群
 
Apache+Tomcat集群
 
Apache+Tomcat集群
 
Apache+Tomcat集群
 
Apache+Tomcat集群

看到右下角绿色的三角图标,为apache安装并启动成功


Apache+Tomcat集群

Apache+Tomcat集群
 

3. 下载Tomcat 7进行解压安装

http://mirrors.shuosc.org/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.zip

解压到tomcat1和tomcat2两个文件夹中,内容完全一样
Apache+Tomcat集群

二、Apache配置
1.加载proxy模块

配置Apache,打开apache配置文件:%APACHE_HOME%/conf/httpd.conf

由于已经将所有模块加载,所以不用在配置中做修改,去掉以下行首的#注释
LoadModule proxy_module modules/mod_proxy.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

 2.配置虚拟主机proxyblancer

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName  localhost
    ErrorLog    "C:/app1_error.log"
    CustomLog   "C:/app1_access.log" common

    ProxyPass /   balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On lbmethod=byrequests timeout=5 maxattempts=3
    ProxyPassReverse / balancer://cluster/

    ProxyRequests Off
    ProxyPreserveHost On

    <proxy balancer://cluster>
        BalancerMember ajp://127.0.0.1:6009 route=tomcat_a
        BalancerMember ajp://127.0.0.1:8009 route=tomcat_b
    </proxy>
</VirtualHost>

三、修改tomcat1和tomcat2的配置

1. 修改tomcat1的conf/server.xml文件

<Server port="6005" shutdown="SHUTDOWN">
    <Connector port="6080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="6443" />
    <Connector port="6009" protocol="AJP/1.3" redirectPort="6443" />

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat_a">
           <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    </Engine>
</Server>

2. 修改tomcat2的conf/server.xml文件

<Server port="8005" shutdown="SHUTDOWN">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat_b">
           <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    </Engine>
</Server>

四、编辑测试文件

1. 在tomcat1中添加测试文件%TOMCAT_ROOT%/webapps/ROOT/test.jsp

test.jsp内容是:

this is tomcat1

2. 在tomcat2中添加测试文件%TOMCAT_ROOT%/webapps/ROOT/test.jsp

test.jsp内容是:

this is tomcat2

五、测试

浏览器中输入地址:

http://localhost:80/test.jsp

显示内容有时为this is tomcat1,有时为this is tomcat2,即为成功

相关推荐