Spring Session + Redis实现分布式Session共享

Spring Session + Redis实现分布式Session共享

通常情况下,Tomcat、Jetty等Servlet容器,会默认将Session保存在内存中。如果是单个服务器实例的应用,将Session保存在服务器内存中是一个非常好的方案。但是这种方案有一个缺点,就是不利于扩展。

目前越来越多的应用采用分布式部署,用于实现高可用性和负载均衡等。那么问题来了,如果将同一个应用部署在多个服务器上通过负载均衡对外提供访问,如何实现Session共享?

实际上实现Session共享的方案很多,其中一种常用的就是使用Tomcat、Jetty等服务器提供的Session共享功能,将Session的内容统一存储在一个数据库(如MySQL)或缓存(如Redis)中。我在以前的一篇博客中有介绍如何配置Jetty的Session存储在MySQL或MongoDB中。

本文主要介绍另一种实现Session共享的方案,不依赖于Servlet容器,而是Web应用代码层面的实现,直接在已有项目基础上加入spring Session框架来实现Session统一存储在Redis中。如果你的Web应用是基于Spring框架开发的,只需要对现有项目进行少量配置,即可将一个单机版的Web应用改为一个分布式应用,由于不基于Servlet容器,所以可以随意将项目移植到其他容器。

采用spring-session-data-redis实现session共享

1.pom文件引入依赖

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

<!-- 另可配置redis jar包,非必须 -->

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

2、在项目中配置文件

1)增加spring-redis.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- session设置 -->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.

RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="3600"></property>
    </bean>
    <!-- redis连接池 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"/>
    <!-- redis连接工厂 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.

JedisConnectionFactory">
        <property name="hostName" value="${redis_hostName}"/>
        <property name="port" value="${redis_port}"/>
        <property name="password" value="${redis_password}"/>
        <property name="timeout" value="${redis_timeout}"/>
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>
</beans>

2)增加redis参数配置文件:redis.properties

#redis服务器ַ
redis_hostName = localhost
#redis服务器端口号
redis_port = 6379
#jedis的主机服务密码
redis_password = 123456
#jedis的获取超时时间
redis_timeout = 20000

3)web.xml文件中增加配置

<!-- 在上下文配置参数中增加spring-redis.xml的引入-->

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-redis.xml</param-value>
</context-param>

<!--增加spring session过滤器,实现将session中的数据保存至redis-->
  <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>

3、启动服务器,在项目中调用request.getSession()进行设值和取值。

如: request.getSession().setAttribute("a", "111111");

request.getSession().getAttribute("a");

a=111111会以key-value的形式保存在redis服务器中。

以保存session的方式,在多个客户端中实现session的共享。

4、登录RedisDesktopManager客户端,可参看redis中保存的session参数


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值