Apache负载均衡之mod_proxy模块实现
Apache负载均衡之mod_proxy模块实现
实验环境:一台虚拟机,IP:192.168.1.117
实验目的:学习mod_proxy的负载功能,实现Apache反代并且负载到后端两个tomcat。
实验步骤:
在/etc/httpd/conf.d目录下增加配置文件,实现Apache的负载均衡。
# cd /etc/httpd/conf.d
# vim mod_proxy.conf
<Proxy balancer://lbcluser1>
BalancerMember http://192.168.1.117:8080 loadfactor=1 route=TomcatA
BalancerMember http://192.168.1.117:9080 loadfactor=1 route=TomcatB
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / balancer://lbcluser1/
ProxyPassReverse / balancer://lbcluser/
保存退出
在上面配置文件</Proxy>和ProxyPass / balancer://lbcluser1/ 的中间中添加如下,这样可以访问其status页面。
<Location /status>
SetHander balancer-manager
ProxyPass !
Order allow,deny
Allow from all
</Location>
即是下面配置, # vim mod_proxy.conf
<Proxy balancer://lbcluster1>
BalancerMember http://192.168.1.117:8080 loadfactor=1 route=TomcatA
BalancerMember http://192.168.1.117:9080 loadfactor=1 route=TomcatB
ProxySet lbmethod=byrequests
</Proxy>
<Location /status>
SetHander balancer-manager
ProxyPass !
Order allow,deny
Allow from all
</Location>
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster/
保存退出之后
# httpd -t
# service httpd restart
下面在tomcat下创建test测试页
现在两个tomcat上做测试页,即在/usr/local/tomcatA/webapps/目录下
创建# mkdir -pv ./test/WEB-INF/{classes,lib},并且在test目录下穿件文件
# vim index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA. .just a test page </font></h1>
<table border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("changyu.com","changyu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
同理在tomcatB同位置处
演示效果,在TomcatB上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.just a test page</font></h1>
<table border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("changyu.com","changyu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
还有一步就是在/usr/local/tomcat{1,2}/webpapps/test/WEB-INF/的目录下加一个web.xml文件并且在其任意位置添加<distributable/>。
对于tomcatA来说
[root@org conf]# cp ./web.xml /usr/local/tomcatA/webapps/test/WEB-INF/
[root@org conf]# cd /usr/local/tomcatA/webapps/test/WEB-INF/
[root@org WEB-INF]# ls
classes lib web.xml
[root@org WEB-INF]#
[root@org WEB-INF]# vim web.xml //任意位置加入下信息
<distributable/>
对于tomcatB来说
[root@org conf]# cp ./web.xml /usr/local/tomcatA/webapps/test/WEB-INF/
[root@org conf]# cd /usr/local/tomcatB/webapps/test/WEB-INF/
[root@org WEB-INF]# ls
classes lib web.xml
[root@org WEB-INF]# vim web.xml //任意位置加入下信息。
<distributable/>
分别重新启动。
[root@org bin]# ./catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcatB
Using CATALINA_HOME: /usr/local/tomcatB
Using CATALINA_TMPDIR: /usr/local/tomcatB/temp
Using JRE_HOME: /opt/jdk1.7.0_71/
Using CLASSPATH: /usr/local/tomcatB/bin/bootstrap.jar:/usr/local/tomcatB/bin/tomcat-juli.jar
[root@org bin]# ./catalina.sh start
Using CATALINA_BASE: /usr/local/tomcatB
Using CATALINA_HOME: /usr/local/tomcatB
Using CATALINA_TMPDIR: /usr/local/tomcatB/temp
Using JRE_HOME: /opt/jdk1.7.0_71/
Using CLASSPATH: /usr/local/tomcatB/bin/bootstrap.jar:/usr/local/tomcatB/bin/tomcat-juli.jar
Tomcat started.
[root@org bin]#
重新启动,并且访问http://192.168.1.117/test
刷新页面
将上面test应用定义成集群会话管理器,将下面文件拷贝到两个文件中
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
之后再次访问,会话保持
刷新一下,不难发现SessionID 没有变化。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。