使用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中添加這兩個依賴:

< dependency >
     < groupId >org.springframework.data</ groupId >
     < artifactId >spring-data-redis</ artifactId >
     < version >1.5.2.RELEASE</ version >
</ dependency >
< dependency >
     < groupId >org.springframework.session</ groupId >
     < artifactId >spring-session</ artifactId >
     < version >1.0.2.RELEASE</ version >
</ dependency >

  再在applicationContext.xml中添加以下bean,用於定義redis的連接池和初始化redis模版操作類,自行替換其中的相關變量。

<!-- redis -->
< bean id = "jedisPoolConfig" class = "redis.clients.jedis.JedisPoolConfig" >
</ bean >
 
< bean id = "jedisConnectionFactory"
     class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
     < property name = "hostName" value = "${redis.host}" />
     < property name = "port" value = "${redis.port}" />
     < property name = "password" value = "${redis.pass}" />
     < property name = "timeout" value = "${redis.timeout}" />
     < property name = "poolConfig" ref = "jedisPoolConfig" />
     < property name = "usePool" value = "true" />
</ bean >
 
< bean id = "redisTemplate" class = "org.springframework.data.redis.core.StringRedisTemplate" >
     < property name = "connectionFactory" ref = "jedisConnectionFactory" />
</ bean >
 
<!-- 將session放入redis -->
< bean id = "redisHttpSessionConfiguration"
class = "org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
     < property name = "maxInactiveIntervalInSeconds" value = "1800" />
</ bean >

  這里前面幾個bean都是操作redis時候使用的,最后一個bean才是spring-session需要用到的,其中的id可以不寫或者保持不變,這也是一個約定優先配置的體現。這個bean中又會自動產生多個bean,用於相關操作,極大的簡化了我們的配置項。其中有個比較重要的是springSessionRepositoryFilter,它將在下面的代理filter中被調用到。maxInactiveIntervalInSeconds表示超時時間,默認是1800秒。寫上述配置的時候我個人習慣采用xml來定義,官方文檔中有采用注解來聲明一個配置類。

  然后是在web.xml中添加一個session代理filter,通過這個filter來包裝Servlet的getSession()。需要注意的是這個filter需要放在所有filter鏈最前面。

<!-- delegatingFilterProxy -->
< filter >
     < filter-name >springSessionRepositoryFilter</ filter-name >
     < filter-class >org.springframework.web.filter.DelegatingFilterProxy</ filter-class >
</ filter >
< filter-mapping >
     < filter-name >springSessionRepositoryFilter</ filter-name >
     < url-pattern >/*</ url-pattern >
</ filter-mapping >

  這樣便配置完畢了,需要注意的是,spring-session要求Redis Server版本不低於2.8。

  驗證:使用redis-cli就可以查看到session key了,且瀏覽器Cookie中的jsessionid已經替換為session。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM