`
星夜的遐想
  • 浏览: 181384 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Apache和Tomcat集群测试案例

阅读更多

上一篇也简单介绍了如何配置集群环境,下面来测试结果:

 

1、首先创建一个项目web项目TestCluster,因为测试比较简单,直接用jsp实现。

2、修改项目的web.xml文件,添加 <distributable/>节点

写道
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestCluster</display-name>
<distributable/>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

 

 

 3、创建index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
	<% String sessionStr=session.getId();%>
	当前的sessionId:<B><%=sessionStr %></B> <br>

  <%
  	out.println("<b>Session 列表</b><br>");
	
  	Enumeration e = session.getAttributeNames();

  	while (e.hasMoreElements()) {

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

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

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

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

       }
  %>
	<hr/>
	<form action="process.jsp" id="jspForm">
		
		键:<input type="text" name="str1" />
		值:<input type="text" name="str2" />
		<input type="submit" value="提交">
	</form>
</body>
</html>

 

4、创建处理页面process.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	
	 	try {
			//设置请求延时10秒钟,以便停止对应的tomcat,模拟服务器崩溃
			Thread.sleep(10*1000);
			
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				
			}
	   String str1=request.getParameter("str1");
	   String str2=request.getParameter("str2");
	   //获得sessionId(427C1BDFD6C3E5A171F2431F8E0004A4.tomcat6_2 形式的字符串)
	   String sessionStr=session.getId();
	   //获得tomcat名称
	   String serverName=sessionStr.substring(sessionStr.lastIndexOf(".")+1);
	   System.out.println("<<======================"+serverName+"被调用了==================>>");
	   
	  
	   session.setAttribute(str1,str2);
	   
	   request.getRequestDispatcher("/index.jsp").forward(request, response);
	%>
</body>
</html>

  

5、测试场景

输入:http://localhost/TestCluster/test.jsp (因为Apache默认端口为80)

 

(1) 第一次页面显示如下:

(2) 点击提交的处理请求的过程中,将tomcat6_1停掉后,响应页面如下:

 

 (3)有图一和图二的sesssionId完全一致,已经实现了session共享,当接收请求应用服务器突然中断后,请求依然会转发给其他的服务器,继续处理,完全实现了我们的要求。

 

(4) 有需要可下载案例源码  ^_^

  • 大小: 11.4 KB
  • 大小: 12.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics