java session 框架_分布式下session一致性架构全解

什么是Session?

what

Session在网络应用中表示"会话控制",用于存储特定用户会话所需的属性及配置信息;Session又表示一个特定的时间间隔,指从注册登陆进入系统到注册退出系统之间所经过的时间。

why

http是无状态的协议

在动态web应用中,往往需要知道前面的操作和后面的操作是不是同一用户。也就是业务需要有关联性的。

how

结合Session和游览器cookie,将服务端Session保存到游览器cookie中来保持http会话状态。

什么是Session一致性问题?

只要用户不重启浏览器,每次http短连接请求,理论上服务端都能定位到session,保持会话。

48d1346f756ba3f0b83632370003dd8a.png

Session一致性问题解决方案

方案一: 基于nginx的ip_hash策略来做负载均衡

原理:根据ip做hash计算,同一个ip的请求始终会定位到同一台tomcat

d8570f243e44d6bcb6ddc6455c65d833.png

nginx配置

upstream sessionTest {

ip_hash;

server 192.168.2.177:801;

server 192.168.2.177:802;

}

niginx 命令

start nginx

nginx-s reload

优点:配置简单,应用没有兼容性的,均匀的 ,便于水平扩展

缺点:存在单点故障,会导致部分服务不可用

方案二:服务器Session复制

原理:Tomcat服务器创建Session后,会通过组播方式把Session发生到组播地址中的其他服务器上

2f27ec04d2909e4c638f53e089354efe.png

tomcat1 server.xml

//8异步发送

tomcat2 server.xml

//8异步发送

web.xml

优点:应用侵入性低,能适应各种负载均衡策略,tomcat宕机不存在session丢失

缺点:session同步延迟,受限于内存资源,大量占用内存和带宽,不利于水平扩展

方案三: Session集中统一管理

原理:Session不由tomcat管理,而是统一放到一个地方集中式管理, 读取和写入Session都依赖第三方软件。例如redis,mongodb,mysql等。

cf90fe9dae3314d44d7a274456887b53.png

添加依赖

spring-session

spring-session-data-redis

redis

jedis

web.xml 配置Filter

springSessionRepositoryFilter

org.springframework.web.filter.DelegatingFilterProxy

springSessionRepositoryFilter

/*

spring-applicationContext.xml:

class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfig

config.properties

reids.hostname=192.168.2.227

redis.port=6379

redis.timeout=3000

redis.pool.maxTotal=100

redis.pool.maxIdle=10

redis命令

修改redis.windows.conf

bind 0.0.0.0

redis-server.exe redis.windows.conf

redis-cli.exe -h 192.168.2.227

keys *

type

hgetall key

注意:session中的对象必须可序列化。

优点:扩展能力强,session不易丢失,适应各种负载均衡策略,

缺点:依赖第三方软件,有应用侵入性;

无法实现跨域名共享session , 只能在单台服务器上共享session , 因为是依赖cookie做的 , cookie 无法跨域;一般是用于多台服务器负载均衡时共享Session的,都是同一个域名,你想要的跨域的登录,可能需要SSO单点登录。

应用场景:大型分布式环境首选。

spring-session原理

spring-session本质:覆盖原有tomcat 容器的HttpSession实现。

7612ea535dca5852a4d51ad41730eb83.png

org.springframework.web.filter.DelegatingFilterProxy 在请求之前 覆盖tomcat 的httpsession实现,具步骤:

1、SessionRepositoryFilter和JedisConnectionFactory注册过程

27718d4ad2614be893adaec50c858f0d.png

2、SessionRepositoryFilter添加到FilterChain

fe6454e0b999e563017350c1842f8f9b.png

3、SessionRepositoryFilter拦截过程

ff952d5e98a25e4dc9bb1921d1e6853a.png

4、SessionRepository保存session数据

f99df9744e5629f624f43afc34a25f2f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值