日期:2014-05-17 浏览次数:20903 次
Apache,Tomcat负载均衡和集群
软件下载:
apache:httpd-2.2.19-win32-x86-no_ssl.msi
http://httpd.apache.org/download.cgi#apache22
?
tomcat:apache-tomcat-6.0.24
http://tomcat.apache.org/
?
jk:mod_jk-1.2.31-httpd-2.2.3.so
http://tomcat.apache.org/download-connectors.cgi
?
负载均衡
1.解压两份Tomcat, 路径分别为 apache-tomcat-1,apache-tomcat-2
?
2.在apache安装目录下conf目录中配置http.conf
在文件最后加上下面一句
include "D:\Program Files\Apache Software Foundation\Apache2.2\conf\mod_jk.conf"
?
3.在apache安装目录下conf目录中创建mod_jk.conf文件,内容如下:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
?
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
?
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器
JkMount /*.jsp controller
?
4.在apache安装目录下conf目录中创建workers.properties文件,内容如下:
worker.list = controller,tomcat1,tomcat2?
?
#server 列表#========tomcat1========
worker.tomcat1.port=8009???????? #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost? #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1?? #server的加权比重,值越高,分得的请求越多
?
#========tomcat2========
worker.tomcat2.port=9009?????? #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=localhost?
#tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1?? #server的加权比重,值越高,分得的请求越多
?
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2?? #指定分担请求的tomcat
worker.controller.sticky_session=1
?
5.修改tomcat配置文件server.xml
打开tomcat2/conf/server.xml文件
<Server port="9005" shutdown="SHUTDOWN">? 改为9005
<Connector port="9080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />???? 改为9080
?<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />改为9009
?
6.编写test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.lang.String"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println("=basePath is:="+basePath+"=end=");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>???
??? <title>test</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">
?<script language="javascript">
?</script>
? </head>?
? <body>
? <%
?? System.out.println("===========================");
? %>
? </body>
</html>
?
7.在apache-tomcat-1\webapps目录下创建test目录并将test.jsp放入
在apache-tomcat-2\webapps目录下创建test目录并将test.jsp放入
?
8.将mod_jk-1.2.31-httpd-2.2.3.so复制到
D:\Program Files\Apache Software Fo