六.基于mod_proxy构建负载均衡
为了实现负载均衡,我们增加一台服务器
172.16.1.21 tom.linuxidc.com
6.1 安装jdk与上相同
6.2 安装tomcat与上相同
6.3.1 增加一虚拟主机,并添加engine的jvmRoute
mkdir -pv /web/balance/WEB-INF
vi /usr/local/tomcat/conf/server.xml
<Enginename="Catalina"defaultHost="localhost"jvmRoute="TomcatA"> ##找到Engine添加
<Hostname="balance.linuxidc.com"appBase="/web/balance"
unpackWARs="true"autoDeploy="true">
<Contextpath=""docBase="/web/balance"/>
</Host>
6.3.2 添加一测试页面
vi /web/balance/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><fontcolor="red">TomcatA </font></h1>
<tablealign="centre"border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
6.3.2 重启catalina 声明:修改tomcat配置文件后需重启以后不再说明了
catalina stop
catalina start
6.3.2 添加windows的hosts解析
172.16.1.21 balance.linuxidc.com
6.3.3 访问http://balance.linuxidc.com:8080/ 查看是否成功
6.4.1 同样在原来cat服务器上也增加同样的虚拟主机,方法同上,engine的jvmroute改为TomcatB,为了方便查看修改index.jsp显示的颜色
vi /usr/local/tomcat/conf/server.xml
<Enginename="Catalina"defaultHost="localhost"jvmRoute="TomcatB"> ##找到Engine添加
vi /web/balance/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><fontcolor="green">TomcatB </font></h1>
<tablealign="centre"border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
6.4.2 重启catalina
6.4.3 修改刚添加的windows的hosts测试这个主机
172.16.1.22 balance.linuxidc.com
6.4.4 访问http://balance.linuxidc.com:8080/测试
6.5.下面来配置前端apache来实现后端负载均衡
apache是在cat上安装的,注意的是基于mod_proxy的负载均衡必须在中心主机中配置
6.5.1 编缉apache配置文件/etc/httpd/httpd.conf
vi /etc/httpd/httpd.conf
#Include /etc/httpd/extra/httpd-jk.conf ##注释掉原来的jk
ServerName balance.linuxidc.com:80
#DocumentRoot "/usr/local/apache/htdocs" ##不注释也可以,在下面添加
ProxyRequests Off
<Proxy balancer://lbcluster>
BalancerMember ajp://172.16.1.21:8009 loadfactor=10route=TomcatA
BalancerMember ajp://172.16.1.22:8009 loadfactor=10route=TomcatB
</Proxy>
ProxyPass / balancer://lbcluster/
ProxyPassReverse / balancer://lbcluster/
6.5.2 重启httpd访问http://balance.linuxidc.com/测试,如果页面能切换代表ok
6.6 当然也可以用http协议与tomcat连接,更改协议与端口即可
BalancerMember http://172.16.1.21:8080 loadfactor=10route=TomcatA
BalancerMember httpp://172.16.1.22:8080 loadfactor=10route=TomcatB