Redis 分布式Session
添加依赖
1 2 3 4 5
| <!-- Spring Redis Session --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| spring: redis: database: 8 host: 114.116.69.230 port: 16379 password: timeout: 120000 session: store-type: redis timeout: 60 redis: flush-mode: immediate namespace: spring-session
|
编写代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
@RestController @RequestMapping(value = "/redis") public class SessionController {
@GetMapping(value = "/first") public Map<String, Object> firstResp(HttpSession session, String name) { Map<String, Object> map = new HashMap<>(1); TestVO vo = new TestVO(); vo.setName(name); session.setAttribute("account", vo); map.put("sessionId", session.getId()); return map; }
@GetMapping(value = "/sessions") public Object sessions(HttpSession session) { Map<String, Object> map = new HashMap<>(2); map.put("sessionId", session.getId()); map.put("message", session.getAttribute("account")); return map; } }
|
拿Postman测试,启动两个服务,端口分别为8080,8081。在8080添加session,在8081获取session