基于mod_proxy Apache 2.2.16 Tomcat 7的负载均衡与集群配置.doc_第1页
基于mod_proxy Apache 2.2.16 Tomcat 7的负载均衡与集群配置.doc_第2页
基于mod_proxy Apache 2.2.16 Tomcat 7的负载均衡与集群配置.doc_第3页
基于mod_proxy Apache 2.2.16 Tomcat 7的负载均衡与集群配置.doc_第4页
基于mod_proxy Apache 2.2.16 Tomcat 7的负载均衡与集群配置.doc_第5页
免费预览已结束,剩余10页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置 Peter Wei周日晚和GF的老乡们喝了五粮液,导致周一起不了床,只好请假在家睡觉。白天睡了半天,晚上自然睡不着觉。正好现在的项目中也用到了负载均衡和集群的东西,虽然有新手贴的危险,但还是手痒,决定写点东西,以备不时之需。也希望能对大家有所帮助。 第一章. 背景简介 对于大多数企业应用,都希望能做到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),现在为止应该是最新的稳定版本,下载地址: /download.cgi 3, Tomcat 7.0.2,目前也是最新的版本。Minimum Java Version1.6.下载地址:/download-70.cgi 4, 安装过程略 第三章. 部署图 第四章. Tomcat7集群配置 一、 就地取材,复制tomcat7/webapps下的examples,重命名为cluster应用,以后就用cluster做测试。 二、 详细配置参照tomcat7 webappsdocscluster-howto.html 或者/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 its own by autodetecting available ports in the range 4000-4100。参照四中的注释 Make sure your web.xml has the element or set at your 参照四 If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine and that the jvmRoute attribute value matches your worker name in perties .用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, 我们采用的是默认的配置,在节点下添加: Java代码 ReceiverclassName=org.apache.catalina.tribes.transport.nio.NioReceiveraddress=auto color=redport=4000/color autoBind=100selectorTimeout=5000maxThreads=6/ Receiver className=org.apache.catalina.tribes.transport.nio.NioReceiver address=auto color=redport=4000/color autoBind=100 selectorTimeout=5000 maxThreads=6/ 2. 修改,仅为了调试方便。 3. tomcat7_awebappsclusterWEB-INFweb.xml中加入 4. ,添加jvmRoute属性,此项为后面apache负载均衡用到。 五、 复制一份tomcat7_a应用,改名为tomcat7_b.只是为了图方便,实际应该复制的是前面的cluster工程。注意以下几点配置就ok. 1. 修改tomcat7_a/conf/server.xml 中的Server port属性,因为是同一台机器两个tomcat应用,所以改一下。 2. 修改,同理,为了避免同一台机器端口号冲突。部置在不同的机器是不用管的。 3. 修改,此项为后面apache负载均衡用到。 4. 修改 5. tomcat7_bebappsclusterWEB-INFweb.xml中加入 到此集群配置完成. 第五章. 集群测试 1. tomcat7_a和tomcat7_b的cluster工程中分别添加测试文件:testCluster.jsp Java代码 ClusterTest % /HttpSessionsession=request.getSession(true); System.out.println(session.getId(); out.println(SESSIONID:+session.getId()+); /如果有新的请求,则添加session属性 Stringname=request.getParameter(name); if(name!=null&name.length()0) Stringvalue=request.getParameter(value); session.setAttribute(name,value); out.print(SessionList:); Enumerationnames=session.getAttributeNames(); while(names.hasMoreElements() Stringsname=names.nextElement(); Stringvalue=session.getAttribute(sname).toString(); out.println(sname+=+value+); System.out.println(sname+=+value); % 名称: 值: Cluster Test% /HttpSession session = request.getSession(true); System.out.println(session.getId(); out.println( SESSION ID: + session.getId()+); / 如果有新的请求,则添加session属性 String name = request.getParameter(name); if (name != null & name.length() 0) String value = request.getParameter(value); session.setAttribute(name, value); out.print(Session List:); Enumeration names = session.getAttributeNames();while (names.hasMoreElements() String sname = names.nextElement(); String value = session.getAttribute(sname).toString();out.println( sname + = + value+); System.out.println( sname + = + value); % 名称: 值: 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方式。 详细查看:tomcat7webappsdocsbalancer-howto.html 1. 首先,监听8000端口, 在Apache安装目录下找到conf/httpd.conf文件 稍前面加上 Java代码 1. #监听端口和监听地址 2. Listen8000# 监听端口和监听地址Listen 80002. conf/httpd.conf,去掉以下文本前的注释符(#)以便让Apache在启动时自动加载代理(proxy)模块 Java代码 LoadModuleproxy_modulemodules/mod_proxy.so LoadModuleproxy_ajp_modulemodules/mod_proxy_ajp.so LoadModuleproxy_balancer_modulemodules/mod_proxy_balancer.so LoadModuleproxy_connect_modulemodules/mod_proxy_connect.so LoadModuleproxy_ftp_modulemodules/mod_proxy_ftp.so LoadModuleproxy_http_modulemodules/mod_proxy_http.soLoadModule 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 3. conf/httpd.conf文件最后加上: Java代码 #虚拟机配置,负载均衡配置 ServerA ServerNamelocalhost ServerAliaslocalhost #小心,有些地方要有空格,要不然会出错哈哈。 ProxyPass/balancer:/cluster/stickysession=JSESSIONID|jsessionidnofailover=On ProxyPassReverse/balancer:/cluster/ #ErrorLoglogs/error.log#CustomLoglogs/access.logcommon #TheProxyRequestsdirectiveshouldusuallybesetoffwhenusingProxyPass. ProxyRequestsOff BalancerMemberajp:/localhost:8009loadfactor=1route=tomcat7_asmax=5max=20ttl=120retry=300timeout=15 BalancerMemberajp:/localhost:9009loadfactor=1route=tomcat7_bsmax=5max=20ttl=120retry=300timeout=15 #status=+H为配置热备,当所有机器都over时,才会请求该机器 #BalancerMember18:8009status=+H ProxySetlbmethod=bytraffic #虚拟机配置,负载均衡配置ServerAdmin ServerName localhostServerAlias localhost#小心,有些地方要有空格,要不然会出错哈哈。ProxyPass / balancer:/cluster/ stickysession=JSESSIONID|jsessionid nofailover=OnProxyPassReverse / balancer:/cluster/#ErrorLog logs/error.log#CustomLog logs/access.log common #The ProxyRequests directive should usually be set off when using ProxyPass.ProxyRequests OffBalancerMember ajp:/localhost:8009 loadfactor=1 route=tomcat7_a smax=5 max=20 ttl=120 retry=300 timeout=15BalancerMember ajp:/localhost:9009 loadfactor=1 route=tomcat7_b smax=5 max=20 ttl=120 retry=300 timeout=15# status=+H为配置热备,当所有机器都over时,才会请求该机器#BalancerMember 18:8009 status=+HProxySet lbmethod=bytraffic4. Tomcat7配置(server.xml): 因为是同一机器,两个应用,所以配不同的端口,不同机器则不用配,要和前面的ajp对应上。 第八章. 负载均衡测试 Tomcat7_a的cluster工程的测试文件:testCluster.jsp加上html代码: 负载均衡测试:此为:Tomcat7_a上的文件,aaaaaaaaaaaaaaaaaa Tomcat7_b的cluster工程的测试文件:testCluster.jsp加上html代码 负载均衡测试:此为:Tomcat7_b上的文件,bbbbbbbbbbbbbbbbbb 打开浏览器,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论