Apache+Tomcat负载均衡及Session绑定的实现_第1页
Apache+Tomcat负载均衡及Session绑定的实现_第2页
Apache+Tomcat负载均衡及Session绑定的实现_第3页
Apache+Tomcat负载均衡及Session绑定的实现_第4页
Apache+Tomcat负载均衡及Session绑定的实现_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、Apache+Tomcat负载均衡及Session绑定的实现集群背景介绍1.1 术语定义服务软体是b/s或c/s结构的s部分,是为b或c提供服务的服务性软件系统。服务硬体指提供计算服务的硬件、比如pc机、pc服务器。服务实体通指服务软体和服务硬体。客户端指接受服务实体服务的软件或硬件。1.2 两大关键特性集群是一组协同工作的服务实体,用以提供比单一服务实体更具扩展性与可用性的服务平台。在客户端看来,一个集群就象是一个服务实体,但事实上集群由一组服务实体组成。与单一服务实体相比较,集群提供了以下两个关键特性:· 可扩展性集群的性能不限于单一的服务实体,新的服务实体可以动态地加入到集群,

2、从而增强集群的性能。· 高可用性集群通过服务实体冗余使客户端免于轻易遇到out of service的警告。在集群中,同样的服务可以由多个服务实体提供。如果一个服务实体失败了,另一个服务实体会接管失败的服务实体。集群提供的从一个出错的服务实体恢复到另一个服务实体的功能增强了应用的可用性。1.3 两大能力为了具有可扩展性和高可用性特点,集群的必须具备以下两大能力:· 负载均衡负载均衡能把任务比较均衡地分布到集群环境下的计算和网络资源。· 错误恢复由于某种原因,执行某个任务的资源出现故障,另一服务实体中执行同一任务的资源接着完成任务。这种由于一个实体中的资源不能工作,

3、另一个实体中的资源透明的继续完成任务的过程叫错误恢复。负载均衡和错误恢复都要求各服务实体中有执行同一任务的资源存在,而且对于同一任务的各个资源来说,执行任务所需的信息视图(信息上下文)必须是一样的。1.4 两大技术实现集群务必要有以下两大技术:· 集群地址集群由多个服务实体组成,集群客户端通过访问集群的集群地址获取集群内部各服务实体的功能。具有单一集群地址(也叫单一影像)是集群的一个基本特征。维护集群地址的设置被称为负载均衡器。负载均衡器内部负责管理各个服务实体的加入和退出,外部负责集群地址向内部服务实体地址的转换。有的负载均衡器实现真正的负载均衡算法,有的只支持任务的转换。只实现任

4、务转换的负载均衡器适用于支持ACTIVE-STANDBY的集群环境,在那里,集群中只有一个服务实体工作,当正在工作的服务实体发生故障时,负载均衡器把后来的任务转向另外一个服务实体。· 内部通信为了能协同工作、实现负载均衡和错误恢复,集群各实体间必须时常通信,比如负载均衡器对服务实体心跳测试信息、服务实体间任务执行上下文信息的通信。具有同一个集群地址使得客户端能访问集群提供的计算服务,一个集群地址下隐藏了各个服务实体的内部地址,使得客户要求的计算服务能在各个服务实体之间分布。内部通信是集群能正常运转的基础,它使得集群具有均衡负载和错误恢复的能力。集群配置从上图可知,由服务实体1、服务实

5、体2和负载均衡器组成了一个集群。服务实体1和服务实体2参与对客户端的服务支持工作,均衡负载器为客户端维护集群的单一影像。集群实体间通过内部的通信网交流信息,这种交流机制一般采用组播协议。负载均衡器通过内部通信网探测各服务实体的心跳信息,服务实体间通过内部通信网完成任务资源的传播。可以看出,配置集群主要由配置服务实体和配置负载均衡器两部分组成。本文使用tomcat 5.5.20、apache 2.2.11配置集群环境。2.1 准备软件· tomcat是开源服务器,下载地点 /download-55.cgi;(本文使用的apache-tomc

6、at-5.5.20.zip)· apache是开源的www服务器,下载地点 /download.cgi;(本文使用的apache_2.2.11-win32-x86-no_ssl.msi)· jk2模块,jk 是mod_jserv的替代者,它是Tomcat-Apache插件,处理Tomcat和Apache之间的通信,在集群配置中充当负载均衡器的作用。JK2是符合apache 2.x系列的新品,下载地址 /tomcat/tomcat-connectors/jk/binaries/win32

7、/jk-1.2.28/。(本文使用的mod_jk-1.2.28-httpd-2.2.3.so)2.2 配置负载均衡器在apache下配置负载均衡器分为三步,注意每次修改httpd.conf和perties时不要忘了重新启动apache。· 第一步,安装和调试apache负载均衡器jk2模块是apache www 服务的插件,所以配置负载均衡器就得先安装apache。本文下载的是windows版本 2.2.11,执行msi并回答一些简单问题就可完成apache的任务。值得注意的是,安装并启动apache后如果apache对http:/localhost/ 地址没反

8、应,你得修改apache安装路径下htdocs目录下的index.html.xx文件,比如把index.html.en改成index.html。· 第二步,安装jk2把下载的 mod_jk-1.2.28-httpd-2.2.3.so 改名为 mod_jk2.so 放到apache的modules目录下,修改apache的conf目录下的httpd.conf,在文件最后加入一行include confmod_jk2.conf,在conf目录下创建mod_jk2.conf文件,内容如下:Conf代码 1 # Load mod_jk2 module 1 LoadModule jk_modu

9、le modules/mod_jk2.so 111 # Where to find perties 1 JkWorkersFile conf/perties 111 # Where to put jk logs 1 JkLogFile logs/mod_jk2.log 111 # Set the jk log level debug/error/info 1 JkLogLevel info 111 # Select the log format 1 JkLogStampFormat "%a %b %d %H:%M:%S %Y "

10、111 # JkOptions indicate to send SSL KEY SIZE, 1 JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories 111 # JkRequestLogFormat set the request format 1 JkRequestLogFormat "%w %V %T" 11 # Send JSPs for context / to worker named loadBalancer 1 JkMount /*.jsp loadBalancer # Load mo

11、d_jk2 moduleLoadModule jk_module modules/mod_jk2.so# Where to find pertiesJkWorkersFile conf/perties# Where to put jk logsJkLogFile logs/mod_jk2.log# Set the jk log level debug/error/infoJkLogLevel info# Select the log formatJkLogStampFormat "%a %b %d %H:%M:%S %Y "# J

12、kOptions indicate to send SSL KEY SIZE,JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories# JkRequestLogFormat set the request formatJkRequestLogFormat "%w %V %T"# Send JSPs for context / to worker named loadBalancerJkMount /*.jsp loadBalancer· 第三步,配置jk2jk2的配置全在一个配置文件中,文件名

13、为perties,和apache 的httpd.conf放在同一个目录下。以下是这个文件的内容:Properties代码 1 # 1 # perties 1 # 11 # list the workers by name 11 worker.list=tomcat1, tomcat2, loadBalancer 11 # - 1 # First tomcat server 1 # - 1 worker.tomcat1.port=8009 1 worker.tomcat1.host=07 1 worker.tomcat1.type

14、=ajp13 11 # Specify the size of the open connection cache. 1 #worker.tomcat1.cachesize 11 # 1 # Specifies the load balance factor when used with 1 # a load balancing worker. 1 # Note: 1 # -> lbfactor must be > 0 1 # -> Low lbfactor means less work done by the worker. 1 worker.tomcat1.lbfact

15、or=100 111 # - 1 # Second tomcat server 1 # - 1 worker.tomcat2.port=8009 1 worker.tomcat2.host=63 1 worker.tomcat2.type=ajp13 11 # Specify the size of the open connection cache. 1 #worker.tomcat2.cachesize 11 # 1 # Specifies the load balance factor when used with 1 # a load balancing work

16、er. 1 # Note: 1 # -> lbfactor must be > 0 1 # -> Low lbfactor means less work done by the worker. 1 worker.tomcat2.lbfactor=100 111 # - 1 # Load Balancer worker 1 # - 11 # 1 # The loadbalancer (type lb) worker performs weighted round-robin 1 # load balancing with sticky sessions. 1 # Note:

17、1 # -> If a worker dies, the load balancer will check its state 1 # once in a while. Until then all work is redirected to peer 1 # worker. 1 worker.loadBalancer.type=lb 1 worker.loadBalancer.balanced_workers=tomcat1, tomcat2 11 # 1 # END perties 1 # # perties# list the worke

18、rs by nameworker.list=tomcat1, tomcat2, loadBalancer# -# First tomcat server# -worker.tomcat1.port=8009worker.tomcat1.host=07worker.tomcat1.type=ajp13# Specify the size of the open connection cache.#worker.tomcat1.cachesize# Specifies the load balance factor when used with# a load balanci

19、ng worker.# Note:# -> lbfactor must be > 0# -> Low lbfactor means less work done by the worker.worker.tomcat1.lbfactor=100# -# Second tomcat server# -worker.tomcat2.port=8009worker.tomcat2.host=63worker.tomcat2.type=ajp13# Specify the size of the open connection cache.#worker.tom

20、cat2.cachesize# Specifies the load balance factor when used with# a load balancing worker.# Note:# -> lbfactor must be > 0# -> Low lbfactor means less work done by the worker.worker.tomcat2.lbfactor=100# -# Load Balancer worker# -# The loadbalancer (type lb) worker performs weighted round-r

21、obin# load balancing with sticky sessions.# Note:# -> If a worker dies, the load balancer will check its state# once in a while. Until then all work is redirected to peer# worker.worker.loadBalancer.type=lbworker.loadBalancer.balanced_workers=tomcat1, tomcat2# END perties#· 对于jk2模

22、块的负载均衡配置可参见相关站点,值得提的是jk2的负载均衡还支持权重分配等优秀功能。测试:在跑tomcat1实例的机器的 webapps/testGroup 测试项目目录生成如下内容的index.jsp文件:(设置session)Jsp代码 2 <% 3 System.out.println("="); 4 System.out.println(session.getAttribute("test"); 5 session.setAttribute("test","Session"); 6 %> 78

23、<html> 9 <body bgcolor="red"> 10 <center> 11 <h1>Tomcat 1</h1> 12 </body> 13 </html> <% System.out.println("="); System.out.println(session.getAttribute("test"); session.setAttribute("test","Session");%>

24、;<html> <body bgcolor="red"> <center> <h1>Tomcat 1</h1> </body></html>同理,在跑tomcat2实例的机器的 webapps/testGroup 测试项目目录生成如下内容的index.jsp文件:Jsp代码 14 <% 15 System.out.println("="); 16 System.out.println(session.getAttribute("test"); 1

25、7 %> 1819 <html> 20 <body bgcolor="blue"> 21 <center> 22 <h1>Tomcat 2</h1> 23 </body> 24 </html> <% System.out.println("="); System.out.println(session.getAttribute("test");%><html> <body bgcolor="blue&quo

26、t;> <center> <h1>Tomcat 2</h1> </body></html>启动Tomcat1, Tomcat2 和Apache服务Apache服务器ip:05Tomcat1服务器ip:07Tomcat2服务器ip:63首先验证Apache服务器的静态页面是否正常,访问:05/(若局域网内不能访问请检查防火墙设置)验证tomcat和apache配置提供负载均衡,用不同的终端访问:05

27、/testGroup/index.jsp如果你看到红色的页面,表示为tomcat1 服务器返回的,如果你看到兰色的页面,表示为tomcat2 服务器返回的。2.3 配置tomcat(绑定session)同属于一个集群下的两个服务实体,要求功能的同一性,所以我们可先安装和配置第一个tomcat,接着拷贝形成第二个tomcat,最后配置第二个tomcat。2.3.1修改tomcat1, tomcat2的server.xml 修改 <Engine /> 标签在跑第一个tomcat实例的机器上,在大约第120行(去掉注释), 将<Engine name="St

28、andalone" defaultHost="localhost" debug="0">替换为:<Engine jvmRoute="tomcat1" name="Standalone" defaultHost="05" debug="0" />对跑第二个tomcat实例的机器上,替换为 jvmRoute="tomcat2".Xml代码 25 <!- You should set jvmRoute to

29、 support load-balancing via AJP ie : 26 <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1"> 27 -> 28 <Engine jvmRoute="tomcat2" name="Standalone" defaultHost="05" debug="0" /> 29<!- You

30、should set jvmRoute to support load-balancing via AJP ie :<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">-> <Engine jvmRoute="tomcat2" name="Standalone" defaultHost="05" debug="0" /> 有的

31、文档写得是在配置中加入一行<Engine jvmRoute="tomcat1" name="Standalone" defaultHost="localhost" debug="0"></Engine>,其实是不对的,这样负载均衡是没有问题的,但是不能做session绑定,也就是说同一用户会在两台服务器上跳来跳去。这是由于jvmRoute不正确引起的,因为如果只是加入一行的话,是没有什么作用的,Engine仍然是原来的 Engine,而原来的Engine中jvmRoute是不正确的,所以一定

32、要替换。而且这个jvmRoute一定要跟 perties 中的名称完全匹配,否则也不能实现session绑定. 修改 <Cluster /> 标签将<Cluster />标签注释去掉,代码如下:Xml代码 30 <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" 31 managerClassName="org.apache.catalina.cluster.session.DeltaManager"

33、32 expireSessionsOnShutdown="false" 33 useDirtyFlag="true" 34 notifyListenersOnReplication="true"> 3536 <Membership 37 className="org.apache.catalina.cluster.mcast.McastService" 38 mcastAddr="" 39 mcastPort="45564" 40 mcastFr

34、equency="500" 41 mcastDropTime="3000"/> 4243 <Receiver 44 className="org.apache.catalina.cluster.tcp.ReplicationListener" 45 tcpListenAddress="auto" 46 tcpListenPort="4001" 47 tcpSelectorTimeout="100" 48 tcpThreadCount="6"/&

35、gt; 4950 <Sender 51 className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" 52 replicationMode="pooled" 53 ackTimeout="15000" 54 waitForAck="true"/> 5556 <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve" 57 filte

36、r=".*.gif;.*.js;.*.jpg;.*.png;.*.htm;.*.html;.*.css;.*.txt;"/> 5859 <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer" 60 tempDir="/tmp/war-temp/" 61 deployDir="/tmp/war-deploy/" 62 watchDir="/tmp/war-listen/" 63 watchEnab

37、led="false"/> 6465 <ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/> 66 </Cluster> <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" managerClassName="org.apache.catalina.cluster.session.

38、DeltaManager" expireSessionsOnShutdown="false" useDirtyFlag="true" notifyListenersOnReplication="true"> <Membership className="org.apache.catalina.cluster.mcast.McastService" mcastAddr="" mcastPort="45564" mcastFrequency

39、="500" mcastDropTime="3000"/> <Receiver className="org.apache.catalina.cluster.tcp.ReplicationListener" tcpListenAddress="auto" tcpListenPort="4001" tcpSelectorTimeout="100" tcpThreadCount="6"/> <Sender className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" replicationMode="pooled" ackTimeout="15000" waitForAck="true"/> <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve" filter=".*.

温馨提示

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

评论

0/150

提交评论