spring-session + redis 实现集群 session 共享

目前市面上实现session共享的方案有很多,其中比较常用的是使用Tomcat、Jetty等web服务器提供的session共享功能,以此将session内容统一存放在数据库(如mysql)或者缓存(redis)中;另外一种方案不依赖于servlet容器,而是web应用代码层面上的实现,而且操作极其简便,只需要在已有项目基础上加入spring-session框架和redis就可实现session共享。

前一种session共享方案依赖servlet容器,如部署使用的是tomcat时需要修改tomcat的相关配置;后一种方案适用于发布容器不固定,例如使用docker作为发布容器,每次重新部署都会重新创建容器,tomcat的配置也要重新修改,相对比较麻烦。

本文主要讲述第二种session共享方案【maven项目为例】。

  1. 添加项目依赖
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>

2.修改spring配置文件

添加以下配置,代表spring-session将存放在redis中,其中maxInactiveIntervalInSeconds表示session存放在redis的过期时间,默认1800秒。

    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="43200"/>
    </bean>

添加redis配置:

单节点redis>

    <bean 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.password}" />
    </bean>

集群redis>

    <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <constructor-arg name="master" value="${redis.master}" />
        <constructor-arg name="sentinelHostAndPorts">
            <set>
                <value>127.0.0.1:2679</value><!--配置redis哨兵-->
                <value>127.0.0.1:2678</value>
            </set>
        </constructor-arg>
    </bean>

    <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="sentinelConfig" />
        <property name="password" value="${redis.password}"/>
    </bean>

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>
    </filter-mapping>

到此,所有工作均已完成,启动项目即可检验session是否实现共享。 项目中使用的redis server版本必须2.8+ 。

喜欢的朋友可以关注我的公众号,更多精彩分享尽在“Java实战”。

作者:乱舞狂刀

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王韵壹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值