【转】spring session redis 整合

【转】http://blog.csdn.net/xiejx618/article/details/42919327

参考资料:

http://projects.spring.io/spring-session/#quick-start
http://docs.spring.io/spring-session/docs/current-SNAPSHOT/reference/html5/guides/httpsession.html#httpsession-sample

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.主要依赖

 

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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服务端交互.

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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事件

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. package org.exam.config;  
  2. import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;  
  3. /** 
  4.  * Created by xin on 15/1/20. 
  5.  */  
  6. public class SessionApplicationInitializer extends AbstractHttpSessionApplicationInitializer {  
  7.     @Override  
  8.     protected void afterSessionRepositoryFilter(ServletContext servletContext) {  
  9.         servletContext.addListener(new HttpSessionEventPublisher());  
  10.     }  
  11. }  
4.将SessionConfig加入到org.exam.config.DispatcherServletInitializer#getRootConfigClasses,不要加到ServletConfigClasses,至于原因看http://blog.csdn.net/xiejx618/article/details/50603758文末
[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. protected Class<?>[] getRootConfigClasses() {  
  3.     return new Class<?>[] {AppConfig.class,SessionConfig.class};  
  4. }  

5.使用例子.

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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命令

源码例子:

http://download.csdn.net/detail/xiejx618/9369518

 

使用redis集群的一个例子:

 

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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>  
[plain]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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  

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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了

猜你喜欢

转载自ln-software.iteye.com/blog/2328879