在分布式系统中,为了提升系统性能,通常会对单体项目进行拆分,分解成多个基于功能的微服务,如果有条件,可能还会对单个微服务进行水平扩展,保证服务高可用。
那么问题来了,如果使用传统管理会话的方式,我们会遇到什么样的问题?
这里拿下单举例,用户小明在天猫上相中了一个的娃,娃觉得不错,果断购买,选尺寸,挑身高,然后确认选择,赶紧提交订单,然后就跳转到了登录页面!小明表示很郁闷,大写的问号? ? ?
-
<李>小明进入娃娃页面,此时请求通过代理服务发送到业务系统一。李>
<李>小明选尺寸,挑身高,此操作并没有对后端服务发送请求。李>
<李>小明提交订单,此时请求通过代理服务发送到业务系统二,然鹅,二系统此时并没有查询到小明的登录信息,就被无情的跳转到登录页了。李>
HttpSession默认使用内存来管理,通常服务端把用户信息存储到各自的Jvm内存中。所以小明下单的时候找不到登录信息,那么我么何不把用户信息集中存储! ?
为了测试效果,这里我们搭建一个演示案例,项目涉及SpringBoot,春季,复述,nginx等相关组件。
pom.xml引入依赖:
& lt; dependency> & lt; groupId> org.springframework.session & lt; artifactId> spring-session-data-redis & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-data-redis & lt;/dependency>
配置复述,参数,软件自行安装:
# #复述, #会话存储类型 spring.session.store-type=复述, spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=123456 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=1 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.timeout=3000
简单的用户登录实现,省略部分代码:
@RequestMapping (value=" https://www.yisu.com/zixun/login ",方法=RequestMethod.POST) 公共结果登录(用户名的字符串,字符串密码,HttpServletRequest请求,HttpServletResponse响应){抛出异常=userService.getUser SysUser用户(用户名); 如果(用户==null) { 返回Result.error(“用户不存在”); 其他}{ 如果(user.getPassword () .equals(密码)){ request.getSession()。setAttribute(“用户”,用户); 返回Result.ok (); 其他}{ 返回Result.error(“密码错误”); } } }
配置代理实现,基于Nginx:
服务器{ 听80; server_name blog.52itstyle.vip; 位置/{ proxy_pass http://192.168.1.2:8080; } 位置/车{ proxy_pass http://192.168.1.3:8080 request_uri美元; } 位置/订单{ proxy_pass http://192.168.1.4:8080 request_uri美元; } }
配置成功后登录系统,在复述中查询用户信息:
127.0.0.1:6379>键* 1)“春:会话:到期:1562577660000” 2)“春天:会议:会议:1076 c2bd - 95 - b1 - 4 - f23 abd4 ab3780e32f6f” 3)“春天:会议:会议:过期:1076 c2bd - 95 - b1 - 4 - f23 abd4 ab3780e32f6f”
这样,小明就可以开心的买娃娃了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。