spring session實現集群中session共享


本文轉自:http://dorole.com/1422/

使用框架的會話管理工具,也就是本文要說的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。

 


免責聲明!

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



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