springboot +springsession+redis 做session共享
2018-09-24 13:03
561 查看
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接
springboot版本 2.0.5.RELEASE
maven:
<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>
启动类:
@SpringBootApplication @ComponentScan(value = "com.qjs.*") @EnableRedisHttpSession public class CollectionApplication { public static void main(String[] args) { SpringApplication.run(CollectionApplication.class, args); } }
controller:
@RestController public class SessionShare { //模拟登陆 @ResponseBody @RequestMapping(value = "/Login",method = RequestMethod.GET) public String Login(HttpServletRequest request) { request.getSession().setAttribute("username","ethan"); return request.getSession().getId(); } @ResponseBody @RequestMapping(value = "/getSession",method = RequestMethod.GET) public String getSession(HttpServletRequest request) { return request.getSession().getAttribute("username")+"@@"+ request.getSession().getId(); } }
此时最简版项目搭建完毕,拷贝项目,分别启动这两个项目,端口分别为8080,9090
先模拟登陆 http://localhost:8080/Login 返回sessionId:eb30fc4b-fff3-408e-bbf9-20ae9e9a9c7c 再访问:http://localhost:8080/getSession 返回:ethan@@eb30fc4b-fff3-408e-bbf9-20ae9e9a9c7c
访问9090端口的项目
http://localhost:9090/getSession 返回:ethan@@eb30fc4b-fff3-408e-bbf9-20ae9e9a9c7c
查看redis
不要用keys*(阻塞),用scan命令(非阻塞)
xxxx:8001> scan 0 match *session* count 10 1) "0" 2) 1) "spring:session:sessions:expires:eb30fc4b-fff3-408e-bbf9-20ae9e9a9c7c" xxxx:8001>
完成的session共享
阅读更多相关文章推荐
- 使用springboot+redis实现session共享
- 详解基于Spring Boot/Spring Session/Redis的分布式Session共享解决方案
- spring boot session Redis共享方案和一些疑惑的解决
- 实战——session共享(Spring Boot + Spring Session + Redis)
- SpringBoot,Security4, redis共享session,分布式SESSION并发控制,同账号只能登录一次
- Spring boot + redis 实现session 共享管理
- SpringBoot+redis 实现shiro集群,共享session。亲测可用
- spring boot + redis 实现session共享
- SpringBoot+Redis+Nginx实现负载均衡以及Session缓存共享
- springboot项目集成redis共享session
- Spring boot + shiro + redis 实现session共享(伪单点登录)
- spring boot + redis 实现session共享
- springboot整合redis,实现session共享
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- springboot(七)redis 实现session共享
- springboot项目Nginx+Tomcat实现负载均衡结合Redis实现session共享问题
- 使用springboot+redis实现session共享
- spring boot + redis 实现session共享