zoukankan      html  css  js  c++  java
  • Tomcat7+Redis存储Session(转)

    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

  • 相关阅读:
    HbuilderX真机运行配置
    Vue前端图片压缩,ios拍照上传出现旋转问题
    inject刷新
    输入框input只能输入数字和小数点
    吸顶效果—position:sticky的vue组件化和兼容性解决
    下拉加载封装
    前端命名规范
    关于表单提交判断不能为空的封装
    js时间格式化
    解决原生打印输入值 打印时为空问题
  • 原文地址:https://www.cnblogs.com/jiligalaer/p/4827278.html
Copyright © 2011-2022 走看看