参考的优秀文章

简单地配置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>

RedisSessionManager的类图



关于PersistentManager、FileStore、JDBCStore参见【Session】Tomcat中Session持久化到文件系统或数据库

【Tomcat】Tomcat Session在Redis共享的更多相关文章

  1. Nginx+tomcat集群使用redis共享session

    一 :nginx负载均衡 当Tomcat当做独立的Servlet容器来运行时,可看做是能运行Java Servlet的独立Web服务器. 此外 Tomcat还可以作为其他Web服务器进程内或者进程外的 ...

  2. linux下实现redis共享session的tomcat集群

    为了实现主域名与子域名的下不同的产品间一次登录,到处访问的效果,因此采用rediss实现tomcat的集群效果.基于redis能够异步讲缓存内容固化到磁盘上,从而当服务器意外重启后,仍然能够让sess ...

  3. nginx tomcat负载均衡 使用redis session共享

    环境准备 1.准备一台nginx服务器 ip192.168.1.133 端口81 安装过程: #首先安装依赖: yum -y install gcc-c++ yum -y install pcre p ...

  4. tomcat中session在两个webapp中实现共享

    现在遇到一个需求就是要求完成简单的单点登录,通过在一个tomcat实例中放置两个webapps应用ROOT应用和CEO应用来完成在ROOT应用登录后,在CEO可以直接使用,而未在ROOT应用登录时,不 ...

  5. (转)tomcat架构&session共享

    (二期)16.tomcat的整体架构与session共享方案 [课程16]tomcat...共享.xmind47.6KB [课程16]tomcat...流程.xmind0.6MB [课程16]tomc ...

  6. 配置tomcat的session共享

    可通过下面方法限制一个用户访问一个服务器之后就只在该服务器上操作. 请求负载过程中会话信息不能丢失.那么在多个tomcat中session需要共享. 配置tomcat的session共享可以有三种解决 ...

  7. Tomcat中session共享问题的简单解决办法

    tomcat-redis-session-manager 使用redis配置tomcat共享session 结构图: 分析: 分布式web server集群部署后需要实现session共享,针对 to ...

  8. Memcached做Tomcat的session共享

    基于cache DB缓存的session共享 基于memcache/redis缓存的session共享.即使用cacheDB存取session信息,应用服务器接受新请求将session信息保存在cac ...

  9. 基于redis实现tomcat的session会话保持 (转)

    出处:https://cloud.tencent.com/developer/article/1402997 基于redis实现tomcat的session会话保持 在实际生产中,我们经常部署应用服务 ...

随机推荐

  1. 通过 --py-files 可以在pyspark中可以顺利导入

    文件import问题 问题: 在脚本中import了其他文件, pyspark中可以运行, 但是在spark-submit中总是失败 假定我们的任务脚本为 app.py , 大体代码像这样: from ...

  2. 关于Java擦除特性

    package thinkingInJava; /* * 模拟擦除 */ public class SimpleHolder { private Object obj ; public void se ...

  3. 【iCore3双核心板】【4.3寸液晶驱动板爆照!】

     [源代码完全开源,过几天连同硬件一起发布] 花了好久的时间,我们的fpga工程师才完成这液晶模块的驱动代码,其核心价值如下: 1.完全基于fpga驱动,sdram当做缓存: 2.内建双缓冲机制:方便 ...

  4. XHTML表单

    1.HTML表单标记提供一套元素和属性,用来创建表单,收集网页参观者信息. 2.创建表单结构标记为<form>和</form>. 3.在<form>标记中,我们必须 ...

  5. CentOS下搭建nginx+php环境

    一.下载安装nginx 参见 http://www.cnblogs.com/kreo/p/4378086.html 不再赘述 二.下载php #下载 wget http://bg2.php.net/d ...

  6. Git 实战教程

    CVS及SVN都是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢? 先说集中式版本控制系统,版本库是集中存放在中央服务器的,而大家工作的时候,用的都是自己的 ...

  7. rabbitmq之消息生命周期

    参考:http://jzhihui.iteye.com/blog/1567232

  8. Vim ide for shell development

    Source : This article is part of the ongoing Vi / Vim Tips and Tricks Series. As a Linux sysadmin or ...

  9. UINavigationBar 总结

    一.某个 ViewController 出现隐藏 NavigationBar - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear ...

  10. <构建之法> 第四章 结对 读后感

    粗读 第四章主要讲的是关于结对合作的事项.大多数的软件开发都是团体性的,而合作的最小单位也就是两个人,这也是软件开发中的最佳实践.而结对中,我们能够更好的编写我们的代码,能够少一些担心,对自己的代码也 ...