Apache+Tomcat+JK实现负载均衡和群集的完整过程_第1页
Apache+Tomcat+JK实现负载均衡和群集的完整过程_第2页
Apache+Tomcat+JK实现负载均衡和群集的完整过程_第3页
Apache+Tomcat+JK实现负载均衡和群集的完整过程_第4页
Apache+Tomcat+JK实现负载均衡和群集的完整过程_第5页
已阅读5页,还剩75页未读 继续免费阅读

下载本文档

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

文档简介

1、linux中apache+tomcat+jk实现负载均衡和群集的完整过程. 分类: apache tomcat server 2012-08-08 12:29 412 人阅读 评论(0)收藏 举报 转载于:最近在开发的项目需要承受很高的并发量。综合各种情况,决定使用apache+tomcat+jk的方式实现负载均衡,并且作为一个统一的服务 还要实现群集(同步session )。在网上找了很多资料,都是零零散散的,没有一个完整的过程。通过几 天的努力,完成了从编译、部署到配置的整个过程,期间也遇到了一些 问题。在接下来的文字中将这些过程记录下来,做个笔记同时也分享给 大家。为了重新演示整个过程,

2、我新搭建了一个服务器,各项参数如下:cpu: intel xeon 5110内存:ddr2 1g*4主机型号:proliant dl140 g3操作系统:red hat enterprise linux server release 5.4 x86_64 版 内核版本:2.6.18gcc 版本:4.1.2g+版本:4.1.2java 版本:1.6.0_27plain view plaincopyprint?1. rootlxp2 # cat /etc/redhat-release2. red hat enterprise linux server release 5.4 (ti kanga)3

3、. rootlxp2 # uname -a4. linux lxp2 2.6.18-164.el5xen #1 smp thu sep 3 0 4:41:04 edt 2009 x86_64 x86_64 x86_64 gnu/linux5. rootlxp2 # gcc -version6. gcc (gcc)4.1.2 20080704 (red hat4.1.2-46)7. copyright (c) 2006 free software foundation, in c.8. this is free software; see the source for copying condi

4、tions. there is no9. warranty; not even for merchantability orfitne ss for a particular purpose.10.11. rootlxp2 # g+ -version12. g+ (gcc) 4.1.2 20080704 (red hat 4.1.2-46)13. copyright (c) 2006 free software foundation, inc.14. this is free software; see the source for copying conditions. there is n

5、o15. warranty; not even for merchantability or fitness for a particular purpose.16.17. rootlxp2 # java -version18. j ava version 1.6.0_2719. java(tm) se runtime environment (build 1.6.0_27-b07)20. java hotspot(tm) 64-bit server vm (build 20.2-b06, mixed mode)21. rootlxp2 #plain view plaincopyprint?1

6、. rootlxp2 # cat /etc/redhat-release2. red hat enterprise linux server release 5.4 (tikanga)3. rootlxp2 # uname -a4. linux lxp2 2.6.18-164.el5xen #1 smp thu sep 3 04:41:04 edt 2009 x86_64x86_64 x86_64 gnu/linux5. rootlxp2 # gcc -version6. gcc (gcc) 4.1.2 20080704 (red hat 4.1.2-46)7. copyright (c) 2

7、006 free software foundation, inc.8. this is free software; see the source for copying conditions. there is no9. warranty; not even for merchantability or fitness for a particular purp ose.10.11. rootlxp2 # g+ -version12. g+ (gcc) 4.1.2 20080704 (red hat 4.1.2-46)13. copyright (c) 2006 free software

8、 foundation, inc.14. this is free software; see the source for copying conditions. there is no15. warranty; not even for merchantability or fitness for a particular purp ose.16.17. rootlxp2 # java -version18. j ava version 1.6.0_2719. java(tm) se runtime environment (build 1.6.0_27-b07)20. java hots

9、pot(tm) 64-bit server vm (build 20.2-b06, mixed mode)21. rootlxp2 # gcc、g+和java是必须的,如果运行上述命令提示command notfound,则需要安装。具体安装方法这里不做介绍,请参阅相关文档。接下来要准备的是apache服务器、tomcat服务器和jk连接器1 .下载apache服务器源码包apache服务器官方没有发布编译好的linux二进制包,只能通过下载源 代码,然后自己编译。因此需要先下载源码。访问网址 /download.cgi ,可以看至u apache

10、服务 器目前放出的版本信息,推荐使用稳定版的release 。downloading the apache http serveruse the linki below to dotiload the apache http our main distnbution directon.only current recotiiniended releases are available on t archive doinl,ad site.stable release:, 2221 (包己的心2011-09-13) |beta release: 2 3-5-b&a (released 2011-

11、11-15)l己空icy release: 2.0,64 (released 2010-10-19)然后选择unix版源码:| *1向5口urx:如d-2 2f tar gzlpgpl fmdsl sha 匚向 pur:httpd-ur tarbjpgpl fxd5 sha 11,win32 source: httpd-owin335rjmpgp 43 sha1 win32 binary without crypto (no moclssl) (msi installerk htiyd 二 一 :山土 win32 binary including openssl 0.9.sr (vist in

12、staller): httpd-1.2217vinn, netware binary: apache 2221-nehvare.zip fpgp fmd51 1shah other2 .下载tomcat服务器源码包 目前tomcat服务器个人还是觉得6.0比较稳定。7.0毕竟是新出的东西,需要一定的生产实践考验才能达到理想的状态。因此这里选择tomcat 6.0。访问网址 /download-60.cgi ,可以看到目前稳定的版本为6.0.33 :6,0.33please see die re ad 4任 tile for packaging ii

13、ifbnnatioti it exbinary distributions* core: zip (pgp. md5)tar且 sgp=md5)口 3 rbit mndgw5芯(pgp, tndf)o 64-bit % indow$ zip (pgp, md5) 64 jbit itanium t-indows zip fogp, md5)0 3bit64-bit windok 占 eervi installer这里强烈建议下载tar.gz格式的压缩包。在linux下,文件访问有着严格的权限限制。一个文件是否允许以二进制或者脚本的形式执行,完全取决于其是否拥有执行缺陷,这与 windows识别

14、文件后缀名(.exe、.bat)的方式不同。zip格式的压缩包中是不保留文件的权限信息的,而tar.gz格式的压缩包是保存有文件的权限信息的。3.下载jk连接器源码包作为apache与tomcat连接的桥梁,jk连接器使用c语言编写,与apache紧密结合,作为模块装载到apache服务器中,通过配置实现与特定的tomcat服务器进行通信,从而实现负载均衡的功能。访问网址 /download-connectors.cgi ,可以找到最新最稳定的jk连接器版本:tomcat connectors jk 1.2for more infbniiation

15、 concenwig tomcat connectors (modjki:for your platfbnunix, linuxf pgp-md5c jk 1 二一 3二 scmriu 区目亡的亡 zip (已一日 w indows).pgp1.小id,binary rule 35 亡与 brow)巳 download arsa brc-., se arclj-. e这里还是推荐下载tar.gz格式的源码。原因同上。4 .解压apache服务器、tomcat服务器和jk连接器都已经下载好了,如下图所示:然后将这三个包都解压出来:rootlxp2downloadstar -xf apache-t

16、omcat-&.g root(aixp2downloads# tar-xf httpd-2.2.21.tarroot0lxp2 downloads# tar , raot01xp2 downloads# 11 total 52764xf tomcat - connectors -d rwxr-xr-x9 root root4白96 nov 18 13:62-rw - r - - r -1 root root10332168 nov 15 69:32d rwxr-xr-x11 search search4g96 sep 9 22;2g-rw - r- - r-1 root root3575898

17、0 nov 15 09:31drwxr-xr*x9 root bin4096 jul 2 13:47-rw-r-r-rootlxp21 root root downloads# |7854080 nov 15 19:6433 .tar1,2.32-src.tarapac he - tomcat - 6.0.33apache-tomcat-6,9 33.tarhttpd-2 2 21httpd-242.21ktartomcat-connectors-1,2,32*srctomcat-connectors-142.32-src.tar5 .编译apache服务器首先编译apache服务器。在编译之

18、前需要执行其自带的检测配置脚本。 对于不同发行版本的linux,默认安装的库都有所差别,即便是同一个 发行版本,由于用户安装软件的软件不同,也会导致系统内包含的库有 所区别。因此apache作为开源服务器,在编译前需要了解系统的库安 装情况,某些模块需要依赖于特定的库,如果这些库不存在,配置脚本 将自动忽略这些库的编译。经过检测时候会生成合适的makefile文件。这里特别提醒一句,如果直接执行配置脚本,是不会编译额外的模块的, 我们希望使用额外模块时,需要在运行配置脚本命令后加入参数,让其尽最大可能编译可用的库。关于这方面的介绍可以参阅我的另外一篇文章“linuxt编译apache服务器mo

19、dules文件夹缺少模块(.so)的问题” ( )。下面进入apache服务器源码目录并执行配置脚本:plain view plaincopyprint?1. rootlxp2 downloads# cd httpd-2.2.212. rootlxp2 httpd-2.2.21# ./configure -enable- so -enable-mods-shared=most -with-mpm=workerplain view plaincopyprint?1. rootlxp2 downloads# cd httpd-2.2.212. rootlxp2 httpd-2.2.21# ./co

20、nfigure -enable-so -enable-mods-shared= most -with-mpm=worker加入-with-mpm=worker是修改apache服务器的工作模式。默认模式是prefork。 prefork采用预派生子进程方式,用单独的子进程来处理 不同的请求,进程之间彼此独立。相对于 prefork, worker是全新的支持 多线程和多进程混合模型的mpm (多路处理模块)。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进 程的服务器。但是,worker也使用了多进程,每个进程又生成多个线程, 以获得基于进程服务器的稳定性。如果配置

21、过程中出现plain view plaincopyprint?1. configure: error: cannot use an external apr wi th the bundled apr-utilplain view plaincopyprint?1. configure: error: cannot use an externalapr with the bundled apr-util这样的错误信息,说明本机没有安装apr运行库,需要下载并安装。访问网址:/download.cgi ,下载 apr 和 apr-util :apr 145

22、 is the best available versionapr is die bass portability library.卜 tni?; sours: gpr-145 tar累pgp 1口可 unix source: pf-14 5 tar bu二pgp xed5 win32 source: apr=l4 5-win32-src-zip pgp atd51 sha1 otiier 61的aprmtfl 1.3.12 is the best available versionapr-util provides a number of helpful abstractions on to

23、p of apr卜 nix soureu; gpr-ntil-l3? tar gg pgp md5tl unix source: apr-iitil-l.l 二 tar b二pgp w indows source: apr-utd-13 1-win31-与ru zip pgp md51 , other files解压 apr 和 apr-utilplain view plaincopyprint?1. rootlxp2 downloads# tar -xf apr-1.4.5.tar.gz2. rootlxp2 downloads# tar -xf apr-util-1.3.12.tar.gz

24、plain view plaincopyprint?1. rootlxp2 downloads# tar -xf apr-1.4.5.tar.gz2. rootlxp2 downloads# tar -xf apr-util-1.3.12.tar.gz 进入apr,并编译plain view plaincopyprint?2. rootlxp2 apr-1.4.5# is3. apr-config.in build.conf helpers memoryshmem4. apr.depbuild-outputs.mk includemiscstrings5. apr.dspchanges lib

25、apr.depmmapsupport6. apr.dsw config.layout libapr.dsp network。tables7. apr.makconfigurelibapr.maknotice test8. apr.pc.inconfigure.inlibapr.rcnwgnumakefile threadproc9. apr.specdocslicensepasswdtime10. atomic dsolockspolluser11. buildemacs-modemakefile.inrandom12. buildconf file_io makefile.win readm

26、e13. rootlxp2 apr-1.4.5# ./configure6. apr.dswconfig.layout include meplain view plaincopyprint?1.rootlxp2 downloads# cd apr-.rootlxp2 apr-1.4.5# lsapr-config.in build.conf helpers memoryshmem4.apr.dep build-outputs.mk include miscstrings5.apr.dsp changeslibapr.dep mmapsupport6.apr.dsw confi

27、g.layout libapr.dsp network_io tables7.apr.mak configurelibapr.mak noticetest8.apr.pc.in configure.in libapr.rc nwgnumakefile threadpro9.capr.spec docslicense passwdtime10.atomicdsolockspolluser11.buildemacs-mode makefile.in random12.buildconf file_iomakefile.win readme13.rootlxp2 apr-1.4.5# ./confi

28、gure生成了 makefile后直接编译plain view plaincopyprint?1. rootlxp2 apr-1.4.5# ls2. apr-1-config buildconf dso lo ckspoll3. apr-config.in build.conf emacs-mode ma kefilerandom4. apr.dep build-outputs.mk file_io ma kefile.in readme5. apr.dsp changes helpers makefile.win shmemmory strings7. apr.makconfig.logli

29、bapr.dep miscsupport8. apr.pc config.nice libapr.dsp mm aptables9. apr.pc.in config.status libapr.mak ne twork_io test10. apr.specconfigurelibapr.rc notice threadproc11. atomicconfigure.inlibtool nwgnumakefile time12. builddocslicense passwduser13. rootlxp2 apr-1.4.5# makeplainview plaincopyprint?1.

30、rootlxp2 apr-1.4.5# ls2.apr-1-configbuildconfdsolockspoll3.apr-config.ini build.confemacs-mode makefile random4.apr.depbuild-outputs.mk file_io makefile.in readme5.apr.dspchangeshelpers makefile.win shmem6.apr.dswconfig.layoutinclude memorystrings7.apr.makconfig.loglibapr.dep miscsupport8.apr.pcconf

31、ig.nicelibapr.dsp mmaptables9.apr.pc.inconfig.statuslibapr.mak networkio test10. apr.specconfigurelibapr.rc notice threadproc11. atomicconfigure.inlibtool nwgnumakefile time12. build docs license passwd user13. rootlxp2 apr-1.4.5# make编译好之后使用root权限安装:plain view plaincopyprint?1. rootlxp2 apr-1.4.5#

32、sudo make installplain view plaincopyprint?1. rootlxp2 apr-1.4.5# sudo make install然后使用类似的方法配置apr-util :plain view plaincopyprint?1. rootlxp2 downloads# cd apr-util-1.3.122. rootlxp2 apr-util-1.3.12# ./configure -with-apr=/usr/local/aprplain view plaincopyprint?1. rootlxp2 downloads# cd apr-util-1.3

33、.122. rootlxp2 apr-util-1.3.12# ./configure -with-apr=/usr/local/apr编译 apr-util :plain view plaincopyprint?i. rootlxp2 apr-util-1.3.12# makeplain view plaincopyprint?1. rootlxp2 apr-util-1.3.12# make编译好之后使用root权限安装:plain view plaincopyprint?1. rootlxp2 apr-util-1.3.12# sudo make installplain view pl

34、aincopyprint?1. rootlxp2 apr-util-1.3.12# sudo make in stall当然如果你在配置apache服务器编译的时候没有提示缺少“apr,请忽略上面关于apr编译的几步。回到apache服务器源码所在目录,开始编译:plain view plaincopyprint?1. rootlxp2 httpd-2.2.21# makeplain view plaincopyprint?1. rootlxp2 httpd-2.2.21# make编译过程大概不到十分钟,完成之后使用 root权限进行安装plain view plaincopyprint?1

35、. rootlxp2 httpd-2.2.21# sudo make installplain view plaincopyprint?2. rootlxp2 httpd-2.2.21# sudo make insta ll如果不出意外,至此apache就安装成功了。来测试一下: 进入apache服务器的bin目录,并启动服务器:plain view plaincopyprint?3. rootlxp2 httpd-2.2.21# cd /usr/local/apache2 /bin/4. rootlxp2 bin# sudo ./apachectl start5. httpd: could

36、not reliably determine the servers fully qualified domain name, using for servernameplain view plaincopyprint?1. rootlxp2 httpd-2.2.21# cd /usr/local/a pache2/bin/2. rootlxp2 bin# sudo ./apachectl start3. httpd: could not reliably determine the servers fully qualified domainname, using 127.

37、0.0.1 for servername在本地打开浏览器,访问 如果出现“it works!则表示启动成功了file edit view history bookmarks rol 与 helpq -均) 合回 http:/127 0.0 1/京| most visited,3 enterprise linux linux 1echnit works!这里要注意一点就是linux的防火墙问题。如果你的linux服务器启动 了防火墙,本地访问上面的网址是没有问题的,但如果其它计算机访问 你的服务器有可能会连接失败。出现这种情况的原因是防火墙将入站 80端口封锁了。解

38、决方法是将80端口加入到允许列表中:进入防火墙设置后,如果发现 firewall状态为enabled ,表示防火墙已启用,需要将 www(http)服务标记为信任,如果需要使用 hhtps协议,还要将secure www(https)服务也标记为信任。如下图所示:另外,此时如果有其他程序占用80端口也是会影响到apache服务器的,需要确保这个端口没有被占用。还有我还要补充一点,在 mac os中按照上述方法安装apache服务器 是不行的。开始的时候我不想搭建linux服务器,想到mac os也是类 unix的系统,操作命令什么的都一样,就先在 mac上实验了。结果安 装上apache服务器

39、后启动了,每次访问都提示505错误,servicetemporarily unavailable 。经过查阅很多资料和尝试才发现,原来 mac 系统中已经自带了 apache服务器。具体应用是在 系统设置”中的共享, 功能。这个功能里有“web享”方式。其实现时使用的服务器就是apache。它采用的配置文件在/etc/httpd/目录中。这里的配置文件和自己安装的apache服务器配置文件冲突了,因此造成 505错误。这一点需要注意。(注:我是用的 mac系统为mac os x lion 10.7.2 )2011年11月23日补充:如果你希望把apache服务器注册为系统服务,让它随着系统启动

40、而启 动,则需要在/etc/init.d/目录中建立服务管理脚本,我们将其命名为 httpd :python view plaincopyprint?1. #!/bin/bash2. #chkconfig: 345 61 613. #description: this is apache http service4. #processname: httpd5. pidfile=/usr/local/apache2/logs/httpd.pid6. httpd_process_name=httpd7. httpd_path=/usr/local/apache2/bin/apachectl8.9.

41、 retval=010. start()11. echo starting apache httpd service.12. httpd_pid_list=pidof $httpd_process_name13. if test -n $httpd_pid_list14. then15. echo fail to launch httpd, since it has al ready started16. retval=117. else18. echo launching apache httpd server19. $httpd_path start20. retval=$?21. ech

42、o launch httpd successfully22. fi;23. 24.25. stop()26. echo stopping apache httpd service.27. httpd_pid_list=pidof $httpd_process_name28. if test -n$httpd_pid_list29. then30. echo find httpd process, start to end them 31. $httpd_path stop32. if test $? = 033. then34. echo success to terminate httpd

43、service35. retval=036. else37. echo can not terminate httpd service38. retval=139. fi;40. else41. echo can not find any httpd process, failto stop service42. retval=043. fi;44. 45.46. r estart()47. stop48. if test $?=049. then50. #sleep 3 seconds to wait for process exit51. sleep 352. start53. retva

44、l= $?54. else55. retval= $?56. fi;57. .58.59. status()60. if test -f$pidfile61. then62. pid_list=cat $pidfile63. echo $httpd_process_name (pid:$pid_list)is running64. else65. echo $httpd_process_name is stopped66. fi;67. 68.69. case $1 in70. start)71. start72. retval=$?73. ;74. stop)75. stop76. retv

45、al=$?77. ;78. restart)79. restart80. retval=$?81. ;82. status)83. status84. ;85. *)86. echo 1quot;usage:$0 start|stop|restartasdfasdfasdfasdf 87. retval=288. esac89.90. exit $retvalpython view plaincopyprint?1. #!/bin/bash2. #chkconfig: 345 61 613. #description: this is apache http service4. #proces

46、sname: httpd5. pidfile=/usr/local/apache2/logs/httpd.pid6. httpd_process_name=httpd7. httpd_path=/usr/local/apache2/bin/apachectl8.9. retval=010. start()11. echo starting apache httpd service.12. httpd_pid_list=pidof $httpd_process_name13. if test -n $httpd_pid_list14. then15. echo fail to launch ht

47、tpd, since it has already started16. retval=117. else18. echo launching apache httpd server19. $httpd_path start20. retval=$?21. echo launch httpd successfully22. fi;23. 24.25. stop()26. echo stopping apache httpd service.27. httpd_pid_list=pidof $httpd_process_name28. if test -n $httpd_pid_list29.

48、then30. echo find httpd process, start to end them31. $httpd_path stop32. if test $? = 033. then34. echo success to terminate httpdservice35. retval=036. else37. echo can not terminate httpdservice38. retval=139. fi;40. else41. echo can not find any httpd process, fail to stop service42. retval=043.

49、 fi;44. 45.46. restart()47. stop48. if test $?=049. then50. #sleep 3 seconds to wait for process exit51. sleep 352. start53. retval= $?54. else55. retval= $?56. fi;57. 58.59. status()60. if test -f $pidfile61. then62. pid_list=cat $pidfile63. echo $httpd_process_name (pid:$pid_list) is running64. el

50、se65. echo $httpd_process_name is stopped66. fi;67. 68.69. case $1 in70. start)71. start72. retval=$?73. ;74. stop)75. stop76. retval=$?77. ;78. restart)79. restart80. retval=$?81. ;82. status)83. status84. ;85. *)86. echo 1quot;usage:$0 start|stop|restart asdfasdfasdfasdf 87. retval=288. esac 89.90

51、. exit $retval编写完成后保存并赋予755权限。然后在该目录下执行plain view plaincopyprint?1. rootlxp2 init.d# chkconfig -add httpdplain view plaincopyprint?2. rootlxp2 init.d# chkconfig -add httpd 将服务添加到系统。脚本的具体解释请参阅我的另外一篇博文:linux中将memcached注册为系统服务(地址: )。6.编译jk连接器刚刚完成了 apache服务器的编译,接下来顺便把jk连接器也编译出来。进入刚刚解压出来的 tomcat-connector目录,再进入 native目录。执行配置:plain view plaincopyprint?1. rootlxp2 downloads# cd tomcat-connectors-1.2 .32-src2. rootlxp2 tomcat-connectors-1.2.32-src# ls3. build.txt

温馨提示

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

评论

0/150

提交评论