当前位置: 首页 > news >正文

分布式session解决方案

        在实际项目中,前台代码部署在nginx中,后台服务内嵌了tomcat运行在不同的节点中,常见的架构如下:

        在上述架构中,nginx转发前台请求,第一次登录后,将用户登录信息写入到一台服务session中(假设为A节点),再次请求后,假设nginx转发到了B节点,B服务节点中没有session用户登录信息,此时还需要登录,session在服务集群内不可用,不符合预期,示例代码如下:

package com.gingko.session.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;@RestController
@RequestMapping("/login")
public class LoginController {@GetMapping("/toLogin")public String toLogin(@RequestParam String userName, @RequestParam String password,HttpSession httpSession) {//校验用户名密码...//用户名加入sessionhttpSession.setAttribute("userName",userName);return "登录成功";}@GetMapping("/getLoginUser")public String getLoginUser(HttpSession httpSession) {return "session获取的值是:" + httpSession.getAttribute("userName") ;}
}

        基于上述的代码测试效果如下(后台启动了2个服务,端口分别是8081和8082),从下图中可以看出8081登录后,访问8082时获取不到登录信息。

 

        分布式session解决方案有很多,本文阐述【Session集中存储】的解决方案,即将session集中存储到某个地方,本文演示存储到redis中,架构示意图如下:

        代码架构引入redis和spring session,示例代码如下:

        1、引入 redis和spring session依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gingko</groupId><artifactId>distributed-session</artifactId><version>0.0.1-SNAPSHOT</version><name>distributed-session</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.7.6</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>com.gingko.session.DistributedSessionApplication</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

        2、修改配置,增加对redis和session的支持

# 应用服务 WEB 访问端口
server:port: 8081
spring:redis:host: 192.168.136.130 #redisport: 6379database: 0session:store-type: redis #session 存储方式timeout: 1800 #session 过期时间 30分钟

        3、启动2个应用测试(端口8081和8082),从下图中可以看出8081登录后,redis缓存中增加了session信息,8082也可以获取到登录信息,符合预期。

http://www.lryc.cn/news/575931.html

相关文章:

  • 【Docker】解决:构建(docker build)或重新运行容器时,丢失apt-get update问题
  • 第三十一章 MCO——PA8从主频分频输出
  • 【数据挖掘】关联规则算法学习—Apriori
  • Gitee 持续集成与交付(CI/CD)篇
  • Solidity学习 - 断言失败
  • Java:链接mysql数据库报错:CommunicationsException: Communications link failure
  • MySQL Limit数量不满足时导致查询变慢
  • 深圳中青宝互动网络股份有限公司游戏运维工程师面试题(笔
  • 使用OpenCV进行3D重建:详细指南
  • OpenCV图像添加水印
  • word中如何保存高清图片,并保存为高质量的pdf文件(图像不失真)
  • 51c~嵌入式~PLC~西门子~合集1
  • GO 语言学习 之 语句块
  • 汉中农业服务——激活田野的希望,共绘乡村振兴图
  • Guava Cache 本地项目缓存
  • 昇腾910(NPU)安装paddlepaddle【自用版】
  • LinuxBridge的作用与发展历程:从基础桥接到云原生网络基石
  • 【Linux指南】压缩、网络传输与系统工具
  • 用 pnpm + TurboRepo,构建多项目高效开发体系
  • 人工智能-基础篇-4-人工智能AI、机器学习ML和深度学习DL之间的关系
  • 几种基于Doherty结构的GAN氮化镓功放设计方法介绍
  • Ehcache、Caffeine、Spring Cache、Redis、J2Cache、Memcached 和 Guava Cache 的主要区别
  • 算法-堆排序
  • 飞算科技依托 JavaAI 核心技术,打造企业级智能开发全场景方案
  • AIOps与人工智能的融合:从智能运维到自适应IT生态的革命
  • 【网络】Linux 内核优化实战 - net.ipv4.tcp_rmem 和 net.core.rmem_default 关系
  • MySQL(1)——count()聚合函数
  • V-by-One V1.4协议介绍
  • QT基础知识3——文件操作:QFile类
  • windows11 源码本地部署大模型anythingllm