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

系统环境:centos4.8

应用版本:

apache:2.2.3

tomcat:7.0.14

配置要点:

1.tomcatsession复制:

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负载均衡用到

<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat7_1">

<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat7_2">

3.同一机子上有多个tomcat,SHUTDOWN端口不能冲突。

tomcat7_1:<Serverport="8006"shutdown="SHUTDOWN">

tomcat7_2:<Serverport="8007"shutdown="SHUTDOWN">

4.同理,http端口和ajp端口都不能冲突:

eg:

<connectorport="8082"protocol="HTTP/1.1"onnectionTimeout="20000"edirectPort="8443"/>

<Connectorport="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>

[email protected]

ServerNamelocalhost

ServerAliaslocalhost

ProxyPass/balancer://cluster/stickysession=JSESSIONID|jsessionidnofailover=On

ProxyPassReverse/balancer://cluster/

#ErrorLog"logs/error.log"

#CustomLog"logs/access.log"common

</VirtualHost>

ProxyRequestsOff

<proxybalancer://cluster>

BalancerMemberajp://内网ip:8009loadfactor=1route=tomcat7_1smax=5max=20ttl=120retry=300timeout=15

BalancerMemberajp://内网ip:8010loadfactor=1route=tomcat7_2smax=5max=20ttl=120retry=300timeout=15

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

#BalancerMemberhttp://xxxxxxx:8009status=+H

#lbmethod=byrequests按照请求次数均衡(默认)

#lbmethod=bytraffic按照流量均衡

#lbmethod=bybusyness按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)

ProxySetlbmethod=bytraffic

</proxy>

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

相关推荐