配置基于mod_jk的反向代理并负载均衡
Apache2.4.12:192.168.2.110
tomcatA:192.168.2.120
tomcatB:192.168.2.121
1、 为了避免用户直接访问后端Tomcat实例,影响负载均衡的效果,建议在Tomcat7的各实例上禁用HTTP/1.1连接器。
2、为每一个Tomcat 7实例的引擎添加jvmRoute参数,并通过其为当前引擎设置全局惟一标识符。如下所示。需要注意的是,每一个实例的jvmRoute的值均不能相同。
<Engine name=”Standalone” defaultHost=”localhost” jvmRoute=”TomcatA ”>
而后去配置apache,修改/usr/local/apache2.4.12/conf/extra/httpd-jk-proxy-lb.conf为如下内容:
[root@node110 extra]# cat httpd-jk-proxy-lb.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/apache2.4.12/conf/extra/workers.properties_lb
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* lbcluster1
JkMount /jkstatus/ stat1
在apache主配置文件httpd.conf中加载httpd-jk-proxy-lb.conf文件:
Include conf/extra/httpd-jk-proxy-lb.conf
编辑/usr/local/apache2.4.12/conf/extra/workers.properties_lb,添加如下内容:
worker.list = lbcluster1,stat1
worker.TomcatA.type = ajp13
worker.TomcatA.host = 192.168.2.120 #后端的tomcat服务
worker.TomcatA.port = 8009
worker.TomcatA.lbfactor = 5 #主机的权重
worker.TomcatB.type = ajp13
worker.TomcatB.host = 192.168.2.121 #后端的tomcat服务
worker.TomcatB.port = 8009
worker.TomcatB.lbfactor = 5 #主机的权重
worker.lbcluster1.type = lb #类型为负载均衡
worker.lbcluster1.sticky_session = 1 #1表示对会话进行绑定,0表示不对会话进行绑定
worker.lbcluster1.balance_workers = TomcatA, TomcatB
worker.stat1.type = status
演示效果,在TomcatA上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><fontcolor="red">TomcatA.magedu.com</font></h1>
<tablealign="centre" border="1">
<tr>
<td>Session ID</td>
<%session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId()%></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
演示效果,在TomcatB上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><fontcolor="blue">TomcatB.magedu.com</font></h1>
<tablealign="centre" border="1">
<tr>
<td>Session ID</td>
<%session.setAttribute("magedu.com","magedu.com"); %>
<td><%=session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
cd /usr/local/apache2.4.12/conf/extra
vim http-jk-proxy-lb.conf
[root@node110 extra]# cat httpd-jk-proxy-lb.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/apache2.4.12/conf/extra/workers.properties_lb
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* lbcluster1
JkMount /jkstatus/ stat1
vim workers.properties_lb
[root@node110 extra]# cat workers.properties_lb
worker.list = lbcluster1,stat1
worker.TomcatA.type = ajp13
worker.TomcatA.host = 192.168.2.120
worker.TomcatA.port = 8009
worker.TomcatA.lbfactor = 5
worker.TomcatB.type = ajp13
worker.TomcatB.host = 192.168.2.121
worker.TomcatB.port = 8009
worker.TomcatB.lbfactor = 5
worker.lbcluster1.type = lb
worker.lbcluster1.sticky_session = 1
worker.lbcluster1.balance_workers = TomcatA, TomcatB
worker.stat1.type = status