zoukankan      html  css  js  c++  java
  • 使用SpringSession和Redis解决分布式Session共享问题

    SpringSession优势

    • 遵循servlet规范,同样方式获取session,对应用代码无侵入且对于developers透明化

    关键点在于做到透明和兼容

    • 接口适配:仍然使用HttpServletRequest获取session,获取到的session仍然是HttpSession类型——适配器模式
    • 类型包装增强:Session不能存储在web容器内,要外化存储——装饰模式

    基本环境需求

    进行使用Spring Session的话,首先的是已经安装好的有一个 Redis服务器!

    添加项目依赖(最基本的依赖使用)

    <!--Spring Session-->
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
        <version>1.3.0.RELEASE</version>
        <type>pom</type>
    </dependency>

    (3)添加Spring配置文件

    添加了必要的依赖之后,我们需要创建相应的Spring配置。Spring配置是要创建一个Servlet过滤器,它用Spring Session支持的HttpSession实现来替换容器本身HttpSession实现。这一步也是Spring Session的核心。

    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <property name="hostName" value="127.0.0.1"/>
      <property name="port" value="6379"/>
      <property name="password" value=""/>
    </bean>

    在web.xml中添加DelegatingFilterProxy(一定要放在自定义filter之前,不然会出现自定义filter中无法获取到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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    DelegatingFilterProxy将通过springSessionRepositoryFilter的名称查找Bean并将其转换为过滤器。对于调用DelegatingFilterProxy的每个请求,也将调用springSessionRepositoryFilter。

    使用工具查看Redis内容:

    对于分布式环境Session跨域共享的问题,不管是使用开源的框架还是使用自己开发的框架,都需要明白的一个问题是:在Tomcat容器中创建Session是一个很耗费内存的事情。因此,我们在自己写类似框架的时候,我们一定要注意的是,并不是Tomcat为我们创建好了Session之后,我们首先获取Session然后再上传到Redis等进行存储,而是直接有我们自己创建Session,这一点是至关重要的!

    关于Error creating bean with name ‘enableRedisKeyspaceNotificationsInitializer’错误的处理:

    添加如下配置让Spring Session不再执行config命令

    <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>

    如果不添加的话,会报如下错误:

    Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]:
    Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to configure Redis to keyspace notifications.
    See http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent
    Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command config

  • 相关阅读:
    装箱和拆箱
    Foundation--NSDictionary+NSMutableDictionary
    Foundation--NSArray+NSMutableArray
    Foundation--NSString+NSMutableString
    类的三大特性
    python之双引号和三引号
    js原型属性之二 实例.hasOwnProperty(“属性”)
    js构造函数
    git使用两人提交,冲突的解决
    使用mybatis-generator-core-1.3.2生成dao,mapping跟model等
  • 原文地址:https://www.cnblogs.com/yanglei-xyz/p/10813332.html
Copyright © 2011-2022 走看看