PS:截止到2015-05-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager

前提:你已经部署了Redis,尚未学会的,可以移步这里:http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007

我的案例下载:http://download.csdn.net/detail/caiwenfeng_for_23/8689847

其实很简单,就几个步骤: 
1.配置Tomcat的conf目录下的context.xml文件:

1> 单点Reids配置

    <!--
Jedis save session
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2> Sentinel集群配置:

        <!-- Sentinel 配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
maxInactiveInterval="60"
sentinelMaster="mymaster"
sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382"
/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.添加jar

3.测试

1> 
存储Session:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hello");
//取得Session对象
HttpSession session=request.getSession();
//设置Session属性
for(int i=0;i<100000;i++){
session.setAttribute("name"+i, "Magci_"+i);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2>重启Tomcat:假如Session保存在tomcat下,重启后Session不存在;如果保存在Redis下,Tomcat重启对Session无影响

3>取出Session:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hello");
//取得Session对象
HttpSession session=request.getSession();
//取出Session属性
for(int i=0;i<100000;i++){
System.out.println(session.getAttribute("name"+i));
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

注意事项:从Tomcat6开始默认开启了Session持久化设置,测试时可以关闭本地Session持久化,其实也很简单,在Tomcat的conf目录下的context.xml文件中,取消注释下面那段配置即可:

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
  • 1
  • 2
  • 3
  • 4

详见这篇博客:Session持久化的实例分析

可以尝试运行上面的demo案例!

转:http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831

Tomcat7+Redis存储Session(转)的更多相关文章

  1. (转)Tomcat7+Redis存储Session

    原创http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831 PS:截止到2015-05-12前是不支持Tomcat8的,详情见官 ...

  2. Tomcat 使用Redis存储Session

    Tomcat Redis Session Github 地址. 下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manage ...

  3. Asp.net Core 使用Redis存储Session

    前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...

  4. Redis存储Session

    net Core 使用Redis存储Session   前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(m ...

  5. redis存储session配制方法

    redis存储session配制方法需要三个模块: 1.redis 2.express-session 3.connect-redis 项目中的配置方法代码片段如下: 首先连接redis,连接redi ...

  6. 几分钟搞定redis存储session共享——设计实现

    前面我们写过C#在redis中存储常用的5种数据类型demo,没看过的可以点击电梯直达:https://www.cnblogs.com/xiongze520/p/10267804.html 我们上一篇 ...

  7. Spring Boot+redis存储session,满足集群部署、分布式系统的session共享

    本文讲述spring-boot工程中使用spring-session机制进行安全认证,并且通过redis存储session,满足集群部署.分布式系统的session共享. 原文链接:https://w ...

  8. Redis使用示例及在PHP环境中用redis存储session

    在文件夹redis-3.2.0下 1. 启动redis服务 nohup ./src/redis-server redis.conf & 2. 停止服务 #使用客户端 ./src/redis-c ...

  9. [转]Asp.net Core 使用Redis存储Session

    本文转自:http://www.cnblogs.com/hantianwei/p/5723959.html 前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储 ...

随机推荐

  1. Java基础知识强化之IO流笔记31:转换流出现的原因和格式

    1. 由于字节流操作中文不是特别方便,所以Java就提供了转换流.  字符流 = 字节流 + 编码表 2. 编码表 由字符及其对应数值组成的一张表 常见的编码表: • ASCII/Unicode字符集 ...

  2. Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]

    1. FileInputStream读取数据一次一个字节数组byte[ ]  使用FileInputStream一次读取一个字节数组: int read(byte[]  b) 返回值:返回值其实是实际 ...

  3. RedHat7搭建MongoDB

    yum安装MongoDB 添加MongoDB源# vi /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0] name=MongoDB Rep ...

  4. JPA entity versioning (@Version and Optimistic Locking)

    详情见: http://www.byteslounge.com/tutorials/jpa-entity-versioning-version-and-optimistic-locking

  5. 当append里面的标签显示不出来的时候,用下面的方式做

     $("#result_td").append(tem1+tem3)  $("#result_td").append($(tem1+tem3)) 

  6. 服务器端PHP多进程编程

    待更新 版权声明:本文为博主原创文章,未经博主允许不得转载.

  7. 微软未公开的 SP

    一些用在SQL 2000的企业管理GUI中,并且不打算用于其他的流程.微软已预计将其中的一些存储过程从未来的SQL Server版本中删除(或已经删除了).虽然这些存储过程可能很有用并为你节省了很多时 ...

  8. Java 读取Properties 配置文件

    方法一,使用 io 包中的 BufferedInputStream 以及 FileInputStream读入文件转成字符流,然后使用 lang 包中 的 Properties 的 load 方法进行读 ...

  9. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  10. NoSql之Redis使用(一)

    一.安装 1.下载安装包: 官方网站:redis.io 官方推荐windows版本:https://github.com/MSOpenTech/redis 2:下载压缩包,解压后如下 redis-se ...