环境准备:

服务器:4台
操作系统:全部都是 win2008R2 64bit
JAVA:JDK1.6.45 64bit
apache:Apache2.2
tomcat:tomcat7.0.61
mod_jk-1.2.31-httpd-2.0.52.so

搭建java tomcat和apche环境这里不在描述

编辑tomcat文件

注意:我有3个tomcat,jvmRoute分别叫做tomcat1、tomcat2、tomcat3

配置apache

  1. 把mod_jk-1.2.31-httpd-2.0.52.so放入到modules下
  2. 修改httpd.conf

    我的Apache安装在D:\wangchy\tas2.6\Apache2.2,找到conf目录下的httpd.conf,在文件的最后一行添加
            include "D:\wangchy\tas2.6\Apache2.2\conf\mod_jk.conf"
  3. 在conf目录下新建mod_jk.conf,内容如下

     LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
    	 JkWorkersFile conf/workers.properties
    	JkMount /* controller
  4. 在conf目录下新建workers.properties,内容如下

    #server
    worker.list = controller
    
    #========tomcat1========
    worker.tomcat1.port=8009
    worker.tomcat1.host=172.16.176.181
    worker.tomcat1.type=ajp13
    worker.tomcat1.lbfactor = 1
    
    #========tomcat2========
    worker.tomcat2.port=8009
    worker.tomcat2.host=172.16.2.39
    worker.tomcat2.type=ajp13
    worker.tomcat2.lbfactor = 1
    
    #========tomcat3========
    worker.tomcat3.port=8009
    worker.tomcat3.host=172.16.2.29
    worker.tomcat3.type=ajp13
    worker.tomcat3.lbfactor = 1
    #========controller,负载均衡控制器========
    
    worker.controller.type=lb
    worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3
    worker.controller.sticky_session=false
    worker.controller.sticky_session_force=1
    #worker.controller.sticky_session=1 

测试

建立一个jsp,放在tomcat的webapps/lb/目录下

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

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

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

<body>

Server Info:

<%

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

<%

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

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

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

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

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

     session.setAttribute(dataName, dataValue);

  }

  out.println("<b>Session 列表</b><br>");

  System.out.println("============================");

  Enumeration e = session.getAttributeNames();

  while (e.hasMoreElements()) {

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

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

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

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

   }

%>

  <form action="test.jsp" method="POST">

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

     <br>

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

     <br>

    <input type=submit>

   </form>

</body>

</html>

依次启动apache和3个tomcat,然后访问http://172.16.7.230/lb/test.jsp,多刷新几次就可以看到如下效果
http://172.16.7.230/lb/test.jsp

|

软件下载地址:http://down.51cto.com/4363165

发表评论

电子邮件地址不会被公开。 必填项已用*标注