centos环境下apache集群tomcat与负载均衡配置笔记

系统环境:centos4.8
应用版本:
apache:2.2.3
tomcat:7.0.14
配置要点:
1.tomcat session复制:
server.xml,在<Engine>节点下添加:
<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"
//tcpListenPort如果是同一机器部署两个tomcat7应用,则修改tomcat7_b为4001,以免冲突. 不同机器下,不用更改此项.
                      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"/>
// 如果tomcat启动出现:严重: FarmWarDeployer can only work as host cluster subelement! 可以将此节点去掉 
          <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>


2. 为tomcat增加唯一标示(个人理解),添加jvmRoute属性,apache负载均衡用到
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_1">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_2">

3.同一机子上有多个tomcat,SHUTDOWN 端口不能冲突。
tomcat7_1:<Server port="8006" shutdown="SHUTDOWN">
tomcat7_2:<Server port="8007" shutdown="SHUTDOWN">

4.同理,http端口和ajp端口都不能冲突:
eg:
<connector port="8082" protocol="HTTP/1.1" onnectionTimeout="20000" edirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

5. 为了方便测试,直接用到了webapps 中的examples工程,
tomcat7_1\webapps\examples\WEB-INF\web.xml中加入<distributable/>
tomcat7_2\webapps\examples\WEB-INF\web.xml中加入<distributable/>
两个tomcat中examples工程都加入test.jsp
代码如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster Test</title></head>
<body>
<%
  //HttpSession session = request.getSession(true);
  System.out.println(session.getId());
  out.println("<br> SESSION ID:" + session.getId()+"<br>");  
    String name = request.getParameter("name");
  if (name != null && name.length() > 0) {
     String value = request.getParameter("value");
     session.setAttribute(name, value);
  }  
	out.print("<b>Session List:</b>");  
	Enumeration<String> names = session.getAttributeNames();
	while (names.hasMoreElements()) {
		String sname = names.nextElement(); 
		String value = session.getAttribute(sname).toString();
		out.println( sname + " = " + value+"<br>");
        System.out.println( sname + " = " + value);
   }
%>
  <form action="test.jsp" method="post">
    session名称:<input type=text size=20 name="name">
     <br>
    session值:<input type=text size=20 name="value">
     <br>
    <input type=submit value="提交">
   </form>
</body>
</html>


在此,session复制工作完毕,测试:
访问tomcat7_1输入session名称和值提交以后再访问tomcat7_2,如果session列表中有tomcat7_1的名称和值说明session同步工作成功!


6.负载均衡配置:

负载方式有jk和 mod_proxy
jk方式曾经已经做过,本次尝试mod_proxy

配置步骤:
httpd.conf中增加以下模块:
LoadModule proxy_module modules/mod_proxy.so  
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so  
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so  
LoadModule proxy_connect_module modules/mod_proxy_connect.so  
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so  
LoadModule proxy_http_module modules/mod_proxy_http.so 


再增加:

#虚拟机配置,负载均衡配置
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName localhost
ServerAlias localhost
ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On
ProxyPassReverse / balancer://cluster/
#ErrorLog "logs/error.log"
#CustomLog "logs/access.log" common
</VirtualHost>

ProxyRequests Off
<proxy balancer://cluster>
BalancerMember ajp://内网ip:8009 loadfactor=1 route=tomcat7_1  smax=5 max=20 ttl=120 retry=300 timeout=15
BalancerMember ajp://内网ip:8010 loadfactor=1 route=tomcat7_2  smax=5 max=20 ttl=120 retry=300 timeout=15

# status=+H为配置热备,当所有机器都over时,才会请求该机器
#BalancerMember http://xxxxxxx:8009 status=+H

#lbmethod=byrequests 按照请求次数均衡(默认)
#lbmethod=bytraffic 按照流量均衡
#lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
ProxySet lbmethod=bytraffic
</proxy>

配置完毕。将今天的奋战结果记录下来,方便以后参考。好记性不如iteye的博客,

猜你喜欢

转载自labuladuo.iteye.com/blog/1102537
今日推荐