一、Apache HTTP Server 与 Tomcat 的四种连接方式
- JK
- http_proxy
- ajp_proxy
- mod_cluster
四者比较:
相对于 JK 的连接方式,http_proxy、ajp_proxy、mod_cluster在配置上相对简单,灵活性也更高。但就稳定性而言就不像 JK 这样久经考验,但是随着时间的推移,使用mod_cluster的互联网网站也越来越多。
二、使用mod_cluster的好处
-
httpd workers 可动态配置
应用服务器依赖于lifecyle事件(server startup/shutdown)通过adverties广播机制通知httpd来动态配置
-
Server端负载均衡因子计算
通过在Server端计算负载均衡因子,本文指Tomcat负责计算,通过将计算放置在Server端,mod_cluster可以提供比proxy端更加健壮、精确的负载均衡
-
更细粒度的web-app生命周期控制
web-app在deploy/undeploy时,Server端会自动通知httpd去新增/删除该web-app的context路由
-
支持多种协议(HTTP、HTTPS、AJP)
不像mod_jk、mod_proxy、ajp_proxy模块仅支持单一协议
三、物理部署图
四、安装文件准备
- apache:httpd-2.2.22-win32-x86-openssl-0.9.8t.msi
- apache mod_cluster模块:mod_cluster-1.2.6.Final-windows-x86.zip
- tomcat:apache-tomcat-7.0.53.tar.gz
- tomcat mod_cluster集成:mod_cluster-parent-1.2.6.Final-bin.tar.gz
- jdk7:jdk-7u79-windows-x64.exe
注: mod_cluster模块必须与apache版本对应,否则可能造成apache无法正常启动。
五、Apache httpd mod_cluster 模块 安装配置
-
apache安装:
直接双击下载的 httpd-2.2.22-win32-x86-openssl-0.9.8t.msi 文件安装即可,这里我们安装到 D:\Apache2.2 目录下
-
解压下载的 mod_cluster-1.2.6.Final-windows-x86.zip 文件,得到 httpd-2.2 目录,复制 httpd-2.2\modules 目录下的下面4个so文件到 D:\Apache2.2\modules 目录下
- mod_advertise.so
- mod_manager.so
- mod_proxy_cluster.so
- mod_slotmem.so
配置mod_cluster模块,在 D:\Apache2.2\conf\extra 目录下新建 http-mod_cluster.conf 配置文件,内容如下:
# Required modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
Listen 192.168.1.107:6666
<VirtualHost 192.168.1.107:6666>
<Location />
Order deny,allow
Deny from all
Allow from all
</Location>
KeepAliveTimeout 60
MaxKeepAliveRequests 0
ManagerBalancerName mycluster
ServerAdvertise On
EnableMCPMReceive
</VirtualHost>
# 设置代理转发,将所有请求都转发至mycluster负载均衡器
ProxyPass / balancer://mycluster/
# 配置mod_cluster-manager以便通过 http://ip:port/mod_cluster-manager 访问mod_cluster管理界面
<Location /mod_cluster-manager>
SetHandler mod_cluster-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
-
修改 D:\Apache2.2\conf\httpd.conf 文件
-
设置 apache 使用的IP及监听端口
ServerName 192.168.1.107:80
-
注释掉mod_proxy_balancer模块,因为此模块与mod_cluster相关模块不兼容
# LoadModule proxy_balancer_module modules/mod_proxy_balancer.so -
在 #Include /private/etc/apache2/extra/httpd-vhosts.conf 配置后增加以下配置
# mod_cluster
Include /private/etc/apache2/extra/httpd-mod_cluster.conf
-
-
验证 D:\Apache2.2\conf\httpd.conf 配置
-
cmd命令行执行以下命令:
D:\Apache2.2\bin\httpd.exe -t
-
如果收到以下结果,则说明配置正确;否则,根据错误提示修正即可
Syntax OK
-
-
启动Apache服务器
-
cmd命令行执行以下命令:
D:\Apache2.2\bin\httpd.exe -k start
-
如果收到以下结果,则说明已正常启动
The Apache2.2 service is running.
] Advertise initialized for process 1764
[Sun Dec 11 18:01:37 2016] [notice] Apache/2.2.22 (Win32) mod_cluster/1.2.6.Final configured -- resuming normal operations
[Sun Dec 11 18:01:37 2016] [notice] Server built: Jan 28 2012 11:16:39
[Sun Dec 11 18:01:37 2016] [notice] Parent: Created child process 4996
[Sun Dec 11 18:01:37 2016] [notice] Child 4996: Child process is running
[Sun Dec 11 18:01:37 2016] [notice] Child 4996: Acquired the start mutex.
[Sun Dec 11 18:01:37 2016] [notice] Child 4996: Starting 64 worker threads.
[Sun Dec 11 18:01:37 2016] [notice] Child 4996: Starting thread to listen on port 6666.
[Sun Dec 11 18:01:37 2016] [notice] Child 4996: Starting thread to listen on port 80. -
在浏览器地址栏输入以下地址访问mod_cluster管理界面:
http://192.168.1.107/mod_cluster-manager
如果看到以下页面,则说明mod_cluster模块安装配置成功
-
六、Tomcat 与 mod_cluster 连接配置
Tomcat7 安装步骤:略……
-
Tomcat7 mod_cluster配置
-
修改 %TOMCAT%\conf\server.xml 新增ModClusterListener
<Listener className="org.jboss.modcluster.container.catalina.standalone.ModClusterListener" advertise="true" />
-
修改 %TOMCAT%\conf\server.xml Engine元素新增jvmRoute=”jvm-app1”
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm-app1">
注: 自mod_cluster 1.1后jvmRoute修改为可选设置,如未设置,则会自动生成一个标识,这里主要是为了区分多个tomcat实例用
-
-
启动Tomcat,并查看 %TOMCAT%\logs\catalina.out 日志。如果发现有以下日志片段,则说明mod_cluster tomcat端配置成功
2016-12-11 18:51:01 org.jboss.modcluster.ModClusterService init
INFO: MODCLUSTER000001: Initializing mod_cluster ${project.version}
2016-12-11 18:51:01 org.jboss.modcluster.advertise.impl.AdvertiseListenerImpl start
INFO: MODCLUSTER000032: Listening to proxy advertisements on /224.0.1.105:233642016-12-11 18:51:02 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
2016-12-11 18:51:02 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
2016-12-11 18:51:02 org.apache.catalina.startup.Catalina start
信息: Server startup in 980 ms
2016-12-11 18:51:12 org.jboss.modcluster.ModClusterService connectionEstablished
INFO: MODCLUSTER000012: Catalina connector will use /192.168.1.107 同理,在另外一台机器上安装配置Tomcat,但jvmRoute设置为”jvm-app2”
七、集成验证
在浏览器地址栏输入 http://192.168.1.107/mod_cluster-manager 查看mod_cluster管理界面,如果看到以下页面,则说明mod_cluster模块安装配置成功
在浏览器地址栏输入 http://192.168.1.107,如果可以看到Tomcat7主页如下,则自此Apache通过mod_cluster模块与Tomcat集成成功。
八、广播地址&端口变更
如:将mod_cluster模块广播地址及端口修改为224.0.1.200:60000
- D:\Apache2.2\conf\extra\http-mod_cluster.conf 配置如下:
# Required modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
Listen 192.168.1.107:6666
<VirtualHost 192.168.1.107:6666>
<Location />
Order deny,allow
Deny from all
Allow from all
</Location>
KeepAliveTimeout 60
MaxKeepAliveRequests 0
ManagerBalancerName mycluster
ServerAdvertise On
# 广播地址,默认为:224.0.1.105:23364
AdvertiseGroup 224.0.1.200:60000
# httpd广播绑定地址端口,在主机有多IP的时绑定,默认为:0.0.0.0:23364
AdvertiseBindAddress 224.0.1.200:60000
EnableMCPMReceive
</VirtualHost>
# 设置代理转发,将所有请求都转发至mycluster负载均衡器
ProxyPass / balancer://mycluster/
# 配置mod_cluster-manager以便通过 http://ip:port/mod_cluster-manager 访问mod_cluster管理界面
<Location /mod_cluster-manager>
SetHandler mod_cluster-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
- %TOMCAT%\conf\server.xml 配置修改如下:
<Listener className="org.jboss.modcluster.container.catalina.standalone.ModClusterListener" advertise="true" advertiseGroupAddress="224.0.1.200" advertisePort="60000" />
...
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm-app1">
- Tomat端更详细的配置可参考:Chapter 9. Server-side Configurantion Properteis
九、mod_cluster负载均衡跳过设置
有时候会碰到部分URL映射到其它地址的场景,这些URL就不需要走mod_cluster负载均衡,就需要跳过,这里我们使用rewrite模块重写配置,比如:把 /manager/ 开头的URL访问都映射到 http://192.168.1.111:8080/manager/ 地址。
- 取消 D:\Apache2.2\conf\httpd.conf 配置中的 mod_proxy_http 、mod_rewrite 模块注释,后面配置rewrite模块需要使用
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
- D:\Apache2.2\conf\extra\http-mod_cluster.conf 文件修改如下:
# Required modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
Listen 192.168.1.107:6666
<VirtualHost 192.168.1.107:6666>
<Location />
Order deny,allow
Deny from all
Allow from all
</Location>
KeepAliveTimeout 60
MaxKeepAliveRequests 0
ManagerBalancerName mycluster
ServerAdvertise On
EnableMCPMReceive
</VirtualHost>
# 设置代理转发,这里为将所有请求都转发至mycluster负载均衡器
ProxyPass / balancer://mycluster/
# 通过rewrite模块设置以 /manager/开头的请求,均跳过mod_cluster负载均衡处理(需要加载mod_rewrite、mod_proxy_http模块)
RewriteEngine on
RewriteRule ^/manager/(.*) http://192.168.1.111:8080/manager/$1 [P]
# 配置mod_cluster-manager,这样才可以访问到mod_cluster-manager管理页面
<Location /mod_cluster-manager>
SetHandler mod_cluster-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
十、参考资料:
[1] . mod_cluster Documentation
[2] . mod_cluster Quick Start Guide
[3] . Server-side Configuration
[4] . Server-side Configuration Properties
[5] . 使用mod_cluster进行负载均衡初步预研
[6] . JBoss 系列二:使用Apache httpd(mod_cluster)和JBoss构架高可用集群环境
[7] . Apache HTTP Server 与 Tomcat 的三种连接方式介绍