轻松实现Apache,Tomcat集群和负载均衡作者:罗代均 ldj_work#126.com,转载请保持完整性0, 环境说明?????? A
轻松实现Apache,Tomcat集群和负载均衡
作者:罗代均 ldj_work#126.com,转载请保持完整性
0, 环境说明
?????? Apache? :apache_2.0.55???? 1 个
???????Tomcat:? apache-tomcat-5.5.17 (zip版) 2个
???????mod_jk:: mod_jk-apache-2.0.55.so? 1个
第一部分:负载均衡
??? 负载均衡,就是apache将客户请求均衡的分给tomcat1,tomcat2....去处理
?? 1.安装apche,tomcat
?? http://httpd.apache.org/?下载Apache 2.0.55
??? http://tomcat.apache.org/download-55.cgi?下载tomcat5.5 zip版本(解压即可,绿色版)
?? http://apache.justdn.org/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.15/? 下载mod_jk,注意和? apache版本匹配
?? 按照jdk,我的路径为:E:\ide\apache\Apache2
?? 解压两份Tomcat, 路径分别为 E:\ide\tomcat1,E:\ide\tomcat2
<%@ 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.print("<b>Session 列表</b>");? 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>然后在test 新建WEB-INF目录,WEB-INF下新建web.xml,内容如下<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
?????? <display-name>TomcatDemo</display-name>
??????
<distributable/></web-app>注意:在你的应用的web.xml加入?
<distributable/>即可ok,讲test复制到tomcat1,tomcat2的webapps下,重启apache,tomcat1,tomcat2,输入网址 http://localhost/test/test.jsp?? 新建一个 名称为 xiaoluo? ,值为 cdut 的session,提交查询,新开一个ie窗口,再提交查询,如图,可以看到,两个tomcat 是负载均衡,并且session同步的