Spring Session+Spring Data Redis 解決分佈式系統架構中 Session 共享問題

一 簡介

如題所示,在分佈式系統架構中須要解決的一個很重要的問題就是——如何保證各個應用節點之間的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

  • 編寫可水平擴展的原生雲應用
  • 將session所保存的狀態保存到特定的外部session存儲中,如Redis或Apache Geode中,它們可以以獨立於應用服務器的方式提供高質量的集羣
  • 當用戶使用WebSocket發送請求的時候,可以保持HttpSession處於活躍狀態
  • 在非Web請求的處理代碼中,可以訪問session數據,好比在JMS消息的處理代碼中
  • 支持每一個瀏覽器上使用多個session,從而可以很容易地構建更加豐富的終端用戶體驗
  • 控制session id如何在客戶端和服務器之間進行交換,這樣的話就能很容易地編寫Restful API,由於它能夠從HTTP 頭信息中獲取session id,而沒必要再依賴於cookie

須要說明的很重要的一點就是,Spring Session的核心項目並不依賴於Spring框架,因此,咱們甚至可以將其應用於不使用Spring框架的項目中web

二 代碼示例

其實,咱們想要在項目中使用Spring Session須要作的工做並很少,只須要作到如下幾步便可:redis

(1)引入相關jar包:

這裏須要引入Spring Session和Spring Data Redis相關的依賴jar包。能夠自行從maven倉庫下載,也能夠參考我使用的jar包:down.51cto.com/data/228183…spring

(2)修改spring-data-redis相關配置:

在項目的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複製代碼

(3)修改web.xml:

在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就整合到項目中了

(4)測試:

運行項目後,能夠發現生成了一個名爲「SESSION」的cookie。接着在咱們登陸以後,能夠經過其cookie值在redis上取得保存的對應的session對象。以下圖所示:


從上圖能夠發現,登陸以後的用戶對象已經保存到redis集羣中了。接着還能夠測試將一樣的項目發佈到其餘端口的tomcat上,在同一瀏覽器上訪問須要登陸認證以後才能訪問的頁面,看看是否可以直接訪問。若是可以直接訪問而不是重定向到登陸頁面,則說明已經達到session共享的效果了

參考:

相關文章
相關標籤/搜索