一、实验环境准备
1.主机规划
Apache主机 | 172.18.12.20 |
TomcatA | 172.18.12.21 |
TomcatB | 172.18.12.22 |
2.tomcat主机安装和配置
# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel # vim /etc/profile.d/java.sh # yum install -y tomcat tomcat-lib tomcat-webapps tomcat-admin-webapps
3.为tomcat主机配置不同的页面来显示配置效果,注意在生产环境中他们应该是相同的页面
# mkdir /var/lib/tomcat/webapps/myapp # mkdir /var/lib/tomcat/webapps/myapp/{classes,lib,WEB-INF,META-INF}
a)TomcatA主机
# vim /var/lib/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.magedu.com</font></h1>
<table align="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>
#systemctl start tomcat
b)TomcatB主机
# vim /var/lib/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.magedu.com</font></h1>
<table align="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>
#systemctl start tomcat
4.Apache主机配置
Apache负载均衡后端主机有三种实现方法,此处我们分别介绍
1)使用httpd自带的模块mod_proxy_http+mod_proxy_balancer通过http协议来实现负载均衡
负载均衡实现
# vim /etc/httpd/conf.d/mod_proxy.conf
<proxy balancer://tcsrvs>
BalancerMember http://172.18.12.21:8080
BalancerMember http://172.18.12.22:8080
ProxySet lbmethod=byrequests
</Proxy>
<VirtualHost *:80>
ServerName lb.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
# systemctl restart httpd
会话保持(session stiky)
# vim /etc/httpd/conf.d/mod_proxy.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://tcsrvs>
BalancerMember http://172.18.12.21:8080 route=TomcatA loadfactor=1
BalancerMember http://172.18.12.22:8080 route=TomcatB loadfactor=2
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
ServerName dy.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
</VirtualHost>
# systemctl reload httpd
2)使用httpd自带的模块mod_proxy_ajp+mod_proxy_balancer通过http协议来实现负载均衡
# vim /etc/httpd/conf.d/mod_proxy_ajp.conf
<proxy balancer://tcsrvs>
BalancerMember ajp://172.18.12.21:8009 route=TomcatA loadfactor=1
BalancerMember ajp://172.18.12.22:8009 route=TomcatB loadfactor=2
ProxySet lbmethod=byrequests
</Proxy>
<VirtualHost *:80>
ServerName lb.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
</VirtualHost>
#systemctl reload httpd
3)使用第三方模块mod_jk
1.编译安装mod_jk
# yum groupinstall -y "development tools" "server platform development" # yum install httpd-devel # tar -xf tomcat-connectors-1.2.40-src.tar.gz # cd tomcat-connectors-1.2.40-src/native/ # ./configure --with-apxs=/usr/bin/apxs #make && make install
模块配置文件:/etc/httpd/conf.d/mod_jk.conf
#vim /etc/httpd/conf.d/mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* tcsrvs 定义一个负载均衡组
JkMount /jk_status StatA
workers配置文件:/etc/httpd/conf.d/workers.properties
#vim /etc/httpd/conf.d/workers.properties
worker.list=tcsrvs,StatA
worker.TomcatA.host=172.18.12.21
worker.TomcatA.port=8009
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.TomcatB.host=172.18.12.22
worker.TomcatB.port=8009
worker.TomcatB.type=ajp13
worker.TomcatB.lbfactor=2
worker.tcsrvs.type=lb
worker.tcsrvs.balance_workers=TomcatA,TomcatB
worker.tcsrvs.sticky_session=0 是否开启会话保持
worker.StatA.type=status
如果要做会话绑定,此处的配置文件使用worker的名称,要与后端tomcat的Engine属性中的jvmRoute的值保持一致;
应做如下修改:
worker.tcsrvs.sticky_session=1
# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">
# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">