ajp方式实现Apache负载均衡+Tomcat集群

本文详细介绍了如何在一台机器上安装并配置Apache作为HTTP服务器,以及两个Tomcat应用实例,通过Apache实现负载均衡,使得多个Tomcat应用能够高效地运行并分配请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:
使用 apache 和 tomcat 配置一个可以应用的 web 网站,要达到以下要求:
Apache 做为 HttpServer ,后面连接多个 tomcat 应用实例,并进行负载均衡。
注:本例程以一台机器为例子,即同一台机器上装一个apache和2个Tomcat。
一、准备工作,下载以下软件
JDK1.5+
APAHCE 2.2.8
Tomcat-7.0.41
二、安装过程
安装Apache
D:\Program Files (x86)\Apache Software Foundation\Apache2.2
解压tomcat
D:\Tomcat-7.0.41-1
D:\Tomcat-7.0.41-2
三、配置
1、Apache配置
1.1、httpd.conf配置
修改APACHE的配置文件Apache2.2\conf\httpd.conf
将以下Module的注释去掉
#apache tomcat负载均衡
LoadModule proxy_module modules/mod_proxy.so
#apache tomcat负载均衡
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#apache tomcat负载均衡
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#apache tomcat负载均衡
LoadModule proxy_connect_module modules/mod_proxy_connect.so
#apache tomcat负载均衡
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#apache tomcat负载均衡
LoadModule proxy_http_module modules/mod_proxy_http.so
再找到<IfModule dir_module></IfModule>加上index.jsp修改成
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
 
去掉如下代码的注释
Include conf/extra/httpd-vhosts.conf
最下面添加
#tomcat集群
ProxyRequests Off
ProxyPass / balancer://example/
<proxy balancer://example>
BalancerMember ajp://127.0.0.1:8010 loadfactor=1 route=jvm1
BalancerMember ajp://127.0.0.1:8011 loadfactor=1 route=jvm2
</proxy>
 
1.2、httpd-vhosts.conf设置
接下来进行虚拟主机的设置。修改Apache2.2\conf\extra\httpd-vhosts.conf
 
<VirtualHost *:80>
    ServerAdmin 506110230@qq.com
    ServerName localhost
	ServerAlias localhost
    ProxyPass / balancer://example/ stickysession=jsessionid nofailover=On
    ProxyPassReverse / balancer://example/
	ErrorLog "logs/loadbalancer-error.log"
    CustomLog "logs/loadbalancer-access.log" common
</VirtualHost>
 
配置 tomcat
2.1.  配置 server 的关闭
 tomcat1
<Server port="8006" shutdown="SHUTDOWN">
 tomcat2
<Server port="8007" shutdown="SHUTDOWN">
 
2.2. 配置 Engine
 tomcat1
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
 tomcat2
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">
 
2.3. 配置 Connector
 tomcat1
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
 tomcat2
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
 
2.5.配置Cluster
 tomcat1(port号是重点)
<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"/>

            <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"/>
          </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>
 tomcat2(port号是重点)
<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="4001"
                      autoBind="100"
                      selectorTimeout="5000"
                      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"/>
          </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>
 
四、启动服务,测试tomcat
1、测试apache和tomcat协作。
先在每个tomcat中的\webapps\ROOT下的index.jsp下面加上以下的测试代码部分:
<%@ page language="java"%> 
<%@ page contentType="text/html; charset=utf-8"%> 
<%@ page import="java.util.*"%>

<% 
System.out.println(new Date().getTime());
%>
 浏览器分别刷新:
http://127.0.0.1:8081/
http://127.0.0.1:8082/
http://127.0.0.1/
观察后台输出

 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值