1、创建一个登陆的web项目做测试,名为session
环境 windows + JDK1.6 +两个Tomcat6 + MSM 1.8 + memcached-win32-1.4.4-14
1.1 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录页面</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"> </head> <body> <table bgcolor="#F8C3C3" border="1" borderColor="#FF0000" align="center" width="260" height="160"> <tr> <td height="15" colspan="2" align="center" style="background-color: #000000; font-size: 28px; color: #FFFF00"><b>用户登录</b></td> </tr> <form method="post" action="LoginSevlet"> <tr> <td width="80" align="center"><b>用户名</b></td> <td><input type="text" name="userName" value="" /></td> </tr> <tr> <td align="center"><b>密 码</b></td> <td><input type="password" name="password" value="" /></td> </tr> <tr> <td colspan="2" align="center" style="background-color: #000000;"> <input type="submit" value="登 录" /> <input type="reset" value="重 置" /> </td> </tr> </form> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录成功页面</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"> </head> <% String userName = (String) request.getSession().getAttribute("userName"); %> <body bgcolor="#000000"> <table width="500" height="200" align="center" border="1" style="background-color: #000000;"> <tr> <td align="center" style="background-color: #F8C3C3; color: #00FF00; font-weight: bold; font-size: 30px"> 当前登录用户:<%=userName %> </td> </tr> </table> </body> </html>
package org.shirdrn.cluster.tomcat.servlet; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginSevlet extends HttpServlet { private static final long serialVersionUID = 8278367768726269037L; private static Map<String, String> accountDB = new HashMap<String, String>(); static { accountDB.put("shirdrn", "123456"); accountDB.put("admin", "999999"); accountDB.put("dev", "000888"); accountDB.put("007", "007007"); } public LoginSevlet() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter("userName"); String password = request.getParameter("password"); if(userName!=null && password!=null && accountDB.containsKey(userName) && accountDB.get(userName).equals(password)) { request.getSession().setAttribute("userName", userName); request.getRequestDispatcher("success.jsp").forward(request, response); } else { request.getRequestDispatcher("login.jsp").forward(request, response); } } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>LoginSevlet</servlet-name> <servlet-class>org.shirdrn.cluster.tomcat.servlet.LoginSevlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginSevlet</servlet-name> <url-pattern>/LoginSevlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
1.5 将项目导入session.war包放到两个tomcat的\apache-tomcat-6.0.41\webapps 目录下面。
jsf-api.jar 和jsf-impl.jar是J2EE 的包,在myeclipse下面可以搜索到。
第二个红区域内的包在http://code.google.com/p/memcached-session-manager下载,memcached-session-manager-tcx.jar,这个是根据你的tomcat来的我的是tomcat6,所以memcached-session-manager-tc6.jar,如果你的是其它版本则自己去下载相应版本有6,7,8三个版本。
spymemcached-2.10.3.jar是在http://spymemcached.googlecode.com下载
将这些jar 包 放到两个 tomcat 的lib 下面。
tomcat启动问题: MemcachedSessionService starts initialization... (configured nodes definition n1:localhost:11211, failover nodes null)
解决方法:加入spymemcached-2.10.3.jar
3、修改两个tomcat 配置文件
3.1 Tomcat 1 /conf/server.xml
第一个地方:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
第二个地方: </Host> 标签内添加下面内容,端口号默认不改。
<Context docBase="/session.war" path="/session" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211,n2:localhost:11311" <span style="color:#ff6666;">failoverNodes="n1"</span> requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> </Context>
3.2 Tomcat 2 /conf/server.xml
第一个地方:改端口号
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8089" protocol="AJP/1.3" redirectPort="8443" />
第二个地方:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
第三个地方: </Host> 标签内添加下面内容
<Context docBase="/session.war" path="/session" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211,n2:localhost:11311" <span style="color:#ff6666;">failoverNodes="n2"</span> requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> </Context>
tomcat1与tomcat2配置区别就是红色的地方,其它默认。
4、启动memcached
4.1 本机是winows 系统启动两个memcached 需要改端口号,假设Memcached在如下目录:D:\memcached-win32-1.4.4-14,后面我会上传到资源库中。
4.2在CMD中执行如下命令即可再安装一个Memcached服务(记得换个端口号啊!):
sc create "Memcached Server1" start= auto binPath= "D:\memcached-win32-1.4.4-14\memcached.exe -d runservice -m 32 -p 11311 -l localhost" DisplayName= "Memcached Server1"
4.3.下面是启动、停止、卸载该服务的命令:
sc start "Memcached Server1" //启动
sc stop "Memcached Server1" //停止
sc delete "Memcached Server1" //卸载该服务
4.4 启动成功后进程里面会有两个 memcached.exe
5.启动tomcat1和tomcat2
5.1 启动不报错后,我们再来测试两个tomcat之间的数据共享。首先访问tomcat1的 http://localhost:8080/session/login.jsp 用户名 admin 密码 999999 登陆成功后跳转到success.jsp页面显示 当前登录用户:admin ,然后我们关闭tomcat1 , tomcat2不要关闭了。
5.2 访问tomcat2的 http://localhost:8081/session/success.jsp ,也显示 ”当前登陆用户:admin, tomcat 2 我都没有登陆,直接进入到success.jsp竟然也能显示与tomcat1登陆后信息一样,说明 tomcat1 当机了之后,tomcat2 能从memcached 取 tomcat1 保存的信息,从而实现session共享了。
代码是copy别人的,具体的自己引入到eclipse中看。
6. 免费下载链接
1、memcached-win32-1.4.4-14.zip
2、MSM1.8集群相关jar包
3、session.war 项目包