当前位置:   article > 正文

Spring Boot集成Spring Session快速入门Demo

Spring Boot集成Spring Session快速入门Demo

1.什么事Spring Session?

HTTP协议本身是无状态的,为了保存会话信息,浏览器Cookie通过SessionID标识会话请求,服务器以SessionID为key来存储会话信息。在单实例应用中,可以考虑应用进程自身存储,随着应用体量的增长,需要横向扩容,多实例session共享问题随之而来。

  • Spring Session就是为了解决多进程session共享的问题

  • Spring Session支持存储在Hazelcast 、Redis、MongoDB、关系型数据库,本文主要讨论session存储在Redis。

2.redis换搭建

docker run -itd --name redis-6379 -p 6379:6379 redis --requirepass 123456

3.代码工程

实验目的:通过redis实现session共享

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>session</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-data-redis</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.session</groupId>
  36. <artifactId>spring-session-data-redis</artifactId>
  37. </dependency>
  38. </dependencies>
  39. </project>

application.properties

  1. ## Session store-type
  2. spring.session.store-type=redis
  3. ## Session timeout default
  4. server.servlet.session.timeout=600
  5. ## Session store to Redis key prefix
  6. spring.session.redis.namespace=test:spring:session
  7. ## Redis config
  8. spring.redis.host=127.0.0.1
  9. spring.redis.password=123456
  10. spring.redis.port=6379
  11. server.port=8088

controller

  1. package com.et.session.java.demo.et.demo.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpSession;
  11. @RestController
  12. public class HelloWorldController {
  13. @RequestMapping("/hello")
  14. public Map<String, Object> showHelloWorld(){
  15. Map<String, Object> map = new HashMap<>();
  16. map.put("msg", "HelloWorld");
  17. return map;
  18. }
  19. @RequestMapping("/get/{name}")
  20. public String getSesseion(HttpServletRequest request, @PathVariable("name") String name){
  21. HttpSession session = request.getSession();
  22. String value = (String)session.getAttribute(name);
  23. return "sessionId:"+session.getId()+" value:"+value;
  24. }
  25. @RequestMapping("/add/{name}/{value}")
  26. public String addSession(HttpServletRequest request,@PathVariable("name") String name,@PathVariable("value") String value){
  27. HttpSession session = request.getSession();
  28. session.setAttribute(name,value);
  29. return "sessionId:"+session.getId()+" name:"+name;
  30. }
  31. }

DemoApplication.java

  1. package com.et.session.java.demo.et.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo

4.测试

启动Spring Boot应用

创建Session

http://127.0.0.1:8088/add/aaa/11198ce4092872478e6d77ea189ca779dfe.png

获取Session

http://127.0.0.1:8088/get/aaa

5.参考

  • https://www.baeldung.com/spring-session

  • https://www.springcloud.cc/spring-session.html#modules

  • http://www.liuhaihua.cn/archives/710469.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/519649
推荐阅读
相关标签
  

闽ICP备14008679号