参考的优秀文章
简单地配置Tomcat Session在Redis共享
我使用的是现有的框架,见:Redis-backed non-sticky session store for Apache Tomcat。
Tomcat使用的是apache-tomcat-7.0.69
。
一、首先,下载好所需的包,放入Tomcat的lib目录下,我使用的是:
- jedis-2.8.2.jar
- tomcat-redis-session-manager1.2.jar
- commons-pool2-2.4.2.jar
二、需在%TOMCAT_HOME%/conf/context.xml
加配置。Value
和Manager
节点的配置是新添加的。
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.9"
port="6379"
database="0"
maxInactiveInterval="60" />
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
三、既然要将Session放在Redis中,那当然要搭建Redis了。可参考:【Redis】Redis的基本安装及使用
四、编写应用包含写、读Session的JSP文件,然后部署在两个Tomcat中。
这里分别是写、读、删Session的两个JSP:
<%@ page import="java.util.Date"%>
<%@ 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>Write session</title>
</head>
<body>
Server : Server 1 (因需将页面部署在不同Tomcat,以测试能读取另一台Tomcat设置的Session,请自行设服务器标识)<br />
Write session
<%
request.getSession().setAttribute("userId", new Date().getTime());
%>
</body>
</html>
<%@ 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>Read session</title>
</head>
<body>
Server : Server 1 (因需将页面部署在不同Tomcat,以测试能读取另一台Tomcat设置的Session,请自行设服务器标识)<br />
<%=request.getSession().getAttribute("userId")%>
</body>
</html>
<%@ page import="java.util.Date"%>
<%@ 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>Write session</title>
</head>
<body>
Server : Server 1 (因需将页面部署在不同Tomcat,以测试能读取另一台Tomcat设置的Session,请自行设服务器标识)<br />
Remove session
<%
request.getSession().removeAttribute("userId");
%>
</body>
</html>
五、访问一个Tomcat的write.jsp后,再访问另一个Tomcat的read.jsp,看是否能读取另一个Tomcat写入的Session。另外,可以用Redis自带的客户端查看是否写入了键值,我的日志如下:
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "5A42DFAD0F66D7D87C85498628168A03"
127.0.0.1:6379>
127.0.0.1:6379> get 5A42DFAD0F66D7D87C85498628168A03
"xacxedx00x05srx00Dcom.orangefunction.tomcat.redissessions.SessionSerializationMetadataBxd9xd9xf7vxa2xdbLx03x00x01[x00x15sessionAttributesHashtx00x02[Bxpwx14x00x00x00x10_xcfxe5xbex94Nx8axa7a=66_xf6x15xdcxsrx00x0ejava.lang.Long;x8bxe4x90xccx8f#xdfx02x00x01Jx00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00x01Vx8exa4&x9asqx00~x00x03x00x00x01Vx8exa4&x9asrx00x11java.lang.Integerx12xe2xa0xa4xf7x81x878x02x00x01Ix00x05valuexqx00~x00x04x00x00asrx00x11java.lang.Booleanxcd rx80xd5x9cxfaxeex02x00x01Zx00x05valuexpx01qx00~x00
sqx00~x00x03x00x00x01Vx8exa4&xa3tx00 5A42DFAD0F66D7D87C85498628168A03sqx00~x00ax00x00x00x01tx00x06userIdsqx00~x00x03x00x00x01Vx8exa4&xa3wx00x00x01Vx8exa4&x9a"
127.0.0.1:6379>
RedisSessionManager的类图
关于PersistentManager、FileStore、JDBCStore参见【Session】Tomcat中Session持久化到文件系统或数据库。