如題所示,在分佈式系統架構中須要解決的一個很重要的問題就是——如何保證各個應用節點之間的Session共享。其實有一個很好的解決辦法就是在redis、memcached等組件中獨立存儲全部應用節點的Session,以達到各個應用節點之間的Session共享的目的javascript
Spring Session提供了一種獨立於應用服務器的方案,這種方案可以在Servlet規範以內配置可插拔的session數據存儲,不依賴於任何應用服務器的特定API。這就意味着Spring Session可以用於實現了servlet規範的全部應用服務器之中(Tomcat、Jetty、 WebSphere、WebLogic、JBoss等),它可以很是便利地在全部應用服務器中以徹底相同的方式進行配置。同時咱們還能夠選擇任意最適應需求的外部session數據存儲方式php
Spring Session爲企業級Java應用的session管理帶來了革新,使得如下的功能更加容易實現:java
須要說明的很重要的一點就是,Spring Session的核心項目並不依賴於Spring框架,因此,咱們甚至可以將其應用於不使用Spring框架的項目中web
其實,咱們想要在項目中使用Spring Session須要作的工做並很少,只須要作到如下幾步便可:redis
這裏須要引入Spring Session和Spring Data Redis相關的依賴jar包。能夠自行從maven倉庫下載,也能夠參考我使用的jar包:down.51cto.com/data/228183…spring
在項目的spring-data-redis相關配置中添加如下配置:瀏覽器
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<!--超時時間,默認1800秒-->
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>複製代碼
完整的context_redis_cluster.xml文件以下:tomcat
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<description>spring-data-redis-cluster</description>
<!-- 配置Cluster -->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.cluster.maxRedirects}" />
<!-- 節點配置 -->
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.cluster.host1}" />
<constructor-arg name="port" value="${redis.cluster.port1}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.cluster.host2}" />
<constructor-arg name="port" value="${redis.cluster.port2}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.cluster.host3}" />
<constructor-arg name="port" value="${redis.cluster.port3}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.cluster.host4}" />
<constructor-arg name="port" value="${redis.cluster.port4}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.cluster.host5}" />
<constructor-arg name="port" value="${redis.cluster.port5}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.cluster.host6}" />
<constructor-arg name="port" value="${redis.cluster.port6}" />
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.cluster.jedisPoolConfig.maxTotal}" />
<property name="maxIdle" value="${redis.cluster.jedisPoolConfig.maxIdle}" />
<property name="maxWaitMillis" value="${redis.cluster.jedisPoolConfig.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.cluster.jedisPoolConfig.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
<property name="password" value="${redis.cluster.jedisConnectionFactory.password}" />
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory">
<!-- 序列化方法 -->
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="jdkSerializer" />
<property name="hashValueSerializer" ref="jdkSerializer" />
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<!--超時時間,默認1800秒-->
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
</beans>複製代碼
注:對應的屬性文件是:服務器
#Redis Cluster
redis.cluster.maxRedirects=3
redis.cluster.host1=192.168.1.30
redis.cluster.port1=7000
redis.cluster.host2=192.168.1.30
redis.cluster.port2=7001
redis.cluster.host3=192.168.1.30
redis.cluster.port3=7002
redis.cluster.host4=192.168.1.224
redis.cluster.port4=7003
redis.cluster.host5=192.168.1.224
redis.cluster.port5=7004
redis.cluster.host6=192.168.1.224
redis.cluster.port6=7005
#JedisPoolConfig
redis.cluster.jedisPoolConfig.maxTotal=1024
redis.cluster.jedisPoolConfig.maxIdle=20
redis.cluster.jedisPoolConfig.maxWaitMillis=100000
redis.cluster.jedisPoolConfig.testOnBorrow=true
#JedisConnectionFactory
redis.cluster.jedisConnectionFactory.password=admin複製代碼
在web.xml中添加如下filter:cookie
<!-- Spring Session配置 -->
<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>複製代碼
注:須要將這個filter放在第一位,其餘的如編碼、shiro等filter須要放在這以後
到此,Spring Session和Spring Data Redis就整合到項目中了
運行項目後,能夠發現生成了一個名爲「SESSION」的cookie。接着在咱們登陸以後,能夠經過其cookie值在redis上取得保存的對應的session對象。以下圖所示:
參考: