apache tomcat 集群

步骤:
1.安装apache,下载mod_jk.jar放到apache目录的modules目录下
2.配置apache,conf目录下的httpd.conf文件加上include conf/mod_jk.conf
3.在conf目录下创建mod_jd.conf文件
  内容:LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.0.52.so(jk的目录)
JkWorkersFile conf/workers.properties (jk配置文件)
JkMount /*.jsp controller
4.在conf目录下创建workers.properties内容为下:
worker.list = controller,tomcat1,tomcat2  #server 列表   
#========tomcat1========   
worker.tomcat1.port=8109       #ajp13 端口号,在tomcat下server.xml配置,默认8009   
worker.tomcat1.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址   
worker.tomcat1.type=ajp13   
worker.tomcat1.lbfactor = 1    #server的加权比重,值越高,分得的请求越多   
  
#========tomcat2========   
worker.tomcat2.port=8209       #ajp13 端口号,在tomcat下server.xml配置,默认8009   
worker.tomcat2.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址   
worker.tomcat2.type=ajp13   
worker.tomcat2.lbfactor = 7    #server的加权比重,值越高,分得的请求越多   
  
#========controller,负载均衡控制器========   
worker.controller.type=lb   
worker.controller.balanced_workers=tomcat2,tomcat1   #指定分担请求的tomcat   
worker.controller.sticky_session=0
5.如果tomcat都在同一台电脑上,重新配置一下tomcat的serverx.xml
<Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443"/>(port端口号)
  <Connector port="8109" protocol="AJP/1.3" redirectPort="8443"/>(port端口号)
<Engine defaultHost="localhost" name="Catalina" jvmRoute="tomcat1">(jvmRoute的值)
6.最后创建Test项目经典index.jsp测试,把这个项目部署到两个tomcat中,然后访问localhost/test/index.jsp,就可以看到结果了:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </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="index.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 tomcat集群)