zoukankan      html  css  js  c++  java
  • Tomcat 8 的session共享解决(redis)

    如果英文不错的看,建议直接看官网吧,官网写的挺清楚。下面的内容是转载的一篇文章,自己补充了一些,供大家参考,也欢迎大家一起讨论

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

    锐洋智能修改的支持Tomcat8的 reyo.redis.session.manager.tomcat8

    修改的源代码:RedisSessionManager.java

        @SuppressWarnings("deprecation")
        private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
            log.info("Attempting to use serializer :" + serializationStrategyClass);
            serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();

            Loader loader = null;

            if (getContainer() != null) {
                loader = getContainer().getLoader();
            }

            ClassLoader classLoader = null;

            if (loader != null) {
                classLoader = loader.getClassLoader();
            }
            serializer.setClassLoader(classLoader);
        }

    修改后的内容

        private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
            log.info("Attempting to use serializer :" + serializationStrategyClass);
            serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();

            Loader loader = null;
            Context context = this.getContext();
            if (context != null) {
                loader = context.getLoader();
            }

            ClassLoader classLoader = null;

            if (loader != null) {
                classLoader = loader.getClassLoader();
            }
            serializer.setClassLoader(classLoader);
        }

    前提:你已经部署了Redis,尚未学会的略过

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

    1> 单点Reids配置

    <Valve className="reyo.redis.session.manager.tomcat8.RedisSessionHandlerValve" />        
    <Manager className="reyo.redis.session.manager.tomcat8.RedisSessionManager"
        host="localhost"
        port="6379"
        database="0"
        password="reyo"
        maxInactiveInterval="60"/>

    2> Sentinel集群配置:

    <!-- Sentinel 配置 -->

    <Valve className="reyo.redis.session.manager.tomcat8.RedisSessionHandlerValve" />        
    <Manager className="reyo.redis.session.manager.tomcat8.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" />

    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); 
            }
        }

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

    3>取出Session:

     protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
            System.out.println("hello");           
            HttpSession session=request.getSession(); 
            //取出Session属性
            for(int i=0;i<100000;i++){
                System.out.println(session.getAttribute("name"+i));
            }
        }

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

     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->

     

    需要注意的是:

    web.xml中的配置是有效的,即使是context.xml总配置maxInactiveInterval默认60秒,只要web.xml中的sessionConfig配置30分钟,则session的失效时间还是30分钟。

     

    运行效果图:

    一:redis主从服务器

    二:redis Sentinel集群(三台)

    三:tomcat8.x 集群(两台)

    Sentinel集群下的tomcat...

    四:nginx作为前端服务器

    五:网站运行效果图:

     实例测试地址:http://sms.reyo.cn 

    用户名:aa 密码:123456

     

  • 相关阅读:
    OpenGL Geometry Shader
    正向渲染路径细节 Forward Rendering Path Details
    Tessellation
    渲染路径-实时渲染中常用的几种Rendering Path
    ugui batches
    如何有效提升Unity Gear VR游戏性能
    Unity3D命令行Build
    手机游戏资源 特效 显存分析工具
    换装demo时美术遇到的问题详解
    Rigging a Versatile Weapon Bone for 3ds Max
  • 原文地址:https://www.cnblogs.com/interdrp/p/4868740.html
Copyright © 2011-2022 走看看