您的位置:首页 > 运维架构 > Tomcat

Apache 负载均衡 + tomcat群集

2011-03-07 10:15 253 查看

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的安装

安装过程省略

环境变量的设置:

我的电脑——属性——高级——环境变量——

添加 CLASS_PATH=C:\Java\jdk1.6.0_13

JAVA_HOME=C:\Java\jdk1.6.0_13

PATH=C:\Java\jdk1.6.0_13\bin





测试java是否安装正确:

运行里面输入cmd——java 或者javac 或有参数出来





或者,会出来帮助信息





3.正式开始安装:

apache的安装:

下载apache软件包:

Win32 Binary without crypto (no mod_ssl) (MSI Installer): apache_2.2.13-win32-x86-no_ssl.msi

官方下载链接地址:
http://labs.xiaonei.com/apache-mirror/httpd/binaries/win32/apache_2.2.13-win32-x86-no_ssl.msi
个人习惯,我在c盘根下建立个webserver文件。里面放apache和tomcat程序。
安装过程省略。安装好后,apache安装在C:\webserver\httpd
配置负载均衡服务器Apache。
把这个模块的注释去掉
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://192.168.1.117:9080 BalancerMember http://192.168.1.117: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://192.168.1.117:9080 BalancerMember http://192.168.1.117: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
<Proxy balancer://mycluster>
BalancerMember http://192.168.1.117:9080/
BalancerMember http://192.168.1.117:10080/
</Proxy>
启动apache服务:
Apache启动成功





Tomcat集群的配置:

1. 下载tomcat的软件包:下载解压版本
下载地址:
http://apache.freelamp.com/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip
解压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" />

1. <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里面添加网页

Tomcat1上的\webapps\ROOT\index.html内容如下:

<html>

<head><title>Tomcat 群集1</title></head>

<body>

<center>Tomcat 1号机在tomcatA机上

</center>

</body>

</html>

Tomcat2上的webapps\ROOT\index.html内容如下:

<html>

<head><title>Tomcat 群集2</title></head>

<body>

<center>Tomcat 2号机在TomcatB机上

</center>

</body>

</html>

启动Tomcat1和Tomcat2,再启动Apache





点击刷新,页面马上会跳到第二台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/。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: