springSession+redis在项目中的使用

   1、前言,session一直都是我们做集群时需要解决的一个难题,过去我们可以从serlvet容器上解决,比如开源servlet容器-tomcat提供的tomcat-redis-session-manager、memcached-session-manager,现在我们可以通过springSession实现session在集群中的共享。

    Spring Session提供了一个用于管理用户会话信息的API和实现,同时也支持集群会话而不受限于特定于应用程序容器的解决方案。

   2、支持功能 

     1)、springSession支持将session存储到第三方存储容器,如redis、mysql中,框架提供了redis、jdbc等多种存储session的容器的方式。 

     2)、多个浏览器会话 - Spring Session支持在单个浏览器实例(即类似于Google的多个经过身份验证的帐户)中管理多个用户的会话,同一个浏览器同一个网站,支持多个session共享。 

      3)、Restful API,Spring Session允许在头文件中提供会话ID以使用RESTful API

      4)、WebSocket - 提供HttpSession在接收WebSocket消息时保持活动的能力。   

    3、项目集成springSession,以springboot为例,使用redis存储session,更多请参考:https://docs.spring.io/spring-session/docs/1.3.1.RELEASE/reference/html5/

    1)、添加pom         

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

     2)、配置springsession   

@Configuration
//session有效时长4小时
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 14400)
public class SpringSessionConfig {

}

    3)、springSession的sessionID传递方式接口 

           1.cookie方式 :CookieHttpSessionStrategy

           2.http header 方式:HeaderHttpSessionStrategy

           默认的是以cookie方式实现

    4)、基于默认的sessionId传递方式,添加cookie配置    

#cookie名称
server.session.cookie.name=MY_PROJECT_SESSION
#cookie会话的域
server.session.cookie.domain=regulus.com
#会话cookie的路径,基于根路径
server.session.cookie.path=/

   更多配置   

server.session.cookie.comment = #注释会话cookie。
server.session.cookie.domain = #会话cookie的域。
server.session.cookie.http-only =#“HttpOnly”标志为会话cookie。
server.session.cookie.max-age = #会话cookie的最大年龄(以秒为单位)。
server.session.cookie.name = #会话cookie名称。
server.session.cookie.path = #会话cookie的路径。
server.session.cookie.secure = #“Secure”标志为会话cookie。
server.session.persistent = false #在重新启动之间持续会话数据。
server.session.store-dir = #用于存储会话数据的目录。
server.session.timeout = #会话超时(秒)。
server.session.tracking-modes =#会话跟踪模式(以下一个或多个:“cookie”,“url”,“ssl”)。

   5)、springSession基于cookie的配置    

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 14400)
public class SpringSessionConfig {

    @Bean
    public CookieSerializer cookieSerializer(ServerProperties serverProperties) {
        Cookie cookie = serverProperties.getSession().getCookie();
        DefaultCookieSerializer bean = new DefaultCookieSerializer();
        if (cookie.getMaxAge() != null) {
            bean.setCookieMaxAge(cookie.getMaxAge());
        }
        if (cookie.getName() != null) {
            bean.setCookieName(cookie.getName());
        }
        if (cookie.getPath() != null) {
            bean.setCookiePath(cookie.getPath());
        }
        if (cookie.getDomain() != null) {
            bean.setDomainName(cookie.getDomain());
        }
        if (cookie.getHttpOnly() != null) {
            bean.setUseHttpOnlyCookie(cookie.getHttpOnly());
        }
        if (cookie.getSecure() != null) {
            bean.setUseSecureCookie(cookie.getSecure());
        }
        return bean;
    }

}

   4、基于以上配置,就可以在项目中,使用springSession了,用法和session的用法一致

   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当将会话管理从 Tomcat 切换到 Spring Session + Redis 时,有几个注意事项: 1. 配置 Redis:确保已正确配置和启动 Redis 服务器。你需要提供 Redis 的主机名、端口号和认证信息(如果有的话)。 2. 添加 Spring SessionRedis 的依赖:在项目的构建文件,添加 Spring SessionRedis 的依赖。例如,在 Maven ,你可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> ``` 3. 配置 Spring Session:在 Spring Boot 的配置文件,添加以下配置来启用 Spring Session 和指定 Redis 的连接信息: ```properties # 启用 Spring Session spring.session.store-type=redis # Redis 连接信息 spring.redis.host=<redis_host> spring.redis.port=<redis_port> spring.redis.password=<redis_password> ``` 4. 在应用使用会话:在应用使用 Spring Session 提供的 API 来管理会话。你可以使用 `@EnableRedisHttpSession` 注解启用 Spring Session,并使用 `@Autowired` 注入 `RedisOperationsSessionRepository` 或 `SessionRepositoryFilter`。 5. 确保会话数据正确存储和检索:通过 Spring Session + Redis,会话数据将存储在 Redis 。确保会话数据能够正确地存储和检索,以及与应用其他部分的交互正常。 6. 集群环境下的同步问题:如果你的应用在多个实例之间共享会话数据,并且使用 Redis 进行存储,需要注意在集群环境下的会话同步问题。你可以使用 Spring Session 提供的其他解决方案,如使用 Redis Pub/Sub 或 Redis Sentinel。 7. 监控和调优:在切换到 Spring Session + Redis 后,你可能需要重新评估和监控应用的性能和资源消耗情况,以确保它能够满足预期的性能需求。 以上是将会话管理从 Tomcat 切换到 Spring Session + Redis 时需要注意的一些事项。根据你的具体情况和需求,可能还需要调整其他相关配置和代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值