当前位置:网站首页>Session sharing problem
Session sharing problem
2022-07-22 16:24:00 【Leisurely summer】
Session Sharing and Session Keep or call Session⼀ Sexual nature
1、Session Problem cause analysis
The reason for this problem , Basically, it's because Http Protocols are stateless protocols . The data generated by the client and server in a session will not be preserved , So the first ⼆ The second request server cannot realize that you have been here , Http Why should it be designed as a stateless protocol ? In the early days, it was static pages, and it didn't matter whether there was a state or not , Later, there are more dynamic contents , You need to be in a state , There are two ways to keep Http Technology of state , That's it Cookie and Session. And the above problem of keeping logging in , The analysis is shown in the figure below :
scene :nginx Default polling policy
2、 solve Session A consistent solution
2.1、Nginx Of IP_Hash Strategy ( have access to )
Same as ⼀ A client IP All requests will be routed to the same ⼀ Target servers , Also called session stickiness
advantage :
- Simple configuration , No ⼊ Invading application , No additional code changes are required
shortcoming :
- Server restart Session The loss of
- There is a risk of high single point load
- Single point problem
2.2、Session Copy ( Not recommended )
That is to say , Multiple tomcat By modifying the configuration file , achieve Session Copy between
advantage :
- Do not invade the application
- It is convenient for the horizontal expansion of the server
- It can adapt to various load balancing strategies
- Server restart or downtime will not cause Session The loss of
shortcoming :
- Low performance
- Memory consumption
- Can't store too much data , Otherwise, the more data, the more performance will be affected
- Delay
2.3、Session share ,Session Centralized storage ( recommend )
Session The essence of is caching , that Session Why not hand over the data to professional caching middleware ? such as Redis
advantage :
- It can adapt to various load balancing strategies
- Server restart or downtime will not cause Session The loss of
- Strong expansion ability
- Suitable for large number of clusters
shortcoming :
Intrusion into applications , And are introduced Redis Interactive code
Spring Session Make based on Redis Of Session Sharing applications is very simple
3、Session Share code samples
1、 introduce 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、 To configure redis
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
3、 adding annotations
see SpringBootServletInitializer Note that , Only in building war File and deploy it . If you like to run embedded containers , Then there is no need to do so .
Want to use redis Conduct session cache , You have to add @EnableRedisHttpSession annotation , It is recommended to add this annotation to SpringBoot On the startup class of .
4、 Source code ( understand )
This annotation can create ⼀ A filter makes SpringSession replace HttpSession Play a role , Find the filter !
Observe its parent class , In the parent class Filter
This Filter Namely SpringSession At the core
In the filter HttpServletRequest packing
It's essentially a HtppRequest, Have the same approach , look for getSession
go back to SessionRepositoryFilter Of doFilterInternal Method
Schematic principle ( understand )
边栏推荐
猜你喜欢
Word: insert vector diagram with specified color
【解决】npm ERR! code E401
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
第七讲 管道、环境变量与常用命令
Hybrid混合开发与JSBridge
Lecture 7 pipeline, environment variables and common commands
Mock模拟数据,并发起get,post请求(保姆级教程,一定能成功)
DC-4-靶场实践
Command line program test automation
科普 | 如何创建一个DAO?
随机推荐
SOC key control LED
内存管理面试问题
mysql中not like的简化写法
Jd-h5 development
sftp创建
C#上传文件到共享文件夹
Android面试:2022请收好这份网易Android开发和抖音电商Android工程师的面经
St table (jump table)
SFTP creation
Hello world of SOC
Matlab simulation of BER performance of BCH coding and decoding
Signal FFT, STFT, wavelet transform, envelope analysis, etc
AT5662 [AGC040D] Balance Beam(二分)
EasyCVR平台如何实现无需鉴权即可接入特殊设备?
网络层面试题
傍晚的天空
Summary of product related issues (Part 1)
Vulkan-官方示例解读-子通道
这个好用的办公网优化工具,官宣免费了
mysql通过开启全局日志进行定位排查慢sql