apache+tomcat+memcached实现负载均衡,session共享(摘抄+整合)

搞了三天终于把apache+tomcat+memcached实现负载均衡,session共享一个系统整套的东西给真机搭建起来了,还包括同一台机器多个tomcat实现启动,突然感觉自己好牛逼的感觉,但是事实告诉我才刚刚开始。。。以后还要真机试验mysql同步+heartbeat心跳检测。虚<span style="font-family:simsun;line-height: 21px; ">拟环境和真实环境还是有很大的不同的。涉及的东西很多。</span><br style="font-family: simsun; line-height: 21px; " /><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">三台机器</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">系统centos6.5</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">这边说下centos6.5系统网卡默认是关闭的,也就是说重启了网卡就关闭连不上了。很蛋疼吧。。。</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">修改/etc/sysconfig/network-scripts/ifcfg-em1中的ONBOOT=yes就可以了</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">主机apache 192.168.2.2</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">tomcat1 192.168.2.3</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">tomcat2 192.168.2.4</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">因为要编译安装apache每台机器上面需要yum install gcc gcc-c++ zlib zilb-devel autoconf libtool 会自动下载一大堆的包,安装就行</span><br style="font-family: simsun; line-height: 21px; " /><span style="font-family:simsun;line-height: 21px; ">http://blog.csdn.net/a05151990225/article/details/19123873参考这篇文章源码安装我的是httpd-2.26</span><br style="font-family: simsun; line-height: 21px; " /></span><p style="margin-top: 0px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; "><span style="background-color:rgb(255,255,255)"><span style="font-size:14px;">2.配置文件</span></span></p><p style="margin-top: 0px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; "><span style="background-color:rgb(255,255,255)"><span style="font-size:14px;">apache安装后,需要注意三个地方需要修改:</span></span></p><p style="margin-top: 0px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; "><span style="background-color:rgb(255,255,255)"><span style="font-size:14px;">1)/etc/hosts文件里面改为192.168.2.2 <wbr> (主机名) 或者127.0.0.1 (主机名)因为我没有联网,直接用路由器DHCP分配地址,如果不改的话apache启动不了,报错找不到DNS服务器</wbr></span></span></p><p style="margin-top: 0px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; "><span style="background-color:rgb(255,255,255)"><span style="font-size:14px;">2)在文件的最后需要配置以下文本,用于控制集群的成员。</span></span></p><span style="background-color:rgb(255,255,255)"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; "><span style="word-wrap:normal; word-break:normal"><span style="word-wrap:normal; word-break:normal"><span style="font-size:14px;"><span style="word-wrap:normal; word-break:normal"></span></span></span></span></span></span><pre code_snippet_id="197012" snippet_file_name="blog_20140220_1_9397539" name="code" class="html"><span style="font-size:14px;">ProxyPass /images !   
ProxyPass /css !   
ProxyPass /js !  
  
ProxyRequests Off  
ProxyPass /test balancer://mycluster/ stickysession=JSESSIONID  
  
ProxyPassReverse /test balancer://mycluster/ stickysession=JSESSIONID  
<</span>Proxy balancer://mycluster>  
     BalancerMember ajp://192.168.2.3:8009/test/ loadfactor=1   
     BalancerMember ajp://192.168.2.4:8009/test/ loadfactor=1  
</</span>Proxy> </span>

该配置为Apache的反向代理,用于通过apache去访问tomcat项目。test为博主的项目Context,两个BalanceMember是集群中的成员。8009端口是tomcat的ajp端口名。

最后的下面balancer-manager是用于查看负载均衡运行情况的路径配置。

该文本需要添加在  

<span style="font-size:14px;">SSLRandomSeed startup builtin  
SSLRandomSeed connect builtin  
 </span>
的上面。
3)最后还需要在
<span style="font-size:14px;">ProxyPass /images !   
ProxyPass /css !   
ProxyPass /js ! </span>
的上面加入下面配置      
SetHandler server-status  
     Order Deny,Allow  
     Deny from all  
     Allow from all  
<span style="font-size:14px;">  SetHandler balancer-manager  
     Order Deny,Allow  
     Deny from all  
     Allow from all  </span>

4.下载JK连接器源码包

作为apache与tomcat连接的桥梁,JK连接器使用C语言编写,与apache紧密结合,作为模块装载到apache服务器中,通过配置实现与特定的tomcat服务器进行通信,从而实现负载均衡的功能。

访问网址http://tomcat.apache.org/download-connectors.cgi,可以找到最新最稳定的JK连接器版本:

tomcat-connectors-1.2.37-src.tar.gz我下载的是这个版本的

刚刚完成了apache服务器的编译,接下来顺便把JK连接器也编译出来。

进入刚刚解压出来的tomcat-connector目录,再进入native目录。执行配置:

<span style="font-size:14px;">[root@lxp2 Downloads]# cd tomcat-connectors-1.2.32-src  
[root@lxp2 tomcat-connectors-1.2.32-src]# ls  
BUILD.txt  conf  docs  jkstatus  LICENSE  native  NOTICE  support  tools  xdocs  
[root@lxp2 tomcat-connectors-1.2.32-src]# cd native/  
[root@lxp2 native]# ls  
aclocal.m4    BUILDING.txt  configure.in  Makefile.am  nt_service  TODO.txt  
apache-1.3    CHANGES       docs          Makefile.in  README.txt  
apache-2.0    common        iis           netscape     scripts  
buildconf.sh  configure     jni           NEWS         STATUS.txt  
[root@lxp2 native]# ./configure --with-apxs=/usr/local/apache2/bin/apxs</span>
目录各位看着自己的修改
然后make && make install
编译完成后使用ls命令来列出native目录下的所有目录和文件。注意有apache-1.3和apache-2.0两个目录。由于在配置编译的时候指 定了apxs工具的位置。配置脚本会根据apxs的反馈结果自动识别目标apache服务器为2.x版本,因此本次编译生成的mod_jk.so模块会放 在apache-2.0目录中,apache-1.3目录中是没有mod_jk.so的,这一点请注意。如下所示:
<span style="font-size:14px;">[root@lxp2 native]# ls  
aclocal.m4    CHANGES        configure     libtool      NEWS        TODO.txt  
apache-1.3    common         configure.in  Makefile     nt_service  
apache-2.0    config.log     docs          Makefile.am  README.txt  
buildconf.sh  config.nice    iis           Makefile.in  scripts  
BUILDING.txt  config.status  jni           netscape     STATUS.txt  
[root@lxp2 native]# cd apache-2.0/  
[root@lxp2 apache-2.0]# ls  
bldjk54.qclsrc  Makefile.apxs     mod_jk.a    mod_jk.lo  
bldjk.qclsrc    Makefile.apxs.in  mod_jk.c    mod_jk.o  
config.m4       Makefile.in       mod_jk.dsp  mod_jk.so  
Makefile        Makefile.vc       mod_jk.la   NWGNUmakefile  
[root@lxp2 apache-2.0]#   </span>
我们现在将编译好的mod_jk.so拷贝到apache服务器的modules目录中,这个目录是专门用来存放扩展模块的:
<span style="font-size:14px;">[root@lxp2 apache-2.0]# sudo cp ./mod_jk.so /usr/local/apache2/modules/  
[root@lxp2 apache-2.0]# cd /usr/local/apache2/modules/  
[root@lxp2 modules]# ls  
httpd.exp               mod_authz_user.so   mod_include.so  
mod_actions.so          mod_autoindex.so    mod_info.so  
mod_alias.so            mod_cgi.so          mod_jk.so  
mod_asis.so             mod_dav_fs.so       mod_log_config.so  
mod_auth_basic.so       mod_dav.so          mod_logio.so  
mod_auth_digest.so      mod_dbd.so          mod_mime.so  
mod_authn_anon.so       mod_deflate.so      mod_negotiation.so  
mod_authn_dbd.so        mod_dir.so          mod_reqtimeout.so  
mod_authn_dbm.so        mod_dumpio.so       mod_rewrite.so  
mod_authn_default.so    mod_env.so          mod_setenvif.so  
mod_authn_file.so       mod_expires.so      mod_speling.so  
mod_authz_dbm.so        mod_ext_filter.so   mod_status.so  
mod_authz_default.so    mod_filter.so       mod_substitute.so  
mod_authz_groupfile.so  mod_headers.so      mod_userdir.so  
mod_authz_host.so       mod_ident.so        mod_version.so  
mod_authz_owner.so      mod_imagemap.so     mod_vhost_alias.so </span>

至此JK连接器模块就部署完成了,但是还需要配置,具体配置将在下文中详细描述。

以上jk内容转载自http://blog.csdn.net/chaijunkun/article/details/6987443#1536434-tsina-1-75584-66a1f5d8f89e9ad52626f6f40fdeadaa


4.安装java

java我是在www.sun.com上面下载的rpm包安装的

jdk-7u51-linux-x64.rpm直接解压之后

#vi /etc/profile

在最后面加入

JAVA_HOME=/usr/java/jdk1.7.0_51
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
ulimit -n 100000

然后java -version查看版本对了就行了


5.Tomcat配置文件

我是下载的tomcat7,可以去官网下载。

首先说一点,Tomcat也只是配置一个文件,就是/tomcat1/conf/server.xml。并且里面的端口无需改动,因为是在真正的分布式中来实现的。只需要修改一个地方,就是Engine节点。

在之前的Engine节点上,加入一个属性jvmRoute,值是刚才我们提到的tomcat1和tomcat2,下面的配置就放在

具体配置如下:

  1. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  
  2.   
  3.       <!--For clustering, please take a look at documentation at:  
  4.           /docs/cluster-howto.html  (simple how to)  
  5.           /docs/config/cluster.html (reference documentation) -->  
  6.         
  7.       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  
  8.     channelSendOptions="8">  
  9.         
  10.      <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  11.                    expireSessionsOnShutdown="false"  
  12.                    notifyListenersOnReplication="true"/>  
  13.   
  14.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  15.             <Membership className="org.apache.catalina.tribes.membership.McastService"  
  16.                         address="228.0.0.4"  
  17.                         port="45564"  
  18.                         frequency="500"  
  19.                         dropTime="3000"/>  
  20.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  21.                       address="192.168.2.2"  
  22.                       port="4000"  
  23.                       autoBind="100"  
  24.                       selectorTimeout="5000"  
  25.                       maxThreads="6"/>  
  26.   
  27.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  28.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  29.             </Sender>  
  30.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  31.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  32.           </Channel>  
  33.   
  34.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>  
  35.           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  
  36.   
  37.     <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  38.                     tempDir="/tmp/war-temp/"  
  39.                     deployDir="/tmp/war-deploy/"  
  40.                     watchDir="/tmp/war-listen/"  
  41.                     watchEnabled="false"/>  
  42.           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  
  43.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  44.      </Cluster>   

上面的配置是我们提到的worker2的配置,是在子节点2上配置的,如果在子节点1上,则只需要将其改成tomcat1,另外还要把Receiver的address修改成当前所在机器的IP地址。

最后把所要操作的项目放到tomcat的webapp下,另外项目本身的web.xml文件中,需要引入<distributable/>元素。

<span style="font-size:14px;"><?xml version="1.0" encoding="gbk"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<title>cluster测试</title> 
</head> 

<body> 
<% 
out.println("this is tomcat1");//在另一台机子上为”this is tomcat2” 

String sessionid = session.getId(); 
out.println("sessionid = " + sessionid); 
%> 
</body> 
</html> </span>


测试的地址  http://192.168.2.2:80/test

测试查看的负载均衡情况地址   http://192.168.2.2:80/balancer-manager

非常感谢



下面是session共享

我做session共享试过修改apache本身的httpd.conf文件,没有成功,而且也了解到apache本身的session共享功能很弱,所以我就选择了memcached来实现session共享

1. memcache的安装及启动

  1. 1.下载包  
  2.   
  3. 1.1.到http://memcached.org/下载linux下的memcached的安装包.  
  4.   
  5. 1.2.到http://libevent.org/ 下载安装memcached前比需要的包libevent-2.0.15我安装的是这个版本.  
  6.   
  7. 2.安装libevent-2.0.15  
  8.   
  9. # tar zxvf libevent-2.0.5-beta.tar.gz  
  10. # cd libevent-2.0.5  
  11. # ./configure –prefix=/usr  
  12. # make  
  13. # make install  
  14.   
  15. 如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。(网上照搬.中间我只是出现过没有GCC的情况)  
  16.   
  17. 3.安装memcached  
  18.   
  19. tar zxvf memcached-1.4.2.tar.gz  
  20.   
  21.   cd memcached-1.4.2  
  22.   
  23.   ./configure --with-libevent=/usr  
  24.   
  25.   make  
  26.   
  27.   make install  
  28.   
  29. 如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。  
  30. 安装完成后会把memcached放到 /usr/local/bin/memcached ,  
  31.   
  32. 4.测试是否成功安装memcached:  
  33.   
  34. # ls -al /usr/local/bin/mem*  
  35.   
  36. 成功的话,会输出一些相关信息。  
  37.   
  38. 5.启动memcached  
  39.   
  40. /usr/local/bin/memcached -d -m 10 -u root -l localhost -p 11211 -c 256 -P /tmp/memcached.pid  
以上部分为从其他网站摘录的memcached安装以及启动的方式,我本人对该安装做了部分修改,仅给大家作为参考,如果安装不成功,请自行查询原因,该文章主要用于对集群的Session共享作为主题。

大家必须要确保两件事情已经完成:

第一:首先确认memcached是否已经启动,可以通过

<span style="font-size:14px;">netstat -lp | grep memcached</span>
来查看是否有11211的进程存在。

注意:11211为memcached的默认端口。

第二:需要在tomcat所在的节点上分别执行telnet命令来确认是否已经可以连接,操作命令为:

telnet 192.168.1.150 11211  

上述操作都完成后,就可以对Tomcat的相关文件进行配置了。

2. Tomcat相关配置

2)配置Tomcat的conf相关配置文件。

在Context.xml文件中引入下列代码:

  1. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"    
  2.                         memcachedNodes="192.168.2.2:11211"    
  3.                         requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"    
  4.                         sessionBackupAsync="false"    
  5.                         sessionBackupTimeout="100"    
  6.                         transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolutio.JavolutionTranscoderFactory"                           copyCollectionsForSerialization="false" />  
我在这卡了很久,找了好久才找到原因,原来是代码中多了几个空格,。。导致访问tomcat都见不到欢迎界面,然后慢慢排除才找到问题。其中也试过其他博主写的memcacahed的代码片段,还是空白页面,这边本人也不清楚是怎么回事,反正找了很久。。。

这里的memcachedNodes节点中的n1为节点的别名,可以随意起。后面的IP必须是memcached所在的主机的IP地址,端口必须是之前配置的11211端口。

该代码段在网上发现其他人说也可以放到Server.xml下面,这里本人没有尝试。

此时,所有的tomcat子节点中都要加上上面的这段代码,每个tomcat里面的内容都是相同的上述内容,无需做任何修改。


至此为止,有关memcache的Session共享已经完全配置完成,具体的启动顺序,首先要把memcached的服务启动,然后是apache服务,最后启动tomcat所在的每个子节点的服务。

最后访问 http://192.168.2.2:80/test刷新网页之后发现后面的tomcat1和tomcat2在变,而前面的session是不会变化的。本人了解过session,因为我们做的分布式,有两台tomcat,当一个用户登录tomcat1之后会产生一个session,如果没做session共享,在用户点击网页上面的连接可能就被分配到另外一个tomcat2上面,这样session就断了,需要用户重新登录。。。所以web服务器session共享是必须的。

好了忙活了三天,真实环境的apache+tomcat+session共享服务器集群成功了,其中学到了好多,原先重来没有接触过,特别套感谢这位博主,大部分都是按照他的两篇文章才弄出来的。
http://blog.csdn.net/fly2749/article/details/10304061

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值