Tomcat通过memcached实现session共享

相关软件及配置环境:

1、memcached-win64-1.4.4-14  tomcat-6.0.44

2、运行环境:win7 64

3、需要的jar:

 javolution-5.5.1.jar

memcached-session-manager-1.9.0-RC1.jar

memcached-session-manager-tc8-1.9.0-RC1.jar

 msm-javolution-serializer-1.9.0-RC1.jar

 spymemcached-2.8.12.jar

安装步骤(memcached):

1、解压到指定目录下:如 F:\memcached

2、切换到命令行:切换到 F:\memcached

3、安装

memcached.exe -d install

4、启动

memcached.exe -d start

5、查看服务是否正常启动:运行输入 services.msc,查看memcached是否已启动。

Tomacat配置:
1、把上述准备的jar包拷贝到(\tomcat-8.0.24\lib)目录下

2、修改(tomcat-8.0.24\conf\context.xml),增加如下配置:

<Manager classname="de.javakaffee.web.msm.MemcachedBackupSessionManager" 

    lockingMode="auto" 

    memcachedNodes="n1:127.0.0.1:11211" 

    requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" 

    sessionBackupAsync="false" 

    sessionBackupTimeout="100" 

    sticky="false" 

    transcoderFactoryclass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>

3、拷贝一份Tomcat, 修改其中一个tomcat的配置(omcat-8.0.24\conf\server.xml

<Server port="9005" shutdown="SHUTDOWN"> 原为8005;

<Connector connectionTimeout="20000" port="9080" protocol="HTTP/1.1" redirectPort="8443"/>原为8080;

<Connector port="9009" protocol="AJP/1.3" redirectPort="8443"/>原为8009;

4、测试Tomcat是否能正常启动。

5、编写测试程序(通过memcached缓存自定义对象信息)

1)自定义类User,必须实现Serializable接口。

import java.io.Serializable;

public class User implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8185062937031496066L;
	/**
	 * 
	 */
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

2)自定义的servlet类:

package cn.sjf.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.sjf.pojo.User;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		PrintWriter out = response.getWriter();
		if (session == null) {
			System.out.println("null");
		} else {
			if (null == session.getAttribute("userinfo")) {
				User user = new User();
				user.setUsername("test2222");
				user.setPassword("helloworld");
				session.setAttribute("userinfo", user);
				out.println("当前用户在session中不存在,需要新创建,并保存到session中。<br>");
				out.println("username:" + user.getUsername() + ",password:" + user.getPassword() + "<br>");
				out.println("当前访问的url:" + request.getRequestURL());
			} else {
				User user = (User) session.getAttribute("userinfo");
				out.println("当前用户在session中存在。<br>");
				out.println("username:" + user.getUsername() + ",password:" + user.getPassword() + "<br>");
				out.println("当前访问的url:" + request.getRequestURL() + "<br>");
			}
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

 3)如果出现乱码,自己可以配置个编码过滤器(略)

 4)把应用分别部署到两个Tomcat下。

 5)在同一个浏览器中,打开两个tab页,分别访问:

http://localhost:8080/memcached1/LoginServlet

 Tomcat通过memcached实现session共享

http://localhost:9080/memcached1/LoginServlet

Tomcat通过memcached实现session共享

至此,tomcat集成memcached完成。

相关推荐