使用Spring Session做分布式会话管理

  在Web项目开发中,会话管理是一个很重要的部分,用于存储与用户相关的数据。通常是由符合session规范的容器来负责存储管理,也就是一旦容器关闭,重启会导致会话失效。因此打造一个高可用性的系统,必须将session管理从容器中独立出来。而这实现方案有很多种,下面简单介绍下:

  第一种是使用容器扩展来实现,大家比较容易接受的是通过容器插件来实现,比如基于Tomcat的tomcat-redis-session-manager,基于Jetty的jetty-session-redis等等。好处是对项目来说是透明的,无需改动代码。不过前者目前还不支持Tomcat 8,或者说不太完善。个人觉得由于过于依赖容器,一旦容器升级或者更换意味着又得从新来过。并且代码不在项目中,对开发者来说维护也是个问题。

  第二种是自己写一套会话管理的工具类,包括Session管理和Cookie管理,在需要使用会话的时候都从自己的工具类中获取,而工具类后端存储可以放到Redis中。很显然这个方案灵活性最大,但开发需要一些额外的时间。并且系统中存在两套Session方案,很容易弄错而导致取不到数据。

  第三种是使用框架的会话管理工具,也就是本文要说的spring-session,可以理解是替换了Servlet那一套会话管理,既不依赖容器,又不需要改动代码,并且是用了spring-data-redis那一套连接池,可以说是最完美的解决方案。当然,前提是项目要使用Spring Framework才行。

  这里简单记录下整合的过程:

  如果项目之前没有整合过spring-data-redis的话,这一步需要先做,在maven中添加这两个依赖:

 1 <dependency>
 2     <groupId>org.springframework.data</groupId>
 3     <artifactId>spring-data-redis</artifactId>
 4     <version>1.5.2.RELEASE</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>org.springframework.session</groupId>
 8     <artifactId>spring-session</artifactId>
 9     <version>1.0.2.RELEASE</version>
10 </dependency>

  再在applicationContext.xml中添加以下bean,用于定义redis的连接池和初始化redis模版操作类,自行替换其中的相关变量。

 1 <!-- redis -->
 2 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
 3 </bean>
 4  
 5 <bean id="jedisConnectionFactory"
 6     class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
 7     <property name="hostName" value="${redis.host}" />
 8     <property name="port" value="${redis.port}" />
 9     <property name="password" value="${redis.pass}" />
10     <property name="timeout" value="${redis.timeout}" />
11     <property name="poolConfig" ref="jedisPoolConfig" />
12     <property name="usePool" value="true" />
13 </bean>
14  
15 <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
16     <property name="connectionFactory" ref="jedisConnectionFactory" />
17 </bean>
18  
19 <!-- 将session放入redis -->
20 <bean id="redisHttpSessionConfiguration"
21 class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
22     <property name="maxInactiveIntervalInSeconds" value="1800" />
23 </bean>

  这里前面几个bean都是操作redis时候使用的,最后一个bean才是spring-session需要用到的,其中的id可以不写或者保持不变,这也是一个约定优先配置的体现。这个bean中又会自动产生多个bean,用于相关操作,极大的简化了我们的配置项。其中有个比较重要的是springSessionRepositoryFilter,它将在下面的代理filter中被调用到。maxInactiveIntervalInSeconds表示超时时间,默认是1800秒。写上述配置的时候我个人习惯采用xml来定义,官方文档中有采用注解来声明一个配置类。

  然后是在web.xml中添加一个session代理filter,通过这个filter来包装Servlet的getSession()。需要注意的是这个filter需要放在所有filter链最前面。

1 <!-- delegatingFilterProxy -->
2 <filter>
3     <filter-name>springSessionRepositoryFilter</filter-name>
4     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
5 </filter>
6 <filter-mapping>
7     <filter-name>springSessionRepositoryFilter</filter-name>
8     <url-pattern>/*</url-pattern>
9 </filter-mapping>

  这样便配置完毕了,需要注意的是,spring-session要求Redis Server版本不低于2.8。

  验证:使用redis-cli就可以查看到session key了,且浏览器Cookie中的jsessionid已经替换为session。

1 127.0.0.1:6379> KEYS *
2 1) "spring:session:expirations:1440922740000"
3 2) "spring:session:sessions:35b48cb4-62f8-440c-afac-9c7e3cfe98d3"

  补充:

  spring session提供以下功能:
1.API and implementations for managing a user's session
2.HttpSession - allows replacing the HttpSession in an application container (i.e. Tomcat) neutral way
2.1.Clustered Sessions - Spring Session makes it trivial to support clustered sessions without being tied to an application container specific solution.
2.2.Multiple Browser Sessions - Spring Session supports managing multiple users' sessions in a single browser instance (i.e. multiple authenticated accounts similar to Google).
2.3.RESTful APIs - Spring Session allows providing session ids in headers to work with RESTful APIs
3.WebSocket - provides the ability to keep the HttpSession alive when receiving WebSocket messages
仅是集群session功能,都是振奋人心的.spring session是通过filter嵌入去实现的(spring security也是使用这种方式),下面是个例子.

1.主要依赖 

 1 <dependency>  
 2     <groupId>org.springframework.data</groupId>  
 3     <artifactId>spring-data-redis</artifactId>  
 4     <version>1.4.1.RELEASE</version>  
 5 </dependency>  
 6 <dependency>  
 7     <groupId>redis.clients</groupId>  
 8     <artifactId>jedis</artifactId>  
 9     <version>2.5.2</version>  
10 </dependency>  
11 <dependency>  
12     <groupId>org.springframework.session</groupId>  
13     <artifactId>spring-session</artifactId>  
14     <version>${spring.session.version}</version>  
15 </dependency>  

2.写一个configuration来启用RedisHttpSession,在这个配置注册一个redis客户端的连接工厂Bean,供Spring Session用于与redis服务端交互.

 
 1 package org.exam.config;  
 2 import org.springframework.context.annotation.Bean;  
 3 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
 4 import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;  
 5 /** 
 6  * Created by xin on 15/1/20. 
 7  */  
 8 @EnableRedisHttpSession  
 9 public class SessionConfig {  
10     @Bean  
11     public JedisConnectionFactory connectionFactory() {  
12         return new JedisConnectionFactory();  
13     }  
14 }  

3.写一个Initializer,主要用于向应用容器添加springSessionRepositoryFilter,顺便注册一下HttpSessionEventPublisher监听,这个监听的作用发布HttpSessionCreatedEvent和HttpSessionDestroyedEvent事件

1 package org.exam.config;  
2 import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;  
3 
4 public class SessionApplicationInitializer extends AbstractHttpSessionApplicationInitializer {  
5     @Override  
6     protected void afterSessionRepositoryFilter(ServletContext servletContext) {  
7         servletContext.addListener(new HttpSessionEventPublisher());  
8     }  
9 }  
4.将SessionConfig加入到org.exam.config.DispatcherServletInitializer#getRootConfigClasses,不要加到ServletConfigClasses,至于原因看http://blog.csdn.net/xiejx618/article/details/50603758文末
1 @Override  
2 protected Class<?>[] getRootConfigClasses() {  
3     return new Class<?>[] {AppConfig.class,SessionConfig.class};  
4 }  

 

5.使用例子.

 1 package org.exam.web;  
 2 import org.springframework.stereotype.Controller;  
 3 import org.springframework.ui.Model;  
 4 import org.springframework.web.bind.annotation.RequestMapping;  
 5 import javax.servlet.http.HttpServletRequest;  
 6 import javax.servlet.http.HttpSession;  
 7 /** 
 8  * Created by xin on 15/1/7. 
 9  */  
10 @Controller  
11 public class DefaultController {  
12     @RequestMapping("/")  
13     public String index(Model model,HttpServletRequest request,String action,String msg){  
14         HttpSession session=request.getSession();  
15         if ("set".equals(action)){  
16             session.setAttribute("msg", msg);  
17         }else if ("get".equals(action)){  
18             String message=(String)session.getAttribute("msg");  
19             model.addAttribute("msgFromRedis",message);  
20         }  
21         return "index";  
22     }  
23 }  

  得到这个被spring session包装过的session,像平常一样直接使用.
  6.测试.先启动redis服务端.
  请求:localhost:8080/testweb/?action=set&msg=123   把123通过spring session set到redis去.
  请求:localhost:8080/testweb/?action=get  从redis取出刚才存入的值.

  从Redis删除存入去相关的值,再次请求localhost:8080/testweb/?action=get查看结果

  redis:

  a.查询所有key:keys命令,keys *

  b.根据某个key删除,使用del命令

  源码例子:

  使用redis集群的一个例子:

 
 1 <dependency>  
 2     <groupId>org.springframework.data</groupId>  
 3     <artifactId>spring-data-redis</artifactId>  
 4     <version>1.7.1.RELEASE</version>  
 5 </dependency>  
 6 <dependency>  
 7     <groupId>org.apache.commons</groupId>  
 8     <artifactId>commons-pool2</artifactId>  
 9     <version>2.4.2</version>  
10 </dependency>  
11 <dependency>  
12     <groupId>redis.clients</groupId>  
13     <artifactId>jedis</artifactId>  
14     <version>2.8.1</version>  
15 </dependency>  
16 <dependency>  
17     <groupId>org.springframework.session</groupId>  
18     <artifactId>spring-session</artifactId>  
19     <version>1.1.1.RELEASE</version>  
20 </dependency>  
1 #REDIS START  
2 redis.maxRedirections=10  
3 redis.maxWaitMillis=1500  
4 redis.maxTotal=2048  
5 redis.minIdle=20  
6 redis.maxIdle=200  
7 redis.jedisClusterNodes=192.168.1.250:6380,192.168.1.250:6381,192.168.1.250:6382  
8 #REDIS END 
 1 @Configuration  
 2 @EnableRedisHttpSession  
 3 public class HttpSessionConfig implements EnvironmentAware {  
 4     private Environment env;  
 5     @Bean  
 6     public JedisConnectionFactory jedisConnectionFactory() {  
 7         String[] jedisClusterNodes = env.getProperty("redis.jedisClusterNodes").split(",");  
 8         RedisClusterConfiguration clusterConfig=new RedisClusterConfiguration(Arrays.asList(jedisClusterNodes));  
 9         clusterConfig.setMaxRedirects(env.getProperty("redis.maxRedirections",Integer.class));  
10   
11         JedisPoolConfig poolConfig=new JedisPoolConfig();  
12         poolConfig.setMaxWaitMillis(env.getProperty("redis.maxWaitMillis",Integer.class));  
13         poolConfig.setMaxTotal(env.getProperty("redis.maxTotal",Integer.class));  
14         poolConfig.setMinIdle(env.getProperty("redis.minIdle",Integer.class));  
15         poolConfig.setMaxIdle(env.getProperty("redis.maxIdle",Integer.class));  
16   
17         return new JedisConnectionFactory(clusterConfig,poolConfig);  
18     }  
19   
20     @Override  
21     public void setEnvironment(Environment environment) {  
22         this.env=environment;  
23     }  
24 }  

下面顺便跟踪下实现吧:

1.注册springSessionRepositoryFilter位置在:org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer#insertSessionRepositoryFilter,从org.springframework.web.filter.DelegatingFilterProxy#initDelegate可以看出会去找名为springSessionRepositoryFilter Bean的实现作为Filter的具体实现.
2.因为使用了@EnableRedisHttpSession,就会使用org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration,这个配置里注册的springSessionRepositoryFilter Bean就是SessionRepositoryFilter.即springSessionRepositoryFilter的实现为org.springframework.session.web.http.SessionRepositoryFilter
3.Filter每一次的请求都会调用doFilter,即调用SessionRepositoryFilter的父类OncePerRequestFilter的doFilter,此方法会调用SessionRepositoryFilter自身的doFilterInternal.这个方法如下:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {  
  2.     request.setAttribute(SESSION_REPOSITORY_ATTR, sessionRepository);  
  3.     SessionRepositoryRequestWrapper wrappedRequest = new SessionRepositoryRequestWrapper(request, response);  
  4.     SessionRepositoryResponseWrapper wrappedResponse = new SessionRepositoryResponseWrapper(wrappedRequest,response);  
  5.     HttpServletRequest strategyRequest = httpSessionStrategy.wrapRequest(wrappedRequest, wrappedResponse);  
  6.     HttpServletResponse strategyResponse = httpSessionStrategy.wrapResponse(wrappedRequest, wrappedResponse);  
  7.     try {  
  8.         filterChain.doFilter(strategyRequest, strategyResponse);  
  9.     } finally {  
  10.         wrappedRequest.commitSession();  
  11.     }  
  12. }  

4.从这里就知request经过了包装,httpSessionStrategy的默认值是new CookieHttpSessionStrategy(),可以猜测它结合了cookie来实现,当然里面的getSession方法也重写了.org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryRequestWrapper#getSession(boolean)方法如下:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public HttpSession getSession(boolean create) {  
  2.     if(currentSession != null) {  
  3.         return currentSession;  
  4.     }  
  5.     String requestedSessionId = getRequestedSessionId();  
  6.     if(requestedSessionId != null) {  
  7.     S session = sessionRepository.getSession(requestedSessionId);  
  8.         if(session != null) {  
  9.             this.requestedValidSession = true;  
  10.             currentSession = new HttpSessionWrapper(session, getServletContext());  
  11.             currentSession.setNew(false);  
  12.             return currentSession;  
  13.         }  
  14.     }  
  15.     if(!create) {  
  16.         return null;  
  17.     }  
  18.     S session = sessionRepository.createSession();  
  19.     currentSession = new HttpSessionWrapper(session, getServletContext());  
  20.     return currentSession;  
  21. }  

即上面的例子调用getSession会调用此方法来获取Session.而此Session是通过sessionRepository创建的,此处注入的是org.springframework.session.data.redis.RedisOperationsSessionRepository(sessionRepository的注册也是在org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration),而不是应用服务器本身去创建的.

可以继续看看org.springframework.session.data.redis.RedisOperationsSessionRepository#createSession

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public RedisSession createSession() {  
  2.     RedisSession redisSession = new RedisSession();  
  3.     if(defaultMaxInactiveInterval != null) {  
  4.         redisSession.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);  
  5.     }  
  6.     return redisSession;  
  7. }  

这里new了一个RedisSession,继续看org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession#RedisSession()

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. RedisSession() {  
  2.             this(new MapSession());  
  3.             delta.put(CREATION_TIME_ATTR, getCreationTime());  
  4.             delta.put(MAX_INACTIVE_ATTR, getMaxInactiveIntervalInSeconds());  
  5.             delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());  
  6.         }  
  7.         RedisSession(MapSession cached) {  
  8.             Assert.notNull("MapSession cannot be null");  
  9.             this.cached = cached;  
  10.         }  
  11.            

这里又new了一个MapSession并赋给了cached变量,再看org.springframework.session.MapSession片段:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public static final int DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS = 1800;  
  2.   
  3. private String id = UUID.randomUUID().toString();  
  4. private Map<String, Object> sessionAttrs = new HashMap<String, Object>();  
  5. private long creationTime = System.currentTimeMillis();  
  6. private long lastAccessedTime = creationTime;  
  7.   
  8. /** 
  9.  * Defaults to 30 minutes 
  10.  */  
  11. private int maxInactiveInterval = DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;  

从这里你可以基本猜测id就是sessionid,这个UUID就是区分不同的客户端的一个唯一标识,它会写入到客户端的cookie,session的有效时间是存在什么地方了,cached和delta都有存.最后就要看它怎么保存到redis里面去了.下面再看看如何保存到redis去:response是经过了SessionRepositoryResponseWrapper包装,SessionRepositoryResponseWrapper是OnCommittedResponseWrapper的子类,服务端一旦调用response.getWriter()就会触发org.springframework.session.web.http.OnCommittedResponseWrapper#getWriter

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. public PrintWriter getWriter() throws IOException {  
  3.     return new SaveContextPrintWriter(super.getWriter());  
  4. }  
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. private class SaveContextPrintWriter extends PrintWriter {  
  2.     private final PrintWriter delegate;  
  3.   
  4.     public SaveContextPrintWriter(PrintWriter delegate) {  
  5.         super(delegate);  
  6.         this.delegate = delegate;  
  7.     }  
  8.   
  9.     public void flush() {  
  10.         doOnResponseCommitted();  
  11.         delegate.flush();  
  12.     }  
  13.   
  14.     public void close() {  
  15.         doOnResponseCommitted();  
  16.         delegate.close();  
  17.     }  

一旦调用out.flush或out.close都会触发doOnResponseCommitted()方法,

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. private void doOnResponseCommitted() {  
  2.     if(!disableOnCommitted) {  
  3.         onResponseCommitted();  
  4.         disableOnResponseCommitted();  
  5.     } else if(logger.isDebugEnabled()){  
  6.         logger.debug("Skip invoking on");  
  7.     }  
  8. }  

回来org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryResponseWrapper#onResponseCommitted

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. protected void onResponseCommitted() {  
  3.     request.commitSession();  
  4. }  

再回来org.springframework.session.web.http.SessionRepositoryFilter.SessionRepositoryRequestWrapper#commitSession

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. private void commitSession() {  
  2.     HttpSessionWrapper wrappedSession = currentSession;  
  3.     if(wrappedSession == null) {  
  4.         if(isInvalidateClientSession()) {  
  5.             httpSessionStrategy.onInvalidateSession(this, response);  
  6.         }  
  7.     } else {  
  8.         S session = wrappedSession.session;  
  9.         sessionRepository.save(session);  
  10.         if(!requestedValidSession) {  
  11.             httpSessionStrategy.onNewSession(session, this, response);  
  12.         }  
  13.     }  
  14. }  

终于看到sessionRepository调用save了

博客地址:http://blog.csdn.net/patrickyoung6625/article/details/45694157

 

  

  

 

posted @ 2017-05-11 10:05  ngulc  阅读(1337)  评论(0编辑  收藏  举报