【APP & Web架构】企业web高可用集群实战之apache整合tomcat实现tomcat负载均衡集群+session

一、环境:

系统最小化安装,系统初使化脚本:

http://linuxops.blog.51cto.com/2238445/841849

系统环境:centos 5.8 x86_64
应用软件:所以应用软件都放在/usr/local/src目录下
 httpd-2.2.22.tar.gz
         apache-tomcat-7.0.26.tar.gz
 jdk-7u4-linux-x64.gz
 tomcat-connectors-1.2.37-src.tar.gz
所需服务器:
         server1:192.168.8.222   -->安装httpd、apache-tomcat、jdk、tomcat-connectors
 server2:192.168.8.223   -->安装apache-tomcat、jdk
二、安装apache
 
 
  1. cd /usr/local/src/

  2. tar zxf httpd-2.2.22.tar.gz

  3. cd httpd-2.2.22

  4. ./configure --prefix=/usr/local/apache2 --enable-modules=so --enable-so --enable-cache --enable-mem-cache --enable-rewrite --enable-ssl --with-mpm=prefork

  5. make;make install

  6. cd ..

三、安装tomcat、jdk
 
 
  1. 1.安装tomcat

  2. tar zxf apache-tomcat-7.0.26.tar.gz

  3. mv apache-tomcat-7.0.26 /usr/local/tomcat

  4. 2.安装jdk

  5. tar zxf jdk-7u4-linux-x64.gz

  6. mv jdk1.7.0_04 /usr/local/jdk

  7. 3.设置java、tomcat运行环境变量

  8. vi /etc/profile

  9. 在文件最后加入如下内容:

  10. #Set java & tomcat Environment

  11. JAVA_HOME=/usr/local/jdk

  12. export JAVA_HOME

  13. JRE_HOME=/usr/local/jdk/jre

  14. export JRE_HOME

  15. CLASSPATH=/usr=/usr/local/tomcat/common/lib/:/usr/local/jdk/lib:/usr/local/jdk/jre/lib

  16. export CLASSPATH

  17. PATH=$PATH:/usr/local/tomcat/bin/:/usr/local/apache/bin:/usr/local/jdk/bin:/usr/local/jdk/jre/bin

  18. export PATH

  19. TOMCAT_HOME=/usr/local/tomcat

  20. export TOMCAT_HOME

  21. 让环境变量立即生效:

  22. source /etc/profile


四、安装jk,整合apache和tomcat
 
 
  1. 1.安装jk

  2. tar zxf tomcat-connectors-1.2.37-src.tar.gz

  3. cd tomcat-connectors-1.2.37-src/native

  4. ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-java-home=/usr/local/jdk

  5. make;make install

  6. 检测模块有没有生成

  7. 通过上面几步,会在/usr/local/apache2/modules/目录下产生mod_jk.so 这个模块文件

  8. 2.整合apache和tomcat

  9. 1.)修改apache配置文件/usr/local/apache2/conf/httpd.conf ,加入:Include conf/mod_jk.conf

  10. echo "Include conf/mod_jk.conf" >> /usr/local/apache2/conf/httpd.conf

  11. 2.)创建mod_jk.conf配置文件

  12. vi /usr/local/apache2/conf/mod_jk.conf

  13. 加入如下内容:

  14. # Load mod_jk module

  15. LoadModule jk_module modules/mod_jk.so

  16. # Where to find workers.properties

  17. JkWorkersFile conf/workers.properties

  18. # Where to put jk logs

  19. JkLogFile logs/mod_jk.log

  20. JkShmFile logs/mod_jk.shm

  21. # Set the jk log level [debug/error/info]

  22. JkLogLevel info

  23. # Select the log format

  24. JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

  25. # JkOptions indicate to send SSL KEY SIZE,

  26. JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

  27. # JkRequestLogFormat set the request format

  28. JkRequestLogFormat "%w %V %T"

  29. # Send JSPs for context /examples to worker named ajp13

  30. JkMount /* controller

  31. 3.配置JK调度器

  32. 创建JK配置文件

  33. cat /usr/local/apache2/conf/workers.properties

  34. #

  35. # workers.properties conf

  36. #

  37. # list the workers by name

  38. worker.list=controller      #定义负载均衡名称

  39. worker.controller.type=lb   #定义负载均衡类型,这里为lb

  40. worker.controller.error_escalation_time=0   #切换错误状态时间

  41. worker.controller.max_reply_timeouts=10

  42. # -------- server 1  ---------

  43. worker.jvm1.reference=worker.template     #jvm1引用worker.template模板

  44. worker.jvm1.port=8009       #jvm1的ajp端口

  45. worker.jvm1.host=192.168.8.222  #jvm1的的IP

  46. worker.jvm1.lbfactor = 5  

  47. # -------- server 2  ---------

  48. worker.jvm2.reference=worker.template

  49. worker.jvm2.port=8009

  50. worker.jvm2.host=192.168.8.223

  51. worker.jvm2.lbfactor = 1

  52. worker.jvm2.activation=A

  53. worker.template.type=ajp13                    

  54. worker.template.socket_connect_timeout=5000 #套接字连接超时为5000毫秒,默认为0

  55. worker.template.socket_keepalive=true       #开启保持为真,默认为false

  56. worker.template.ping_mode=A         #使用cping/cpong模式,有C,P,I,A四个模式,A表示选择所以模式

  57. worker.template.ping_timeout=10000      #探测cping/cpong超时间默认10000毫秒

  58. worker.template.connection_pool_minsize=0   #连接池关闭最小时间

  59. worker.template.connection_pool_timeout=600 #连接池600秒

  60. worker.template.reply_timeout=300000        #答复超时300000毫秒

  61. worker.template.recovery_options=3      #重试次数

  62. worker.controller.balance_workers=jvm1,jvm2 #负载均衡的客户名称

  63. worker.controller.sticky_session=0

  64. worker.controller.sticky_session_force=0

  65. #worker.balancer.sticky_session=1

PS: 这里worker.jvm1.port=8009的端口设置为tomcat server.xml配置文件中ajp13的默认端口!也可以自己定义,两边必须相同!
worker.controller.sticky_session=0
worker.controller.sticky_session_force=0
这是实现session复制的关键!
把worker.balancer.sticky_session=1 设置为1貌似不成,索性用上面的方法OK!
五、tomcat集群和session复制配置
 
 
  1. 1.server1上配置

  2. 1.)在/usr/local/tomcat/conf/server.xml 中修改Enginx节点信息,大概在103行:

  3. 把<Engine name="Catalina" defaultHost="localhost">

  4. 修改成:

  5. <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

  6. 2.)加入Cluster节点信息,大概在110行左右处加入如下内容:

  7. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">

  8. <Manager className="org.apache.catalina.ha.session.DeltaManager"

  9.           expireSessionsOnShutdown="false"

  10.           notifyListenersOnReplication="true"/>  

  11.  <Channel className="org.apache.catalina.tribes.group.GroupChannel">  

  12.    <Membership className="org.apache.catalina.tribes.membership.McastService"

  13.                address="228.0.0.4"

  14.                port="45564"

  15.                frequency="500"

  16.                dropTime="3000"/>  

  17.    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

  18.              address="192.168.8.222"

  19.              port="4000"

  20.              autoBind="100"

  21.              selectorTimeout="5000"

  22.              maxThreads="6"/>  

  23.    <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  

  24.      <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  

  25.    </Sender>  

  26.    <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  

  27.    <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  

  28.  </Channel>  

  29.  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

  30.         filter=""/>  

  31.  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  

  32.  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"

  33.            tempDir="/tmp/war-temp/"

  34.            deployDir="/tmp/war-deploy/"

  35.            watchDir="/tmp/war-listen/"

  36.            watchEnabled="false"/>  

  37.  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  

  38.  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  

  39. </Cluster>

2.server2上配置
 
 
  1. 1.)在/usr/local/tomcat/conf/server.xml 中修改Enginx节点信息,大概在103行:

  2. 把<Engine name="Catalina" defaultHost="localhost">

  3. 修改成:

  4. <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">

  5. 2.)加入Cluster节点信息,大概在110行左右处加入如下内容:

  6. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">

  7. <Manager className="org.apache.catalina.ha.session.DeltaManager"

  8.           expireSessionsOnShutdown="false"

  9.           notifyListenersOnReplication="true"/>  

  10.  <Channel className="org.apache.catalina.tribes.group.GroupChannel">  

  11.    <Membership className="org.apache.catalina.tribes.membership.McastService"

  12.                address="228.0.0.4"

  13.                port="45564"

  14.                frequency="500"

  15.                dropTime="3000"/>  

  16.    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

  17.              address="192.168.8.223"

  18.              port="4000"

  19.              autoBind="100"

  20.              selectorTimeout="5000"

  21.              maxThreads="6"/>  

  22.    <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  

  23.      <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  

  24.    </Sender>  

  25.    <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  

  26.    <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  

  27.  </Channel>  

  28.  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

  29.         filter=""/>  

  30.  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  

  31.  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"

  32.            tempDir="/tmp/war-temp/"

  33.            deployDir="/tmp/war-deploy/"

  34.            watchDir="/tmp/war-listen/"

  35.            watchEnabled="false"/>  

  36.  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  

  37.  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  

  38. </Cluster>

  39. 3.修改web站点文件存放主目录路径

  40. 1.)apache配置文件要修改的地方

  41. 把下面两处修改成:

  42. DocumentRoot "/data/www/"    #大概在104行

  43. <Directory "/data/www">      #大概在131行

  44. 2.)tomcat配置文件修改成:

  45.   <Host name="localhost"  appBase="/data/www"

  46.            unpackWARs="true" autoDeploy="true">

  47. <Context path="/" docBase="" debug="0" reloadable="true" />


4.建立测试目录
 
 
  1. mkdir -p /data/www/

  2. cat /data/www/test.jsp

  3. <%

  4. System.out.println("Welcome tomcat_cluster session repication");

  5. %>

  6. 在/data/www目录下创建admin目录,再在admin目录下创建WEB-INF目录

  7. mkdir -p /data/www/admin/WEB-INF

  8. cd /data/www/admin/WEB-INF

  9. vi web.xml

  10. 加入如下内容:

  11. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"

  12. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  13. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.co

  14. m/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

  15. <display-name>TomcatDemo</display-name>

  16. <distributable/>

  17. </web-app>


PS: <distributable/>这个必须加进去,不然也实现不了复制!
在admin目录下创建测试代码:
 
 
  1. cat /data/www/admin/index.jsp

  2. <%@ page contentType="text/html; charset=GBK" %>

  3. <%@ page import="java.util.*" %>

  4. <html><head><title>Cluster App Test</title></head>

  5. <body>

  6. Server Info:

  7. <%

  8. out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>

  9. <%

  10. out.println("<br> ID " + session.getId()+"<br>");

  11. // 如果有新的 Session 属性设置

  12. String dataName = request.getParameter("dataName");

  13. if (dataName != null && dataName.length() > 0) {

  14. String dataValue = request.getParameter("dataValue");

  15. session.setAttribute(dataName, dataValue);

  16. }

  17. out.print("<b>Session 列表</b>");

  18. Enumeration e = session.getAttributeNames();

  19. while (e.hasMoreElements()) {

  20. String name = (String)e.nextElement();

  21. String value = session.getAttribute(name).toString();

  22. out.println( name + " = " + value+"<br>");

  23. System.out.println( name + " = " + value);

  24. }

  25. %>

  26. <form action="index.jsp" method="POST">

  27. 名称:<input type=text size=20 name="dataName">

  28. <br>

  29. 值:<input type=text size=20 name="dataValue">

  30. <br>

  31. <input type=submit>

  32. </form>

  33. </body>

  34. </html>


PS: 以上代码来源于网上!
浏览器测试结果:
http://192.168.8.222/admin/

162155429.jpg

162210180.jpg
再回到服务器上查看日志:
223服务器:
161827384.jpg
222服务器:

161902925.jpg

1.这里ID必须相同,且jvm1和jvm2来回切换!
2.ID不同,jvm1和jvm2来回切换只是实现了负载均衡!
后面的文章介绍: apache+tomcat集群用memcached保持session
               apache+tomcat集群用apache的 proxy-ajp和proxy-balance模块保持session
后面陆续介绍nginx+tomcat集群!欢迎大家来逛逛!!哈哈。。。。


你可能感兴趣的:(apache,session,tomcat集群,ajp)