第一章. 背景简介
对于大多数企业应用,都希望能做到7*24小时不间断运行。要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群。系统变为集群时,除了要求系统能够支持水平伸缩外,还要解决两个问题:

1, 如何均衡地访问到提供业务功能的机器。

2, 如何保证当机器出现问题时,用户能自动跳转到另外的机器,不影响使用。

常用的负载均衡技术有硬件和软件两种,本示例常用软件的技术实现。软件也有很多实现技术,如基于apache的mod_jk以及mod_proxy等。基于mod_jk的文章有不少,本文演示一下用mod_proxy的方式。

实现集群的应用最重要的是处理用户Session的问题,一般有三种策略:

1, Session复制

2, Session Sticky

3, 基于Cache的集中式Session

本文使用的是Tomcat 7.0.2应用服务器,用的方法是Session复制。

第二章. 配置环境

1, JDK1.6,请自行下载安装,搞Java的一般都装有的吧,哈哈。

2, Apache 2.2.16, (released 2010-07-25),现在为止应该是最新的稳定版本,下载地址: http://httpd.apache.org/download.cgi

3, Tomcat 7.0.2,目前也是最新的版本。Minimum Java Version1.6.下载地址:http://tomcat.apache.org/download-70.cgi

4, 安装过程略

第三章. 部署图

第四章. Tomcat7集群配置

一、 就地取材,复制tomcat7/webapps下的examples,重命名为cluster应用,以后就用cluster做测试。

二、 详细配置参照tomcat7 \webapps\docs\cluster-howto.html 或者http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html

三、 为了在Tomcat7中实现session复制,以下必需完成:

• 所有session属性必需实现 java.io.Serializable

• Uncomment the Cluster element in server.xml。把Cluster元素的注释去掉。参照四

• If you have defined custom cluster valves, make sure you have the
ReplicationValve defined as well under the Cluster element in server.xml
。参照四

• If your Tomcat instances are running on the same machine, make
sure the tcpListenPort attribute is unique for each instance, in most
cases Tomcat is smart enough to resolve this on it's own by
autodetecting available ports in the range 4000-4100。参照四<
Receiver>中的注释

• Make sure your web.xml has the <distributable/> element or set at your <Context distributable="true" /> 参照四

• If you are using mod_jk, make sure that jvmRoute attribute is set
at your Engine <Engine name="Catalina" jvmRoute="node01" > and
that the jvmRoute attribute value matches your worker name in
workers.properties .用mod_jk的情况,我们可以不管。

• Make sure that all nodes have the same time and sync with NTP service! 当使用多台机器时,要保证不同机器时间的同步。原因为tomcat session复制的一些机制。具体原因看文档。

• Make sure that your loadbalancer is configured for sticky session mode. 保证负载均衡软件设置为session sticky模式。

四、 详细配置:

1. 修改tomcat7_a/conf/server.xml, 我们采用的是默认的配置,在<Engine>节点下添加:

  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
  2. channelSendOptions="8">
  3. <Manager className="org.apache.catalina.ha.session.DeltaManager"
  4. expireSessionsOnShutdown="false"
  5. notifyListenersOnReplication="true"/>
  6. <Channel className="org.apache.catalina.tribes.group.GroupChannel">
  7. <Membership className="org.apache.catalina.tribes.membership.McastService"
  8. address="228.0.0.4"
  9. port="45564"
  10. frequency="500"
  11. dropTime="3000"/>
  12. <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
  13. address="auto"
  14. <!—tcpListenPort如果是同一机器部署两个tomcat7应用,则修改tomcat7_b为4001,以免冲突. 不同机器下,不用更改此项。—>
  15. [color=red]port="4000"[/color]
  16. autoBind="100"
  17. selectorTimeout="5000"
  18. maxThreads="6"/>
  19. <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
  20. <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
  21. </Sender>
  22. <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
  23. <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
  24. </Channel>
  25. <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
  26. filter=""/>
  27. <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
  28. <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
  29. tempDir="/tmp/war-temp/"
  30. deployDir="/tmp/war-deploy/"
  31. watchDir="/tmp/war-listen/"
  32. watchEnabled="false"/>
  33. <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
  34. <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
  35. </Cluster>

2. <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 修改,仅为了调试方便。

3. tomcat7_a\webapps\cluster\WEB-INF\web.xml中加入<distributable/>

4. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_a">,添加jvmRoute属性,此项为后面apache负载均衡用到。

五、 复制一份tomcat7_a应用,改名为tomcat7_b.只是为了图方便,实际应该复制的是前面的cluster工程。注意以下几点配置就ok.

1. 修改tomcat7_a/conf/server.xml 中的Server port属性<Server port="8006" shutdown="SHUTDOWN">,因为是同一台机器两个tomcat应用,所以改一下。

2. 修改<connector port="8082" protocol="HTTP/1.1" onnectionTimeout="20000" edirectPort="8443" />,同理,为了避免同一台机器端口号冲突。部置在不同的机器是不用管的。

3. 修改<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_b">,此项为后面apache负载均衡用到。

4. 修改<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6"/>

5. tomcat7_b\ebapps\cluster\WEB-INF\web.xml中加入<distributable/>

到此集群配置完成.

第五章. 集群测试

1. tomcat7_a和tomcat7_b的cluster工程中分别添加测试文件:testCluster.jsp

  1. <%@ page contentType="text/html; charset=GBK" %>
  2. <%@ page import="java.util.*" %>
  3. <html><head><title>Cluster Test</title></head>
  4. <body>
  5. <%
  6. //HttpSession session = request.getSession(true);
  7. System.out.println(session.getId());
  8. out.println("<br> SESSION ID:" + session.getId()+"<br>");
  9. // 如果有新的请求,则添加session属性
  10. String name = request.getParameter("name");
  11. if (name != null && name.length() > 0) {
  12. String value = request.getParameter("value");
  13. session.setAttribute(name, value);
  14. }
  15. out.print("<b>Session List:</b>");
  16. Enumeration<String> names = session.getAttributeNames();
  17. while (names.hasMoreElements()) {
  18. String sname = names.nextElement();
  19. String value = session.getAttribute(sname).toString();
  20. out.println( sname + " = " + value+"<br>");
  21. System.out.println( sname + " = " + value);
  22. }
  23. %>
  24. <form action="testCluster.jsp" method="post">
  25. 名称:<input type=text size=20 name="name">
  26. <br>
  27. 值:<input type=text size=20 name="value">
  28. <br>
  29. <input type=submit value="提交">
  30. </form>
  31. </body>
  32. </html>

2. 启动tomcat7_a,启动完毕后,启动tomcat7_b

3. 进入http://localhost:8081/cluster/testCluster.jsp 对应tomcat7_a(8081),登录几次,可看到

4. 另外打开一个浏览器,进入http://localhost:8082/cluster/testCluster.jsp对应tomcat7_b(8082),登录name:tomcat_b,value:b value可看到

5. 刷新tomcat7_a(8081)相关页面,可以看到从tomcat7_b提交的值session同步过来了,说明集群成功。

第六章. Session集群工作步骤

参照tomcat7 doc:

To make it easy to understand how clustering works, We are gonna
take you through a series of scenarios. In the scenario we only plan to
use two tomcat instances TomcatA and TomcatB. We will cover the
following sequence of events:

1. TomcatA starts up

2. TomcatB starts up (Wait that TomcatA start is complete)

3. TomcatA receives a request, a session S1 is created.

4. TomcatA crashes

5. TomcatB receives a request for session S1

6. TomcatA starts up

7. TomcatA receives a request, invalidate is called on the session (S1)

8. TomcatB receives a request, for a new session (S2)

9. TomcatA The session S2 expires due to inactivity.

第七章. 负载均衡配置

Tomcat有两种负载均衡的方式:

1. 使用 JK1.2.x native connector

2. 使用Apache HTTP Server 2.x with mod_proxy

我们使用的是mod_proxy,在Apache Http Server2.2以上版本已经自动带有:

Mod_proxy supports either HTTP or AJP load balancing. 我们通过ajp方式。

详细查看:tomcat7\webapps\docs\balancer-howto.html

1. 首先,监听8000端口, 在Apache安装目录下找到conf/httpd.conf文件

稍前面加上

  1. # 监听端口和监听地址
  2. Listen 8000

2. conf/httpd.conf,去掉以下文本前的注释符(#)以便让Apache在启动时自动加载代理(proxy)模块。

  1. LoadModule proxy_module modules/mod_proxy.so
  2. LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
  3. LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
  4. LoadModule proxy_connect_module modules/mod_proxy_connect.so
  5. LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
  6. LoadModule proxy_http_module modules/mod_proxy_http.so

3. conf/httpd.conf文件最后加上:

  1. #虚拟机配置,负载均衡配置
  2. <VirtualHost *:8000>
  3. ServerAdmin weigbo@163.com
  4. ServerName localhost
  5. ServerAlias localhost
  6. #小心,有些地方要有空格,要不然会出错哈哈。
  7. ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On
  8. ProxyPassReverse / balancer://cluster/
  9. #ErrorLog "logs/error.log"
  10. #CustomLog "logs/access.log" common
  11. </VirtualHost>
  12. #The ProxyRequests directive should usually be set off when using ProxyPass.
  13. ProxyRequests Off
  14. <proxy balancer://cluster>
  15. BalancerMember ajp://localhost:8009 loadfactor=1 route=tomcat7_a  smax=5 max=20 ttl=120 retry=300 timeout=15
  16. BalancerMember ajp://localhost:9009 loadfactor=1 route=tomcat7_b  smax=5 max=20 ttl=120 retry=300 timeout=15
  17. # status=+H为配置热备,当所有机器都over时,才会请求该机器
  18. #BalancerMember http://192.168.1.218:8009 status=+H
  19. ProxySet lbmethod=bytraffic
  20. </proxy>

4. Tomcat7配置(server.xml):

因为是同一机器,两个应用,所以配不同的端口,不同机器则不用配,要和前面的ajp对应上。

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

第八章. 负载均衡测试

Tomcat7_a的cluster工程的测试文件:testCluster.jsp加上html代码:

<b>负载均衡测试:此为:Tomcat7_a上的文件,<font color=red>aaaaaaaaaaaaaaaaaa</font><b>

Tomcat7_b的cluster工程的测试文件:testCluster.jsp加上html代码

<b>负载均衡测试:此为:Tomcat7_b上的文件,<font color=red>bbbbbbbbbbbbbbbbbb</font><b>

打开浏览器,进入地址:http://localhost:8000/cluster/testCluster.jsp

多刷新几次,会看到页面值会在aaaaa和bbbbb间切换,说明负载均衡配置成功。如图:

第九章. Mod_proxy负载均衡算法

目前mod_proxy有3种负载均衡算法:

1. Request Counting(我猜是Round-robin), lbmethod=byrequests

2. Weighted Traffic Counting(这个是按权重,此例也是用此算法), lbmethod=bytraffic

3. Pending Request Counting(从apche文档来看,应该是按负载量,也就是往负载少的派发新请求). lbmethod=bybusyness

它们通过lbmethod值设置。

第十章. 参考文档:

Tomcat 7 doc文档

http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

第十一章. 补充内容:负载均衡管理器

负载均衡管理器可以动态更新负载均衡的应用,你可以用管理器更改负载因子或者特定的应用,还可以把应用设为off模式。

为了使用balancer management, mod_status和mod_proxy_balancer 必需要加载到apache.

设置:

#此项为mod_proxy_balancer管理器需要

LoadModule status_module modules/mod_status.so

在负载均衡项前加上:

#mod_balancer管理应用

<Location /balancer-manager>  

     SetHandler balancer-manager

     Order Allow,Deny

     Allow from all

</Location>

可以通过以下地址访问:http://localhost:8000/balancer-manager

附件中是一些相关的配置文件,大家可以参考:
配置下载

基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置的更多相关文章

  1. apache的tomcat负载均衡和集群配置 "

    略看了一下,感觉太复杂,要配置的东西太多,因此在这里写出一种更简洁的方法. 要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务 ...

  2. 使用apache和nginx代理实现tomcat负载均衡及集群配置详解

    实验环境: 1.nginx的代理功能 nginx proxy: eth0: 192.168.8.48 vmnet2 eth1: 192.168.10.10 tomcat server1: vmnet2 ...

  3. apache + tomcat 负载均衡分布式集群配置

    Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...

  4. 基于apache的tomcat负载均衡和集群配置

    最近不是很忙,用零碎时间做点小小的实验. 以前公司采用F5负载均衡交换机,F5将请求转发给多台服务器,每台服务器有多个webserver实例,每个webserver分布在多台服务器,交叉式的分布集群. ...

  5. Apache +Tomcat的负载均衡与集群配置

    实验拓扑图: 一.搭配环境 (1).Tomcat的安装和配置 Tomcat_a的ip:192.168.55.229 Tomcat_b的ip:192.168.55.231 Tomcat的需要安装jdk和 ...

  6. 基于apache的tomcat负载均衡和集群配置session共享

    接上面的话题接着继续完善.为什么没接到上篇呢?原因很简单太长的文章不爱看!就像有人写了上千行的方法一样,不是逼得没办法谁爱看谁看,反正我不看. 期间我没有一次配置成功,从失败的开始说起, 1.准备ja ...

  7. 基于nginx的tomcat负载均衡和集群

    要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务器即可共享SESSION了. 你可以自己写tomcat的扩展来保存SESSI ...

  8. 基于Apache的Tomcat负载均衡和集群(2)

    反向代理负载均衡 (Apache+JK+Tomcat) 使用代理服务器可以将请求转发给内部的Web服务器,让代理服务器将请求均匀地转发给多台内部Web服务器之一上,从而达到负载均衡的目的.这种代理方式 ...

  9. Apache配置反向代理、负载均衡和集群(mod_proxy方式)

    Apache配置负载均衡和集群使用mod_jk的方式比较多,但是mod_jk已经停止更新,并且配置相对复杂.Apache2.2以后,提供了一种原生的方式配置负载均衡和集群,比mod_jk简单很多. 1 ...

随机推荐

  1. oracle中查询含字母的数据[正则表达式]

    1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBSTR的功能相似4,REGEXP_REPLACE :与 ...

  2. 引用类型传递 ListView展示数据

    教师评分项目总结 //创建一个SE员工类 1.1       //首先分析项目 * 01.我需要在LIstView控件中显示三个员工的信息 * 那么可以定义一个长度为3的数组来承载要显示的数据 * 0 ...

  3. bzoj 1879 [Sdoi2009]Bill的挑战(状压DP)

    Description  Input 本题包含多组数据. 第一行:一个整数T,表示数据的个数. 对于每组数据: 第一行:两个整数,N和K(含义如题目表述). 接下来N行:每行一个字符串. Output ...

  4. iLearning D3.js 2.0 released

    There are some great changes in 2.0 version. Console in tutorial: In previous version, there will be ...

  5. Storm系列(十五)架构分析之Executor-Spout

    Spout实现mk-threads接口用于创建与Executor对应的消息循环主函数. defmulti mk-threads executor-selector Mk-threads函数的主消息循环 ...

  6. HW3.12

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. ndk-gdb of NDK r9d modified to *always* debug the ":remote"-process of your app

    https://gist.github.com/TomTasche/9690186 ndk-gdb of NDK r9d modified to *always* debug the ":r ...

  8. 在JAVA中线程到底起到什么作用

    这是javaeye上非常经典的关于线程的帖子,写的非常通俗易懂的,适合任何读计算机的同学. 线程同步 我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread ...

  9. [Ember] Creating Your First Ember.js Project with Ember-CLI

    In this lesson, we'll setup Ember-CLI and use it to create and run our first Ember.js project. Insta ...

  10. 详解QUiLoader 动态加载.ui文件

    http://blog.chinaunix.net/uid-13838881-id-3652523.html 1.适用情况: 动态加载UI文件是指,用 Qt Designer 通过拖拽的方式生产.ui ...