apache2.2.14 + Tomcat6.0.14/Tomcat6.0.20配置负载均衡

apache2.2.14+Tomcat6.0.14/Tomcat6.0.20配置负载均衡

关键字:apachetomcat负载均衡

Apache做为HttpServer,后面连接多个tomcat应用实例,并进行负载均衡

1.安装JDK6.0

2.安装Tomcat6.0.14和Tomcat6.0.20

3.下载并安装ApacheHTTPServer2.2.14,下载地址:http://hitech.onlinedown.net/down/apache_2.2.14-win32-x86-no_ssl.zip

此处注意,如果你本机已经装了iis并且占用80端口,务必先在服务中将iis停止或禁用。软件默认安装即可。安装过程中设置NetworkDomain和ServerName为localhost,设置你的email地址,下方会让你选择onlyforthecurrentuser或forallusers。默认选择forallusers。这样Apache就会占用80端口,并且做为一个系统服务开机自运行。

安装完成之后,你在浏览器中输入http://localhost,将会看到Apache的成功页面“Itworks!”

4..配置Apache:

修改Apache配置文件\Apache2.2\conf\httpd.conf

1)将以下6个Module的注释去掉:

LoadModuleproxy_modulemodules/mod_proxy.so

LoadModuleproxy_ajp_modulemodules/mod_proxy_ajp.so

LoadModuleproxy_balancer_modulemodules/mod_proxy_balancer.so

LoadModuleproxy_connect_modulemodules/mod_proxy_connect.so

LoadModuleproxy_ftp_modulemodules/mod_proxy_ftp.so

LoadModuleproxy_http_modulemodules/mod_proxy_http.so

apache2.x以前配置apache和tomcat可能需要用到mod_jk.so进行apache和tomcat的链接。apache2.x中集成了mod_jk.so的功能,只需简单的把上面6个module的注释打开。

2)在DirectoryIndex后面增加index.jsp,改完以后是:

Xml代码

<IfModuledir_module>

DirectoryIndexindex.htmlindex.jsp

</IfModule>

<IfModuledir_module>

DirectoryIndexindex.htmlindex.jsp

</IfModule>

3)在文件httpd.conf的最后加入以下内容:

Xml代码

ProxyRequestsOff

<proxybalancer://cluster>

BalancerMemberajp://127.0.0.1:8010loadfactor=1route=jvm1

BalancerMemberajp://127.0.0.1:8009loadfactor=1route=jvm2

</proxy>

ProxyRequestsOff

<proxybalancer://cluster>

BalancerMemberajp://127.0.0.1:8010loadfactor=1route=jvm1

BalancerMemberajp://127.0.0.1:8009loadfactor=1route=jvm2

</proxy>

4)设置apache2.2虚拟主机

a>.先在conf\httpd.conf中引入配置虚拟主机的控制文件,将配置文件中默认注释掉的配置信息放开就可以了:

#Virtualhosts

Includeconf/extra/httpd-vhosts.conf

b>.修改conf/extra/httpd-vhosts.conf文件,在最下面加入虚拟主机的配置信息

Xml代码

<VirtualHost*:80>

ServerNamelocalhost

ServerAliaslocalhost

ProxyPass/balancer://cluster/stickysession=jsessionidnofailover=On

ProxyPassReverse/balancer://cluster/

</VirtualHost>

<VirtualHost*:80>

ServerNamelocalhost

ServerAliaslocalhost

ProxyPass/balancer://cluster/stickysession=jsessionidnofailover=On

ProxyPassReverse/balancer://cluster/

</VirtualHost>

其中的域名和路径的值依照安装apachehttpserver2.2时设置的域名和路径,保持一致即可。

5.配置tomcat

1).配置tomcat的启动和关闭端口

若是在同一台机器上同时运行多个tomcat,则需要修改tomcat的启动端口和关闭端口。

a>.启动端口修改:将每个tomcat的server.xml文件作如下修改

Xml代码

<Connectorport="8080"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

<Connectorport="8080"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

改为

Xml代码

<Connectorport="xxxx"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

<Connectorport="xxxx"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

如果需要用到tomcat的共享线程池,其端口值也设置为xxxx,保持一致(线程池缺省是被注释掉的):

Xml代码

<Connectorexecutor="tomcatThreadPool"

port="xxxx"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

<Connectorexecutor="tomcatThreadPool"

port="xxxx"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

b>.设置tomcat的关闭端口:

将<Serverport="8005"shutdown="SHUTDOWN">修改为

<Serverport="yyyy"shutdown="SHUTDOWN">每个tomcat的端口yyyy不同就行。

2).配置engine

tomcat默认的engine配置为:

<Enginename="Catalina"defaultHost="localhost">

为了通过AJP来支持负载均衡,需要配置engine的jvmRoute属性。将server.xml的默认engine配置注释掉,按给出的jvmRoute示例做如下配置:

<Enginename="Standalone"defaultHost="localhost"jvmRoute="jvm1">

注:关于engine的name属性值的设置,不同版本的tomcat里面server.xml中给出的配置例子不同,比如tomcat6.0.14中的例子为:

Xml代码

<!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:

<Enginename="Catalina"defaultHost="localhost"jvmRoute="jvm1">

-->

<!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:

<Enginename="Catalina"defaultHost="localhost"jvmRoute="jvm1">

-->

tomcat6.0.20中例子为:

Xml代码

<!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:

<Enginename="Standalone"defaultHost="localhost"jvmRoute="jvm1">

-->

<!--YoushouldsetjvmRoutetosupportload-balancingviaAJPie:

<Enginename="Standalone"defaultHost="localhost"jvmRoute="jvm1">

-->

一般按照tomcat中给的例子的name属性值设置即可(Catalina或Standalone或aaa,任意都行),新安装的tomcat在第一次启动后,将在tomcat的\conf\目录下创建一个与engine中name属性值一样的文件夹。

不过在我配置时发现6.0.14版本的tomcat在启动后并没有在\conf\目录下新建文件夹,而且查看启动日志也没发现异常。6.0.14版的tomcat是安装exe的形式注册服务的,6.0.20版的tomcat是解压然后手动注册服务的,安装形式不同,可能这是这个地方存在差异的原因吧。

3)配置AJPConnector

将server.xml中的缺省配置

Xml代码

<Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/>

<Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/>

修改为

Xml代码

<Connectorport="zzzz"protocol="AJP/1.3"redirectPort="8443"/>

<Connectorport="zzzz"protocol="AJP/1.3"redirectPort="8443"/>

port的值zzzz与apache2.2中的文件httpd.conf中<proxybalancer://cluster></proxy>之间配置的均衡器成员的端口一致,一一对应;上面在配置的jvmRoute的值(如jvm1)应该与httpd.conf中<proxybalancer://cluster></proxy>之间配置的均衡器成员的route属性的值一致,且必须唯一。

4)配置Cluster

打开tomcat的server.xml,默认的Cluster配置如下,缺省被注释掉了:

<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

如何配置<Cluster,可以参考\webapps\docs\cluster-howto.html,直接将说明文档中“ConfigurationExample”后面的一段复制过来,即可满足基本的使用,内容如下:

Xml代码

<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"

channelSendOptions="6">

<Managerclassname="org.apache.catalina.ha.session.BackupManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"

mapSendOptions="6"/>

<!--

<Managerclassname="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

-->

<Channelclassname="org.apache.catalina.tribes.group.GroupChannel">

<Membershipclassname="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.4"

port="45564"

frequency="500"

dropTime="3000"/>

<Receiverclassname="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="auto"

port="5000"

selectorTimeout="100"

maxThreads="6"/>

<Senderclassname="org.apache.catalina.tribes.transport.ReplicationTransmitter">

<Transportclassname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

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

</Channel>

<Valveclassname="org.apache.catalina.ha.tcp.ReplicationValve"

filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

<Deployerclassname="org.apache.catalina.ha.deploy.FarmWarDeployer"

tempDir="/tmp/war-temp/"

deployDir="/tmp/war-deploy/"

watchDir="/tmp/war-listen/"

watchEnabled="false"/>

<ClusterListenerclassname="org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>

<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"

channelSendOptions="6">

<Managerclassname="org.apache.catalina.ha.session.BackupManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"

mapSendOptions="6"/>

<!--

<Managerclassname="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

-->

<Channelclassname="org.apache.catalina.tribes.group.GroupChannel">

<Membershipclassname="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.4"

port="45564"

frequency="500"

dropTime="3000"/>

<Receiverclassname="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="auto"

port="5000"

selectorTimeout="100"

maxThreads="6"/>

<Senderclassname="org.apache.catalina.tribes.transport.ReplicationTransmitter">

<Transportclassname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

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

</Channel>

<Valveclassname="org.apache.catalina.ha.tcp.ReplicationValve"

filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

<Deployerclassname="org.apache.catalina.ha.deploy.FarmWarDeployer"

tempDir="/tmp/war-temp/"

deployDir="/tmp/war-deploy/"

watchDir="/tmp/war-listen/"

watchEnabled="false"/>

<ClusterListenerclassname="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>

<ClusterListenerclassname="org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>

若启动时出现这样的错误提示:

2009-11-1713:02:55org.apache.catalina.ha.deploy.FarmWarDeployerstart

严重:FarmWarDeployercanonlyworkashostclustersubelement!

则将以下部分注释掉即可:

Xml代码

<!--

<Deployerclassname="org.apache.catalina.ha.deploy.FarmWarDeployer"

tempDir="/tmp/war-temp/"

deployDir="/tmp/war-deploy/"

watchDir="/tmp/war-listen/"

watchEnabled="false"/>

-->

相关推荐