当前位置:网站首页>分布式集群架构场景优化解决方案:Session共享问题
分布式集群架构场景优化解决方案:Session共享问题
2022-07-28 05:21:00 【丿涛哥哥】
分布式集群架构场景优化解决方案:Session共享问题
Session共享问题
Session共享及Session保持或者叫做Session一致性

1、 Session问题原因分析
出现这个问题的原因,从根本上来说是因为Http协议是无状态的协议。客户端和服务端在某次会话中产生的数据不会被保留下来,所以第二次请求服务端无法认识到你曾经来过, Http为什么要设计为无状态协议?早期都是静态页面无所谓有无状态,后来有动态的内容更丰富,就需要有状态,出现了两种用于保持Http状态的技术,那就是Cookie和Session。而出现上述不停让登录的问题,分析如下图:
场景:Nginx默认轮询策略

2、解决Session一致性的方案
1、Nginx的 IP_Hash 策略(可以使用)
同一个客户端IP的请求都会被路由到同一个目标服务器,也叫做会话粘滞
优点:
- 配置简单,不入侵应用,不需要额外修改代码
缺点:
- 服务器重启Session丢失
- 存在单点负载高的风险
- 单点故障问题
2、Session复制(不推荐)
多个tomcat之间通过修改配置文件,达到Session之间的复制

优点:
- 不入侵应用
- 便于服务器水平扩展
- 能适应各种负载均衡策略
- 服务器重启或者宕机不会造成Session丢失
缺点:
- 性能低
- 内存消耗
- 不能存储太多数据,否则数据越多越影响性能
- 延迟性
3、Session共享,Session集中存储(推荐)
Session的本质就是缓存,那Session数据为什么不交给专业的缓存中间件呢?比如Redis

优点:
- 能适应各种负载均衡策略
- 服务器重启或者宕机不会造成Session丢失
- 扩展能力强
- 适合大集群数量使用
缺点:
- 对应用有入侵,引入了和Redis的交互代码
Spring Session使得基于Redis的Session共享应用起来非常的简单
1)引入Jar
<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>
2)配置redis
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
3)添加注解

3、 源码示意


观察其父类,父类中有Filter


这个Filter就是SpringSession最核心的地方

在过滤器中将HttpServletRequest包装

本质就是一个HtppRequest,拥有同样的方法,找getSession



回到SessionRepositoryFilter的doFilterInternal方法




原理示意图

边栏推荐
- MarsNFT :个人如何发行数字藏品?
- 命令注入绕过方式总结
- Books - mob
- Chapter 8 aggregate function
- 预告来袭:【豆冰冰】发.售,现.金.抽.奖等你来拿
- Books - smart investors
- 基于php学生学籍管理系统获取(php毕业设计)
- At the moment of the epidemic, online and offline travelers are trapped. Can the digital collection be released?
- Hit your face ins? Mars digital collection platform explores digital collection light social networking
- regular expression
猜你喜欢

(php毕业设计)基于php水果销售商店管理系统获取

基于php心理健康服务系统获取(php毕业设计)

Mars数*字*藏*品*平*台守卫者计划细节公布

Flex elastic box item properties

On July 7, the national wind 24 solar terms "Xiaoshu" came!! Attachment.. cooperation.. completion.. advance.. report

regular expression

(php毕业设计)基于thinkphp5小区物业管理系统

常见WAF拦截页面总结

浅谈数字藏品与实体如何相互赋能

Acquisition of mental health service system based on PHP (PHP graduation design)
随机推荐
(php毕业设计)基于php甘肃旅游网站管理系统获取
svn incoming内容无法更新下来,且提交报错:svn: E155015: Aborting commit: XXX remains in conflict
MySQL view, stored procedure and stored function
登录时密码错误次数过多,对该用户进行封禁,
基于php学生学籍管理系统获取(php毕业设计)
On July 7, the national wind 24 solar terms "Xiaoshu" came!! Attachment.. cooperation.. completion.. advance.. report
Wechat official account - authorized login
【七】redis缓存与数据库数据一致性
CVE_2017_11882漏洞复现(Metasploit开启NT远程桌面添加账户)
DataX installation and use
撞脸ins? Mars数字藏品平台探索数字藏品轻社交
CMD and NPM basic commands
Related concepts and operations of DOM model
No module named yum
MarsNFT :个人如何发行数字藏品?
使用pyhon封装一个定时发送邮件的工具类
Installation and use of sqoop
1:开启慢查询日志 与 找到慢SQL
Digital collections strengthen reality with emptiness, enabling the development of the real economy
发售预告:7月22日“大暑”发售,【传统国风廿四节气】夏季发售完毕。