重学SpringBoot3-集成Redis(九)之共享Session

embedded/2024/10/15 15:43:39/

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》
期待您的点赞👍收藏⭐评论✍

重学SpringBoot3-集成Redis(九)之共享Session

  • 1. 为什么需要 Session 共享
  • 2. Spring Session 和 Redis 的集成
    • 2.1. 引入依赖
    • 2.2. 配置 Redis 连接
    • 2.3. 注解启用 Redis 作为 Session 存储
    • 2.4. 测试 Session 共享
  • 3. Spring Boot 3 + Redis Session 共享的优势
  • 4. 总结

在分布式系统中,用户的 Session 共享是一个常见的需求。随着应用规模的增长,单一的服务器已无法满足业务需求,系统往往需要部署多台服务器组成集群。然而,集群环境中如何保证用户在不同服务器间访问时的 Session 一致性成为了一个重要问题。

在这种情况下,我们可以借助 Redis 这种分布式存储系统来实现 Session 共享。通过 Redis,我们可以将用户的 Session 数据统一存储在 Redis 中,不论用户访问的是哪一台服务器,都能保证 Session 的一致性。

本篇文章将介绍如何使用 Spring Boot 3Redis 来实现分布式环境下的 Session 共享,确保用户在多个实例之间切换时,Session 数据保持一致。

1. 为什么需要 Session 共享

当应用处于单服务器环境时,用户的 Session 是直接保存在服务器内存中的。每当用户发起请求,服务器会根据 Cookie 中的 Session ID 来读取对应的 Session 数据。但在分布式环境中,应用往往部署了多台服务器,如果 Session 数据只保存在单一服务器上,那么用户请求的服务器不同,Session 数据就可能丢失。

  • 单台服务器:用户的 Session 存储在内存中,用户的请求总是由这台服务器处理,Session 可直接使用。
  • 多台服务器:当用户第一次访问时,Session 可能存储在 A 服务器上,但下一次请求由 B 服务器处理,这时 B 服务器无法访问 A 服务器的内存,导致 Session 数据丢失。

通过 Redis,我们可以将 Session 存储在集中式的缓存系统中,不管用户请求的是哪一台服务器,都能访问同一个 Session 数据。

2. Spring Session 和 Redis 的集成

Spring 提供了 Spring Session 来解决分布式环境下的 Session 管理问题。它支持多种数据存储机制,其中最常用的就是 Redis。通过将 Session 存储在 Redis 中,所有服务器实例都能共享同一份 Session 数据,从而解决分布式环境下的 Session 不一致问题。

2.1. 引入依赖

在 Spring Boot 项目中使用 Redis 实现 Session 共享,首先需要引入相关的依赖。确保在 pom.xml 中包含以下依赖:

        <dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  • spring-session-data-redis:用于将 Spring Session 存储在 Redis 中。
  • spring-boot-starter-data-redis:Spring Boot 连接 Redis 所需的基础依赖。

2.2. 配置 Redis 连接

接下来,我们需要在 application.yml 中配置 Redis 连接信息:

具体配置见配置类,org.springframework.boot.autoconfigure.session.RedisSessionProperties

spring:data:redis:host: localhostport: 6379            # Redis 端口password:             # 如果有密码可以在这里配置lettuce:pool:max-active: 100    # 最大并发连接数max-idle: 50       # 最大空闲连接数min-idle: 10       # 最小空闲连接数session:redis:namespace: "coderjia:session"   # 定义存储在 Redis 中的 session 数据的命名空间flush-mode: on_save             # 每次保存或更新 session 时立即将数据同步到 Redissave-mode: always               # 每次请求结束时都保存 session
  • spring.data.redis:配置 Redis 的主机、端口和连接池参数。
  • spring.session:配置 Session 相关信息。

2.3. 注解启用 Redis 作为 Session 存储

另外一种配置方式是注解方式,启用 Spring Session 的 Redis 支持,只需在启动类或配置类上加上 @EnableRedisHttpSession 注解即可:

package com.coderjia.boot310redis;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@SpringBootApplication
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60, redisNamespace = "CoderJia:session")
public class SpringBoot310RedisApplication {public static void main(String[] args) {SpringApplication.run(SpringBoot310RedisApplication.class, args);}}

@EnableRedisHttpSession 注解会自动配置 Spring Session 使用 Redis 进行 Session 存储和管理,和手动配置冲突!!!

@EnableRedisHttpSession

2.4. 测试 Session 共享

接下来,我们可以通过一个简单的 Controller 来测试 Session 共享是否成功。

package com.coderjia.boot310redis.demos.web;import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author CoderJia* @create 2024/10/7 下午 06:17* @Description**/
@RestController
@RequestMapping("/session")
public class SessionController {@GetMapping("/set")public String setSession(HttpSession session) {session.setAttribute("user", "CoderJia");return "Session set for user: CoderJia";}@GetMapping("/get")public String getSession(HttpSession session) {return "User from session: " + session.getAttribute("user");}
}

使用说明

  1. 访问 /session/set,在 Session 中存储一个名为 user 的属性,值为 CoderJia
  2. 访问 /session/get,获取 Session 中存储的 user 属性,验证数据是否存储成功。

通过部署多个实例后,测试这些接口时,即使请求被不同的实例处理,Session 数据也能共享,确保用户数据的一致性。

Session中存储属性

Session中存储属性

Session中获取属性

Session中获取属性

Session存储结构

Session存储结构

设置缓存时间

注解上可以设置缓存的时间:

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)

设置<a class=缓存时间之后" />

3. Spring Boot 3 + Redis Session 共享的优势

通过 Redis 和 Spring Session 的结合,我们可以轻松解决分布式系统中的 Session 共享问题。相比传统的基于服务器内存的 Session 管理,Redis 作为 Session 存储具有以下优势:

  1. 横向扩展:由于 Redis 是一个分布式存储系统,它能够支持多实例共享 Session 数据,轻松解决集群环境下的 Session 一致性问题。
  2. 高性能:Redis 作为内存数据库,具有极高的读写速度,能够迅速处理大量的 Session 读写操作,确保用户体验。
  3. 持久化:Redis 支持数据持久化,即使 Redis 实例重启也能恢复 Session 数据。
  4. 弹性伸缩:通过 Redis,我们可以轻松应对应用的扩展需求,保证系统的高可用性和稳定性。

4. 总结

通过本文的介绍,我们了解了如何通过 Spring Boot 3Redis 实现分布式环境下的 Session 共享。Redis 作为 Session 的集中式存储,可以确保用户在多个服务器实例之间切换时,Session 数据保持一致,解决了分布式系统中的 Session 管理问题。

在实际项目中,Session 共享的场景非常常见,特别是在需要保证用户会话一致性和系统高可用的分布式架构中,Redis 是一个非常高效且可靠的解决方案。

下一篇文章中,我们将继续探索更多 Redis 和 Spring Boot 结合的实际应用场景,敬请期待!


http://www.ppmy.cn/embedded/127934.html

相关文章

Python脚本实现发送QQ邮件

需要发件人邮箱地址、授权码和收件人邮箱地址 1、登录QQ邮箱后台&#xff0c;点击右上角设置&#xff0c;下拉找到第三方服务&#xff0c;开启SMTP服务&#xff0c;复制生成的授权码 2、新建一个python文件&#xff0c;输入以下源码&#xff0c;更替参数后运行即可 import smt…

<Rust>iced库(0.13.1)学习之部件(三十二):使用markdown部件来编辑md文档

前言 本专栏是学习Rust的GUI库iced的合集,将介绍iced涉及的各个小部件分别介绍,最后会汇总为一个总的程序。 iced是RustGUI中比较强大的一个,目前处于发展中(即版本可能会改变),本专栏基于版本0.12.1. 注:新版本已更新为0.13 概述 这是本专栏的第三十二篇,主要介绍一…

WebGl 如何给页面绑定点击事件

在WebGL中给页面绑定点击事件&#xff0c;可以通过为WebGL的绘图上下文所在的<canvas>元素添加事件监听器来实现点击事件的处理。 1. 画布添加点击事件 const ctx document.getElementById(canvas) const gl ctx.getContext(webgl)ctx.onclick function (e) {// 给ca…

如何获取网页内嵌入的视频?

如何获取网页内嵌入的视频&#xff1f; 有时插件无法识别的视频资源&#xff0c;可以通过手动使用浏览器的开发者工具来抓取。你可以按照以下步骤操作&#xff1a; 步骤&#xff1a; 打开网页并按 F12&#xff1a;在视频页面按下 F12 或右键点击网页并选择“检查”或“Inspe…

Spring Boot 进阶-Spring Boot如何整合AOP实现自定义注解

通过之前的文章,我们知道在Spring Boot中使用了大量的注解,而对于注解大家应该不陌生。 Java注解是在JDK1.5的时候引入的新特性,它提供了一种类似注释的机制,用来将任何的信息或者元数据与类、方法,或者是成员变量来进行关联。在注解中附带了一些信息,这些信息可以在编译…

QT文件操作【记事本】

mainwindow.h核心函数 QFileDialog::getOpenFileName()QFileDialog::getSaveFileName() #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include<QFileDialog> #include<QMessageBox> #include<QDebug> #include<QFile> #…

PHP-FPM和FastCGI

文章目录 前言一. FastCGI1.定义2.工作方式3.协议4.架构5.工作原理&#xff08;请求生命周期&#xff09; 二. PHP-FPM1.定义&#xff1a;2.特性3.进程管理模式4.工作流程 三.关系与应用四.配置示例五.性能优化六.配置选项七.常见问题及解决方案 前言 PHP-FPM 是基于 FastCGI …

【计算机网络 - 基础问题】每日 3 题(四十)

✍个人博客&#xff1a;https://blog.csdn.net/Newin2020?typeblog &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞…