apache2.2+tomcat7.0.37+tomcat7.0.26负载均衡

本人亲试!!OK


1.安装apache2.2
创建#
# workers.properties
#


# list the workers by name

worker.list=controller, status

# localhost server 1
# ------------------------
worker.jvm1.port=8009
worker.jvm1.host=localhost
worker.jvm1.type=ajp13
worker.jvm1.lbfactor = 1

# localhost server 2
# ------------------------
worker.jvm2.port=9009
worker.jvm2.host=localhost
worker.jvm2.type=ajp13
worker.jvm2.lbfactor = 1


worker.controller.type=lb
worker.retries=3
worker.controller.balanced_workers=jvm1, jvm2
worker.controller.sticky_session=1

worker.status.type=status



2.创建rulmap

/*=DLOG4J
#所有请求都由controller这个server处理
#/jkstatus=status
#所有包含jkstatus请求的都由status这个server处理

!/*.gif=DLOG4J
#所有以.gif结尾的请求都不由controller这个server处理,以下几个都是一样的意思

!/*.png=DLOG4J
!/*.css=DLOG4J
!/*.js=DLOG4J
!/*.htm=DLOG4J
!/*.html=DLOG4J


3.JkWorkersFile conf/workers.properties
JkMountFile conf/uriworkermap.properties
JkLogFile logs/mod_jk.log
JkLogLevel warn


4 VirtualHost 注意controller必须和 worklist  controller一样
<VirtualHost *:81>
ServerName 10.0.9.85
UseCanonicalName off
DocumentRoot "D:\demo"
DirectoryIndex index.html home.cn
JkMount /sun.shop/blog/* controller
    #例子2,向Apache请求此文件夹内页面时,系统将转向用Tomcat解析
  JkMount /*.jsp controller
   #例子3,向 Apache 请求jsp页面时,用 Tomcat 进行解析
  JkMount /*.do controller
  #例子4,向 Apache 请求.do 动作时, 用 Tomcat 进行解析
  JkMount /*.action controller
   #例子5,向 Apache 请求 .action 动作时, 用 Tomcat 进行解析
</VirtualHost>

5.tomcat 3个重要修改

  a<Connector port="9009" protocol="AJP/1.3" redirectPort="8443"/>


  
   b <Engine defaultHost="localhost" name="Catalina" jvmRoute="jvm2">

    
 
      c<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

6.重启apache && tomcat

7.给web.xml 添加  <distributable/> 便于session复制



8.OK.jsp


<%@ 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="ok.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)