【Java Web】apache+tomcat集群实现负载均衡

一步步按照流程实现Apache 负载均衡 + tomcat群集的步骤:


1.环境介绍


操作系统环境 :windows xp sp3 (一台机器上跑 2个或多个tomcat服务)
Java环境:   jdk1.6.0_13
软件:apache_2.2.13-win32-x86-no_ssl.msi
      apache-tomcat-6.0.20.zip


2.jdk的安装
安装过程省略
环境变量的设置


3.正式开始安装:
apache的安装:
下载apache软件包:apache_2.2.13-win32-x86-no_ssl.msi

个人习惯,我在c盘根下建立个webserver文件。里面放apache和tomcat程序。
安装好后,apache安装在C:\webserver\httpd


配置负载均衡服务器Apache。
把这个模块的注释去掉(conf/httpd.conf)
LoadModule proxy_module modules/mod_proxy.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_ajp_module modules/mod_proxy_ajp.so 
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so


再找到<IfModule dir_module></IfModule>加上index.jsp修改成


<IfModule dir_module>
DirectoryIndex index.html index.jsp
</IfModule>


在配置文件的末尾加上:
ProxyRequests Off
ProxyPass / balancer://mycluster stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://127.0.0.1:9080
BalancerMember http://127.0.0.1:10080
</Proxy>


<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>


<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from all
</Location>






说明:
ProxyPass / balancer://mycluster/ stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://127.0.0.1:9080
BalancerMember http://127.0.0.1:10080
</Proxy>


ProxyPass为代理转发的Url,即将所有访问/ (这里的/ 指的是apache默认的htdoc的主目录)的请求转发到群集balancer://mycluster
BalancerMember为群集的成员,即群集服务器A或B,负载均衡服务器会根据均衡规则来将请求转发给BalancerMember。


注:如果你的网页主目录下面的index.jsp要连接其他的子链接的话,这里的连接方式应该这样写
不然访问网页的时候会报错
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, ldtrain@163.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.


修改方法:
ProxyPass / balancer://mycluster/ stickysession=jsessionid nofailover=On(ProxyPass / balancer://mycluster后面多个斜杠/)
<Proxy balancer://mycluster>
BalancerMember http://127.0.0.1:9080/
BalancerMember http://127.0.0.1:10080/
</Proxy>


启动apache服务:
Apache启动成功




Tomcat集群的配置:


1.下载tomcat的软件包:下载解压版本
解压tomcat 到c盘根目录,重命名为tomcat1,然后复制tomcat1为tomcat2.


2. 配置群集服务器tomcat1号服务器
端口规划:
配置参数     群集服务器A(tomcat1)     群集服务器B(tomcat2)


Server port   9005                           10005


Connector     9080                           10080


Coyote AJP Connector         9009             10009


Cluster mcastAddr         224.0.0.1           224.0.0.1


Cluster mcastPort           45564             45564


tcpListenAddress         本机ip地址         本机ip地址


Cluster tcpListenPort         5001             5002




修改server.xml配置文件
Tomcat1中修改的部分:
1.<Server port="9005" shutdown="SHUTDOWN">


2. <Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


3.<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />


4. <Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">


———————————–添加的部分———————————–


添加在<Host></Host>之间


<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="224.0.0.1"
port="45564"
frequency="500"
dropTime="3000"/>


<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="5001"
selectorTimeout="100"
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"/>


<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>


</Channel>


<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>


<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.ClusterSessionListener"/>
</Cluster>
</Host>




Tomcat2中修改的部分:
1.<Server port="10005" shutdown="SHUTDOWN">


2.<Connector port="10080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


3.<Connector port="10009" protocol="AJP/1.3" redirectPort="8443" />


4. 添加在<Host></Host>之间
<Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>


<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="5002"
selectorTimeout="100"
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"/>


<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>


<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>


<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.ClusterSessionListener"/>


</Cluster>


</Host>


集群测试
在2个tomcat的webapps目录下放web工程a
启动Tomcat1和Tomcat2,再启动Apache
访问:http://127.0.0.1/a/index.jsp
点击刷新,页面马上会跳到第二台tomcat服务器上


注:如果访问网页出现拒绝访问的情况,
Forbidden 
You don't have permission to access /~web on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.


原来是httpd.conf文件中的权限访问控制设置不对,具体是以下这段:
<Directory /你的web主目录的位置>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
其中的Deny from all应该设置成Allow from all,这是针对根目录/的一个设置,因为我的自定义目录是/myweb/。


**************************************************************************************************************************
也可以如下配置:
httpd/conf/httpd.conf 修改如下:
ProxyPass / balancer://mycluster/ stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://127.0.0.1:9080/a loadfactor=1 route=jvm1 
BalancerMember http://127.0.0.1:10080/a loadfactor=1 route=jvm2
</Proxy>


tomcat/conf/server.xml中分别修改如下
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1"> 
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm2"> 


启动Tomcat1和Tomcat2,再启动Apache
访问:http://127.0.0.1/index.jsp






C:\WINDOWS\system32\drivers\etc\hosts
在测试机的host 文件里指定www.test.com 对应为127.0.0.1,然后在浏览器里访问:http:// www.test.com























































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值