<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
HttpSession是经过Servlet容器进行建立和管理的,在单机环境中。经过Http请求建立的Session信息是存储在Web服务器内存中,如Tomcat/Jetty。css
假如当用户经过浏览器访问应用服务器,session信息中保存了用户的登陆信息,而且session信息没有过时失,效那么用户就一直处于登陆状态,能够作一些登陆状态的业务操做!html
可是如今不少的服务器都采用分布式集群的方式进行部署,一个Web应用,可能部署在几台不一样的服务器上,经过LVS或者Nginx等进行负载均衡(通常使用Nginx+Tomcat实现负载均衡)。此时来自同一用户的Http请求将有可能被分发到不一样的web站点中去(如:第一次分配到A站点,第二次可能分配到B站点)。那么问题就来了,如何保证不一样的web站点可以共享同一份session数据呢?java
假如用户在发起第一次请求时候访问了A站点,并在A站点的session中保存了登陆信息,当用户第二次发起请求,经过负载均衡请求分配到B站点了,那么此时B站点可否获取用户保存的登陆的信息呢?答案是不能的,由于上面说明,Session是存储在对应Web服务器的内存的,不能进行共享,此时Spring-session就出现了,来帮咱们解决这个session共享的问题!web
简单点说就是请求http请求通过Filter职责链,根据配置信息过滤器将建立session的权利由tomcat交给了Spring-session中的SessionRepository,经过Spring-session建立会话,并保存到对应的地方。redis
实际上实现Session共享的方案不少,其中一种经常使用的就是使用Tomcat、Jetty等服务器提供的Session共享功能,将Session的内容统一存储在一个数据库(如MySQL)或缓存(如Redis,Mongo)中,spring
@Configuration @EnableRedisHttpSession public class RedisSessionConfig { } 若是须要添加失效时间可使用如下的写法: @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60) //1分钟失效
session.setMaxInactiveInterval(3 * 60);//3分钟失效
(二)9.30 出现问题:
根据提示,去相应的页面查看解决方案数据库
Firing SessionDeletedEvent or SessionExpiredEvent is made available through the SessionMessageListener which listens to Redis Keyspace events. In order for this to work, Redis Keyspace events for Generic commands and Expired events needs to be enabled. For example:浏览器
redis-cli config set notify-keyspace-events Egx
If you are using @EnableRedisHttpSession the SessionMessageListener and enabling the necessary Redis Keyspace events is done automatically. However, in a secured Redis enviornment the config command is disabled. This means that Spring Session cannot configure Redis Keyspace events for you. To disable the automatic configuration add ConfigureRedisAction.NO_OP as a bean.缓存
For example, Java Configuration can use the following:tomcat
@Bean public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; }
XML Configuration can use the following:
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
(三)原理简述
以上是常规实现的步骤,spring 帮咱们作了
增长配置类:
官方文档:
The Spring configuration is responsible for creating a Servlet Filter that replaces the HttpSession implementation with an implementation backed by Spring Session.
也就是说,这个配置类能够建立一个过滤器,这个过滤器支持Spring Session代替HttpSession发挥做用。
The @EnableRedisHttpSession annotation creates a Spring Bean with the name of springSessionRepositoryFilter that implements Filter. The filter is what is in charge of replacing the HttpSession implementation to be backed by Spring Session. In this instance Spring Session is backed by Redis.
@EnableRedisHttpSession注解会建立一个springSessionRepositoryFilter的bean对象去实现这个过滤器。过滤器负责代替HttpSession。
也就是说,HttpSession再也不发挥做用,而是经过过滤器使用redis直接操做Session。
(四)redis的数据
key的简单介绍说明:
# 存储 Session 数据,数据类型hash
Key:spring:session:sessions:XXXXXXX
# Redis TTL触发Session 过时。(Redis 自己功能),数据类型:String
Key:spring:session:sessions:expires:XXXXX
#执行 TTL key ,查看剩余生存时间
#定时Job程序触发Session 过时。(spring-session 功能),数据类型:Set
Key:spring:session:expirations:XXXXX
取自:
另有一片源码分析:
Spring Session解决分布式Session问题的实现原理
https://blog.csdn.net/xlgen157387/article/details/60321984