【Tomcat】Tomcat Session在Redis共享

来源:互联网 发布:什么叫淘宝pc端 编辑:程序博客网 时间:2024/05/17 23:46

【Tomcat】Tomcat Session在Redis共享

参考的优秀文章

  • Redis-backed non-sticky session store for Apache Tomcat

简单地配置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加配置。ValueManager节点的配置是新添加的。

<?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"\xac\xed\x00\x05sr\x00Dcom.orangefunction.tomcat.redissessions.SessionSerializationMetadataB\xd9\xd9\xf7v\xa2\xdbL\x03\x00\x01[\x00\x15sessionAttributesHasht\x00\x02[Bxpw\x14\x00\x00\x00\x10_\xcf\xe5\xbe\x94N\x8a\xa7\a=66_\xf6\x15\xdcxsr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01V\x8e\xa4&\x9asq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\x9asr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexq\x00~\x00\x04\x00\x00\a\bsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x01q\x00~\x00\nsq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\xa3t\x00 5A42DFAD0F66D7D87C85498628168A03sq\x00~\x00\a\x00\x00\x00\x01t\x00\x06userIdsq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\xa3w\b\x00\x00\x01V\x8e\xa4&\x9a"127.0.0.1:6379> 
作者:Nick Huang博客:http://www.cnblogs.com/nick-huang/本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。
标签: Redis
0 0
原创粉丝点击