SpringBoot之——spring boot + redis 实现session共享随笔
来源:互联网 发布:屏蔽淘宝网 百度 编辑:程序博客网 时间:2024/06/03 21:19
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/65081718
今天,我们一起来实现基于spring boot + redis的session共享,好了,我们直接进入主题。
在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring session支持,配置如下:
@Configuration @EnableRedisHttpSession public class RedisSessionConfig { }而@EnableRedisHttpSession这个注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>接下来,则需要在application.properties中配置redis服务器的位置了,在这里,我们就用本机:
spring.redis.host=localhost spring.redis.port=6379这样以来,最简单的spring boot + redis实现session共享就完成了,下面进行下测试。
首先我们开启两个tomcat服务,端口分别为8080和9090,在application.properties中进行设置
server.port=8080接下来定义一个Controller:
@RestController @RequestMapping(value = "/admin/v1") public class QuickRun { @RequestMapping(value = "/first", method = RequestMethod.GET) public Map<String, Object> firstResp (HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); request.getSession().setAttribute("request Url", request.getRequestURL()); map.put("request Url", request.getRequestURL()); return map; } @RequestMapping(value = "/sessions", method = RequestMethod.GET) public Object sessions (HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); map.put("sessionId", request.getSession().getId()); map.put("message", request.getSession().getAttribute("map")); return map; } }启动之后进行访问测试,首先访问8080端口的tomcat,返回
{"request Url":"http://localhost:8080/admin/v1/first"}接着,我们访问8080端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}最后,再访问9090端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}可见,8080与9090两个服务器返回结果一样,实现了session的共享
如果此时再访问9090端口的first的话,首先返回:
{"request Url":"http://localhost:9090/admin/v1/first"}
而两个服务器的sessions都是返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"}
通过spring boot + redis来实现session的共享非常简单,而且用处也极大,配合nginx进行负载均衡,便能实现分布式的应用了。
本次的redis并没有进行主从、读写分离等等配置 3 0
- SpringBoot之——spring boot + redis 实现session共享随笔
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- spring boot 共享session redis实现
- spring boot + redis 实现session共享
- spring boot + redis 实现session共享
- Spring Boot使用Redis实现session共享
- spring boot + redis 实现session共享
- Spring boot + redis 实现session 共享管理
- spring boot之Session实现集群-redis
- spring boot整合redis实现shiro的分布式session共享
- 实战——session共享(Spring Boot + Spring Session + Redis)
- [Session共享]Spring-Redis实现Session共享
- springboot整合redis,实现session共享
- springboot(七)redis 实现session共享
- vue+axios+springboot+redis 实现session 共享
- Android 开发艺术探索笔记 第七章 Android动画深入分析
- Unreal 入门-人物走跑跳相关问题:方向控制和人物旋转不匹配。并且人物动画状态变换不协调。
- JSP 环境配置
- 二叉树
- Unreal 入门-Collision和Physics,碰撞检测
- SpringBoot之——spring boot + redis 实现session共享随笔
- Infoblox Grid Manager简介
- Unreal 入门-Unreal 基础知识。
- 过山车 【裸 匈牙利算法】
- 最常用的15大Eclipse开发快捷键技巧
- CodeForces 131D【图特性+BFS】
- QT--XML(未完)
- Unreal 入门-VR 游戏中需要注意的事情
- tiny-yolo 训练自己的数据集