apache与tomcat负载集群集成方法有3种jk、jk_proxy、http_proxy
本次集成使用的软件版本:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
apache:httpd
-
2.2
.
17
-
win32
-
x86
-
no_ssl.msi
tomcat:apache
-
tomcat
-
6.0
.
20
.zip
jdk:jdk
-
6u14
-
windows
-
i586.exe
本次测试是1个apache集成两个tomcat。
安装apache http server省略,访问地址为http://127.0.0.1:8081
安装tomcat,解压apache-tomcat-6.0.20.zip,测试时我是把两个tomcat分开放在不同的虚拟机,其中一个是和apache同一台虚拟机。
两个tomcat分别命名为worker2和worker3
先说tomcat.worker2的配置:
server.xml
(1)配置http监听端口,这里端口设为8079,该步骤非必要,只要不冲突就行了。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
Connector
port
="8079"
protocol
="HTTP/1.1"
connectionTimeout
="20000"
redirectPort
="8443"
/>
(2)配置AJP监听端口,这里端口设为8077,该步骤非必要,只要不冲突就行了。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
Connector
port
="8077"
protocol
="AJP/1.3"
redirectPort
="8443"
/>
(3)配置服务器标识,这里标识名配置为:worker2,添加jvmRoute="worker2",该步骤必须。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
Engine
name
="Catalina"
defaultHost
="localhost"
jvmRoute
="worker2"
>
在Engine节点启用集群配置,只需去掉Cluster节点前的注释就行了,该步骤必须,配置了集群才能实现Session复制,如果只有一个集群,只按我下边的配置就行了,如果多个集群,则不能按此配置,tomcat服务器内的帮助文档/docs/cluster-howto.html,/docs/config/cluster.html有介绍,需要的可以参考下。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
Engine
name
="Catalina"
defaultHost
="localhost"
jvmRoute
="worker2"
>
<
Cluster
className
="org.apache.catalina.ha.tcp.SimpleTcpCluster"
/>
要实现session复制,还需要在context.xml添加属性distributable="true",如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
Context
distributable
="true"
>
如果不想在context.xml中添加distributable="true",还有另一方法是在应用程序的web.xml中添加<distributeable/>,不过这方法我没有测试。
配置完成,访问地址为:http://127.0.0.1:8079
另一个tomcat.worker3的配置
server.xml
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
Connector
port
="8078"
protocol
="HTTP/1.1"
connectionTimeout
="20000"
redirectPort
="8443"
/>
<
Connector
port
="9009"
protocol
="AJP/1.3"
redirectPort
="8443"
/>
<
Engine
name
="Catalina"
defaultHost
="localhost"
jvmRoute
="worker3"
>
<
Cluster
className
="org.apache.catalina.ha.tcp.SimpleTcpCluster"
/>
context.xml配置tomcat.worker2一样。
配置完成,访问地址为:http://127.0.0.1:8078
注意:如果两个tomcat是放在同一台机内,server.xml可能还需要修改其它端口,请确认两个tomcat能正常启动。
在tomcat.worker2和tomcat.worker3的webapps文件夹添加同样的测试文件test"test.jsp
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
html
>
<
head
>
<
title
>
helloapp
</
title
>
</
head
>
<
body
>
<%
System.out.println(
"
call test.jsp
"
);
%>
SessionID:
<%
=
session.getId()
%>
</
body
>
</
html
>
访问http://127.0.0.1:8078/test/test.jsp和http://127.0.0.1:8079/test/test.jsp,确保能成功访问。
现在开始apache和tomcat的不同方式集成
1、jk方式集成
下载mod_jk-1.2.31-httpd-2.2.3.so,请下载合适的mod_jk版本,改名为mod_jk.so放进modules文件夹内
修改conf/httpd.conf配置
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /*.do loadbalancer
JkMount /*.jsp loadbalancer
增加conf/workers.properties文件,添加内容
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
worker.list=loadbalancer
worker.worker2.port=8077 #ajp的监听端口
worker.worker2.host=108.88.3.105
worker.worker2.type=ajp13
worker.worker2.lbfactor=1
worker.worker3.port=9009
worker.worker3.host=127.0.0.1
worker.worker3.type=ajp13
worker.worker3.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker2,worker3 #这里在的worker2、worker3为需要与上边tomcat设置的别名一致
worker.loadbalancer.sticky_session=1
配置完成任务,访问http://127.0.0.1:8081/test/test.jsp,检查是否能正常访问。
2、ajp_proxy
去掉httpd.conf文件中下面内容的注释(删掉#号),开启下边的配置
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
Include conf/extra/httpd-vhosts.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.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
注意:
除了mod_proxy.so,mod_proxy_balancer.so,mod_proxy_connect.so
如果是采用ajp_proxy,需要加载mod_proxy_ajp.so这个模块;
如果是采用http_proxy,需要加载mod_proxy_http.so这个模块;
httpd.conf,删除刚才jk方式的配置内容,增加下边的配置
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
ProxyRequests Off
<
proxy
balancer://loadbalancer
>
BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=worker3
BalancerMember ajp://108.88.3.105:8077 loadfactor=1 route=worker2
</
proxy
>
conf/extra/httpd-vhosts.conf增加配置
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
VirtualHost
*:8081
>
ServerAdmin [email protected]
ServerName localhost
ServerAlias localhost
ProxyPass / balancer://loadbalancer/ stickysession=jsessionid nofailover=On
ProxyPassReverse / balancer://loadbalancer/
ErrorLog "logs/loadbalancer-error.log"
CustomLog "logs/loadbalancer-access.log" common
</
VirtualHost
>
配置完成任务,访问http://127.0.0.1:8081/test/test.jsp,检查是否能正常访问。
3、http_proxy
http_proxy的配置与ajp_proxy类似,修改方法2的httpd.conf配置如下,其它不变
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
ProxyRequests Off
<
proxy
balancer://loadbalancer
>
BalancerMember http://127.0.0.1:8078 loadfactor=1 route=worker3
BalancerMember http://108.88.3.105:8079 loadfactor=1 route=worker2
</
proxy
>
配置完成任务,访问http://127.0.0.1:8081/test/test.jsp,检查是否能正常访问。
apache_tomcat配置文件下载:apache_tomcat