• 初试spring-session


    一、简介

      spring-session提供了用户会话信息管理的API和实现。

      它将取代容器中的HttpSession。在没有容器会话集群方案的情况下,使得支持会话集群微不足道。

      它支持在一个浏览器实例中,管理多用户会话。

      接下来,我们将介绍如何在项目中如何使用spring-session。

    二、集群session的解决方案

      随着应用访问量的增大,单台机器很难支撑,我们就要部署应用集群,对请求进行分流。

      但是,这样就会存在一个问题,集群中的每个应用的session不是共享的,导致访问出现问题。

      1、使用容器中提供的session集群方案。

        例如:tomcat自己提供了session集群方案。在集群规模比较小的情况下,各个节点中的session相互进行备份,还是可以的。

        但是,如果集群规模比较大,成百上千台,他们节点之间的备份将是非常耗资源的,只适合小规模集群。

      2、session统一存储

        既然容器中的复制不是一个好的选择,我们可以将session后台统一存储,例如:存储到数据库或缓存中。

        spring-session为我们提供了各种存储方式的解决方案,mysql,redis,mongo等。这里我们只介绍redis存储,其他方式请参考官方文档。

    三、项目中使用spring-session

      1、在项目的pom.xml中加入spring-session-redis的jar包,在项目的pom.xml文件中加入如下配置:

            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-data-redis</artifactId>
                <version>1.2.2.RELEASE</version>
                <type>pom</type>
            </dependency>    

      2、在项目的spring配置文件中加入如下配置:

      <context:annotation-config/>
      <bean id="cookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
            <property name="cookiePath" value="/"/>
            <property name="useHttpOnlyCookie" value="false"/>
            <!--cookie有效期 单位:秒-->
            <property name="cookieMaxAge" value="1800"/>
            <property name="domainName" value="${your_domain}"/>
            <property name="cookieName" value="${your_cookie_name}"/>
        </bean>
        <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
            <property name="cookieSerializer" ref="cookieSerializer"/>
            <property name="maxInactiveIntervalInSeconds" value="1800"/>
        </bean>
        <bean id="jedisConnFactory"
              class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="hostName" value="192.168.2.233"/>
            <property name="port" value="6379"/>
            <property name="database" value="0"/>
        </bean>

      在这里,容器启动时将生成一个springSessionRepositoryFilter,它实现了Filter接口,它负责将HttpSession替换成SpringSession,并将其存储在redis中。

      spring-session是使用RedisConnectionFactory 连接redis的,所以我们创建JedisConnectionFactory 。使用spring-redis的高级配置请参考spring-data-redis文献。

      maxInactiveIntervalInSeconds是Session在redis中的过期时间,这个时间通常与cookie的有效期配置成一样。

      在cookieSerializer中,可以配置cookie的相关信息,域名,cookie名称,httpOnly等。

      3、在web.xml中配置过滤器,拦截所有请求,配置如下:

     <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>

        在web.xml中配置过滤器,拦截将所有的请求拦截,通过springSessionRepositoryFilter将HttpSession替换成SpringSession。

        这样,所有的Session都存储到redis中,并且从redis读取,实现了Session的集中管理,又使得应用可以搭建集群。

                                                                作者原创,如有不妥之处,还请见谅

     

      

  • 相关阅读:
    第01组 团队Git现场编程实战
    2019 SDN上机第1次作业
    第二次结对编程作业
    团队项目-需求分析报告
    团队项目-选题报告
    2019 SDN阅读作业
    2019 SDN上机第3次作业
    2019 SDN上机第2次作业
    2019 SDN上机第1次作业
    OpenDayLight安装Features
  • 原文地址:https://www.cnblogs.com/boboooo/p/7114998.html
Copyright © 2020-2023  润新知