(转)Apache2 + Tomcat6配置负载均衡

1. 安装Apache和Tomcat,这里假定Apache2.2.3,Tomcat6.x;且Apache安装在apachehost,tomcat分别安装在tomcathost1和tomcathost2上;
2. 修改/etc/hosts文件,确定以下行没有被注释掉
view plaincopy to clipboardprint?
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
3. 修改/etc/hosts文件,添加以下行:
view plaincopy to clipboardprint?
ProxyRequests off
<Proxy balancer://tccluster>
BalancerMember http://tomcathost1:8080 loadfactor=2 max=150 smax=145
BalancerMember http://tomcathost2:8080 loadfactor=1 max=150 smax=145
Order Deny,Allow
Allow from all
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>
<Location /examples>
ProxyPass balancer://tccluster/examples stickysession=jsessionid
ProxyPassReverse balancer://tccluster/examples
Order Deny,Allow
Allow from all
</Location>
ProxyRequests off
<Proxy balancer://tccluster>
BalancerMember http://tomcathost1:8080 loadfactor=2 max=150 smax=145
BalancerMember http://tomcathost2:8080 loadfactor=1 max=150 smax=145
Order Deny,Allow
Allow from all
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>
<Location /examples>
ProxyPass balancer://tccluster/examples stickysession=jsessionid
ProxyPassReverse balancer://tccluster/examples
Order Deny,Allow
Allow from all
</Location>
4. 确定apachehost和两台tomcathost可以互访,并且两个tomcat机器在一个网段内;
5. 确定两台tomcat host多播已经打开,在Linux机器上可以使用cat /proc/net/dev_mcast检查,如果文件存在基本上就是打开了;另外确定两台tomcat机器的/etc/hosts文件中包含“xxx.xxx.xxx.xxx hostname”比如“172.17.1.101 tomcathost1”或“172.17.1.102 tomcathost2”;
6. 修改两个tomcat的conf/server.xml文件,对Engine节点分别添加jvmRoute="tomcat1"和jvmRoute="tomcat2",并添加以下内容:
view plaincopy to clipboardprint?
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">


<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<!--
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
-->
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.5"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
selectorTimeout="100"
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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">


<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<!--
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
-->
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.5"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
selectorTimeout="100"
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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
7. 创建一个web app,这里为了简单使用tomcat自带的examples web app(webapps/examples),修改其中的WEB-INF/web.xml文件,在其中<display-name>Servlet and JSP Examples</display-name>节点后添加<distributable/>表明此应用与集群服务器复制Session;
8. 在两个Tomcat的webapps/examples目录下各创建一个test.jsp文件,用来测试Cluster中两个Tomcat的Session复制,代码如下:
view plaincopy to clipboardprint?
<html>
<head>
<title></title>
</head>
<body>
<%
String mydata = request.getParameter("mydata");
if (mydata != null && mydata.length() != 0) {
session.setAttribute("mydata", mydata);
}

out.println("request.getLocalAddr(): " + request.getLocalAddr());
out.println("<br/>");
out.println("request.getLocalPort(): " + request.getLocalPort());
out.println("<br/>");
out.println("Session ID: " + session.getId());
out.println("<br/>");

out.println("mydata: " + session.getAttribute("mydata"));
%>
<form>
<input type=text size=20 name="mydata">
<br>
<input type=submit>
</form>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<%
String mydata = request.getParameter("mydata");
if (mydata != null && mydata.length() != 0) {
session.setAttribute("mydata", mydata);
}

out.println("request.getLocalAddr(): " + request.getLocalAddr());
out.println("<br/>");
out.println("request.getLocalPort(): " + request.getLocalPort());
out.println("<br/>");
out.println("Session ID: " + session.getId());
out.println("<br/>");

out.println("mydata: " + session.getAttribute("mydata"));
%>
<form>
<input type=text size=20 name="mydata">
<br>
<input type=submit>
</form>
</body>
</html>
9. 启动Tomcat和Apache,访问http://apachehost/examples/,可以修改其中Session中的值,并且可以看到两个tomcat的Session中的值是一样的;
10. 访问http://apachehost/balancer-manager,可以使用此负载均衡管理页面来动态enable/disable其中的某个tomcat。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值