前言* 随着IT行业的发展,linux服务器在企业中应用广泛,人们对linux上的应用服务要求也越来越高,上篇搭建了nginx群集,今天我们来搭建目前用得最多的apache群集,接下来我们亲自来搭建一个属于自己的高性能web服务器,负载均衡服务器。一、系统环境
系统版本:Centos 5.3 64位系统 Nginx版本为:httpd-2.2.13.tar.bz2 Jdk版本为:1.6.0_18_64 Tomcat版本为:Apache Tomcat/6.0.30 其他相似版本也可以!安装服务所需的包如下下载路径:本网站集成部分下载: jdk请到以下网站下载:请选择自己系统版本的jdk。 https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SingleVariationTypeFilter【Apache、Tomcat下载地址,下载到/usr/src下】 cd /usr/src && wget http://blog.mgcrazy.com/download/{httpd-2.2.13.tar.bz2,apache-tomcat-6.0.30.tar.gz }
二、安装Apache
cd /usr/src && tar -jxvf httpd-2.2.13.tar.bz2 && cd httpd-2.2.13/srclib/apr &&./configure --prefix=/usr/local/apr &&make &&make install &&cd ../apr-util/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &&make&&make install && cd ../../ && ./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --with-mpm=worker --enable-deflate --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-proxy --enable-proxy-ajp --enable-proxy-balancer --enable-proxy-http --enable-rewrite --enable-charset-lite --disable-ipv6 --disable-dav --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
可以直接拷贝安装apache
【注意*这里安装mod_proxy处理Tomcat jsp页面模块,安装apr、apr-util增加Tomcat处理性能,当然也可以用mod_jk的方式,如果是mod_jk的方式的话,可以参考这篇文章:
】
三、Tomcat安装配置:
1、将所需的包下载至/usr/src 目录;首先安装jdk:
chmod o+x jdk* && ./jdk* ;程序包会提示按回车键,我们根据提示安装即可,解压完毕,会在当前目录生成 jdk1.6.0_18 这个文件夹
mkdir -p /usr/java && mv jdk1.6.0_18 /usr/java/下 vi /etc/profile 最后面加入以下语句: export JAVA_HOME=/usr/java/jdk1.6.0_18 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin source /etc/profile //使环境变量马上生效 java –version //查看java版本 cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz 解压完成执行: mv apache-tomcat-6.0.30 /usr/local/tomcat_1 && cp /usr/local/tomcat_1 /usr/local/tomcat_2 -r
【移动到/usr/local下并重命名为tomcat_1文件夹,并cp一个tomcat_2】部署两个Tomcat群集做负载均衡用!
2、设置tomcat的java变量
cd /usr/local/tomcat_1/bin/ && vi setenv.sh 创建一个setenv.sh脚本文件,添加如下语句即可 JAVA_HOME=/usr/java/jdk1.6.0_18 JAVA_JRE=/usr/java/jdk1.6.0_18/jre 并且拷贝setenv.sh文件到tomcat_2/bin/下: cp -p /usr/local/tomcat_1/bin/setenv.sh /usr/local/tomcat_2/bin 下即可! 保存退出 检查两个tomcat下bin目录的*.sh 文件是否有可执行权限,如果没有则进入到相应的目录执行 chmod o+x *.sh 即可! 给两个tomcat分别创建不同的目录如下:mkdir -p /usr/webapps/{www_1,www_2}
3、设置Tomcat的 server.xml文件
两个Tomcat配置的cluster里面的端口分别为4000、4001 ,必须设置成不同的端口;而且得分别设置两个jvmroute名称,一会在apache配置文件中会用到!
四、Tomcat session会话复制
分别在两个tomcat的conf下web.xml文件里面加入如下一行即可: 修改web应用里面WEB-INF目录下的web.xml文件,加入标签,也可以加在应用的web.xml里面如下:直接加在之前就可以了 这个是加入tomcat的session复制的,做tomcat集群必须需要这一步,否则用户的session就无法正常使用 配置完毕后,我们测试一下广播 java -cp tomcat-replication.jar MCaster 224.0.0.1 45564 Terminal1 java -cp tomcat-replication.jar MCaster 224.0.0.1 45564 Terminal2
如果不报错则能正常广播tomcat-replication.jar 下载:
Tomcat配置完毕,启动两个tomcat,先启动tomcat1,tomcat_1启动日志如下:
信息: Initializing Coyote HTTP/1.1 on http-8080 2011-3-18 19:56:21 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1088 ms 2011-3-18 19:56:21 org.apache.catalina.core.StandardService start 信息: Starting service Catalina 2011-3-18 19:56:21 org.apache.catalina.core.StandardEngine start 信息: Starting Servlet Engine: Apache Tomcat/6.0.18 2011-3-18 19:56:21 org.apache.catalina.ha.tcp.SimpleTcpCluster start 信息: Cluster is about to start 2011-3-18 19:56:21 org.apache.catalina.tribes.transport.ReceiverBase bind 信息: Receiver Server Socket bound to:/192.168.2.79:4000 2011-3-18 19:56:21 org.apache.catalina.tribes.membership.McastServiceImpl setupSocket 信息: Setting cluster mcast soTimeout to 500 2011-3-18 19:56:21 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers 信息: Sleeping for 1000 milliseconds to establish cluster membership, start level:4 2011-3-18 19:56:22 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers 信息: Done sleeping, membership established, start level:4 2011-3-18 19:56:22 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers 信息: Sleeping for 1000 milliseconds to establish cluster membership, start level:8 2011-3-18 19:56:23 org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers 信息: Done sleeping, membership established, start level:8 2011-3-18 19:56:23 org.apache.catalina.ha.deploy.FarmWarDeployer start 严重: FarmWarDeployer can only work as host cluster subelement! 2011-3-18 19:56:23 org.apache.catalina.ha.session.DeltaManager start 信息: Register manager to cluster element Engine with name Catalina 2011-3-18 19:56:23 org.apache.catalina.ha.session.DeltaManager start 信息: Starting clustering manager at 2011-3-18 19:56:23 org.apache.catalina.ha.session.DeltaManager getAllClusterSessions 信息: Manager [localhost#]: skipping state transfer. No members active in cluster group. 2011-3-18 19:56:23 org.apache.catalina.ha.session.JvmRouteBinderValve start 信息: JvmRouteBinderValve started 2011-3-18 19:56:23 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8080 2011-3-18 19:56:24 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8411 2011-3-18 19:56:24 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/42 config=null 2011-3-18 19:56:24 org.apache.catalina.startup.Catalina start 信息: Server startup in 2676 ms
Tomcat_2的日志如下:
000,{-64, -88, 2, 79},4000, alive=86901,id={78 42 25 -19 -102 3 64 10 -76 93 69 -43 45 76 95 -112 }, payload={}, command={}, domain={}, ]. This operation will timeout if no session state has been received within 60 seconds. 2011-3-18 19:57:48 org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor report 信息: ThroughputInterceptor Report[ Tx Msg:1 messages Sent:0.00 MB (total) Sent:0.00 MB (application) Time:0.01 seconds Tx Speed:0.04 MB/sec (total) TxSpeed:0.04 MB/sec (application) Error Msg:0 Rx Msg:1 messages Rx Speed:0.00 MB/sec (since 1st msg) Received:0.00 MB] 2011-3-18 19:57:48 org.apache.catalina.ha.session.DeltaManager waitForSendAllSessions 信息: Manager [localhost#]; session state send at 11-3-18 下午7:57 received in 125 ms. 2011-3-18 19:57:48 org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor report 信息: ThroughputInterceptor Report[ Tx Msg:2 messages Sent:0.00 MB (total) Sent:0.00 MB (application) Time:0.05 seconds Tx Speed:0.02 MB/sec (total) TxSpeed:0.02 MB/sec (application) Error Msg:0 Rx Msg:2 messages Rx Speed:0.00 MB/sec (since 1st msg) Received:0.00 MB] 2011-3-18 19:57:49 org.apache.catalina.ha.session.JvmRouteBinderValve start 信息: JvmRouteBinderValve started 2011-3-18 19:57:49 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8081 2011-3-18 19:57:49 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8412 2011-3-18 19:57:49 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/34 config=null 2011-3-18 19:57:49 org.apache.catalina.startup.Catalina start 信息: Server startup in 2962 ms
如果有报错或者请查看原因!skipping state transfer. No members active in cluster group.”mcastBindAddress 没有添加或添加得不对
如上报错,如果启动其中一个报的错,而且已经修改了mcastBindAddress,此时还是有报错,我们可以不用管,在启动另一个 tomcat看看,第二个tomcat没有No members active in cluster group,则表示两个tomcat通信正常。
五、配置Apache+Session
vi vhosts.conf proxy.conf 文件,并在httpd.conf文件末行添加:
Include conf/vhosts.conf Include conf/proxy.conf
如上两行即可!
把以下内容写入到vhosts.conf里面:
ServerAdmin wgkgood@163.com DocumentRoot "/usr/webapps/www” ServerName 192.168.2.79 AllowOverride None Options None Order allow,deny Allow from all RewriteEngine On ProxyPreserveHost On RewriteRule ^$ balancer://wwwCluster/ [P,L,NC] RewriteRule ^/(.*)$ balancer://wwwCluster/$1 [P,L,NC] ProxyPass / balancer://wwwCluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPa***everse / balancer://wwwCluster/ BalancerMember ajp://localhost:8411 loadfactor=1 route=tomcat1 smax=5 max=20 ttl=120 retry=300 timeout=15 BalancerMember ajp://localhost:8412 loadfactor=1 route=tomcat2 smax=5 max=20 ttl=120 retry=300 timeout=15
注意:* /usr/webapps/www 内容其实跟/usr/webapps/www_1是一样的;最后红色部分是session复制部分。绿色字体部分是处理tomcat请求。这段代码必须放在虚拟主机里面,放在proxy.conf里面的话,session绑定不会生效。所以要注释掉httpd.conf里面Include conf/proxy.conf这个配置
六、配置Session共享Memcache
下载如下memcache套件包:本站点集成下载:
http://blog.mgcrazy.com/download/session-memcache/javolution-5.4.3.1.jar http://blog.mgcrazy.com/download/session-memcache/memcached-2.5.jar http://blog.mgcrazy.com/download/session-memcache/memcached-session-manager-1.3.0.jar http://blog.mgcrazy.com/download/session-memcache/msm-javolution-serializer-1.3.0.jar http://blog.mgcrazy.com/download/session-memcache/msm-javolution-serializer-cglib-1.3.0.jar http://blog.mgcrazy.com/download/session-memcache/msm-javolution-serializer-jodatime-1.3.0.jar 以上包请到新地址下载: http://chinaapp.sinaapp.com//download/memcached-session/memcached-session-manager.tar.gz 解压即可!
下载后,分别放在两个tomcat的lib下:【/usr/local/tomcat_1/lib下即可】
然后分别两个tomcat配置文件:server.xml最后</host>前一行里面添加如下代码:
如果有多个memcache缓存服务器,可以在上面192.168.0.84:11211后已空格分开!例如:
n1:192.168.0.84:11211 n2:192.168.0.84:11213 即可!
tomcat_1的发布目录index.jsp内容为:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% %> http://blog.mgcrazy.com <%out.print(request.getSession()) ;%> <%out.println(request.getHeader("Cookie")); %>
tomcat_2的发布目录index.jsp内容为:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% %> http://wgkgood.gicp.net <%out.print(request.getSession()) ;%> <%out.println(request.getHeader("Cookie")); %> 所以配置完毕,重启tomcat和apache即可!
最后测试!在同一个ie浏览器窗口下 ctrl+f5刷新,都是同一个tomcat内容,它不会跳转到另一tomcat
http://192.168.2.79 结果如下:
http://wgkgood.gicp.net org.apache.catalina.session.StandardSessionFacade@2d7aece8 JSESSIONID=FEB19E24719ED666D1C9C50A7A09A2BA.tomcat2 另一个firefox浏览器窗口为: http://blog.mgcrazy.com org.apache.catalina.session.StandardSessionFacade@259e215b rtime=0; ltime=1298014951125; cnzz_eid=85744900-1298010049-; Hm_lvt_0e74098a87be059a433e5a74f1b5fecf=1298014951312; Hm_lvt_b7c035fd79b2c378053e06c86493de76=1300439158373; JSESSIONID=B2574ABB491F50F37162479BD3FE761C.tomcat1
无论你怎么刷新同一个窗口下都不会变!
最后,本环境是在一台机器,也可以是多台机器,只需要修改负载的ip即可。
apache+tomcat+session不能实现故障转移,可以采用nginx+tomcat+session实现故障转移,或者LVS+后端apache均衡;另外去掉session绑定的话,apache+tomcat负载均衡、并且可以带故障转移。
本文参考: