Giter VIP home page Giter VIP logo

tomcat-redis-session-manager's Introduction

tomcat-redis-session-manager

使用redis配置tomcat共享session

结构图:

分析:

分布式web server集群部署后需要实现session共享,针对 tomcat 服务器的实现方案多种多样,
比如 tomcat cluster session 广播、nginx IP hash策略、nginx sticky module等方案,
本文主要介绍了使用 redis 服务器进行 session 统一存储管理的共享方案。

必要环境:

  • java1.7
  • tomcat7
  • redis2.8

nginx 负载均衡配置

  1. 修改nginx conf配置文件加入

     upstream tomcat {
         server 200.10.10.67:8110;
         server 200.10.10.67:8120;
         server 200.10.10.44:8110;
         server 200.10.10.66:8110;
     }
    
  2. 配置 相应的server或者 location地址到 http://tomcat

tomcat session共享配置步骤

  1. 添加redis session集群依赖的jar包到 TOMCAT_BASE/lib 目录下

  2. 修改 TOMCAT_BASE/conf 目录下的 context.xml 文件

     	<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
     	<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
          host="localhost"
          port="6379"
          database="0"
          maxInactiveInterval="60"
          sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.."
          sentinelMaster="SentinelMasterName"
          sentinels="sentinel-host-1:port,sentinel-host-2:port,.."/>
    

    属性解释:

    • host redis服务器地址

    • port redis服务器的端口号

    • database 要使用的redis数据库索引

    • maxInactiveInterval session最大空闲超时时间,如果不填则使用tomcat的超时时长,一般tomcat默认为1800 即半个小时

    • sessionPersistPolicies session保存策略,除了默认的策略还可以选择的策略有:

      [SAVE_ON_CHANGE]:每次 session.setAttribute() 、 session.removeAttribute() 触发都会保存. 
      	注意:此功能无法检测已经存在redis的特定属性的变化,
      	权衡:这种策略会略微降低会话的性能,任何改变都会保存到redis中.
      
      [ALWAYS_SAVE_AFTER_REQUEST]: 每一个request请求后都强制保存,无论是否检测到变化.
      	注意:对于更改一个已经存储在redis中的会话属性,该选项特别有用. 
      	权衡:如果不是所有的request请求都要求改变会话属性的话不推荐使用,因为会增加并发竞争的情况。
      
    • sentinelMaster redis集群主节点名称(Redis集群是以分片(Sharding)加主从的方式搭建,满足可扩展性的要求)

    • sentinels redis集群列表配置(类似zookeeper,通过多个Sentinel来提高系统的可用性)

    • connectionPoolMaxTotal

    • connectionPoolMaxIdle jedis最大能够保持idel状态的连接数

    • connectionPoolMinIdle 与connectionPoolMaxIdle相反

    • maxWaitMillis jedis池没有对象返回时,最大等待时间

    • minEvictableIdleTimeMillis

    • softMinEvictableIdleTimeMillis

    • numTestsPerEvictionRun

    • testOnCreate

    • testOnBorrow jedis调用borrowObject方法时,是否进行有效检查

    • testOnReturn jedis调用returnObject方法时,是否进行有效检查

    • testWhileIdle

    • timeBetweenEvictionRunsMillis

    • evictionPolicyClassName

    • blockWhenExhausted

    • jmxEnabled

    • jmxNameBase

    • jmxNamePrefix


  3. 重启tomcat,session存储即可生效

tomcat-redis-session-manager's People

Contributors

izerui avatar

Watchers

James Cloos avatar 锋少 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.