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换搭建

css 复制代码
docker run -itd --name redis-6379 -p 6379:6379 redis --requirepass 123456

3.代码工程

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

pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>


    <artifactId>session</artifactId>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <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>


    </dependencies>
</project>

application.properties

shell 复制代码
## Session store-type
spring.session.store-type=redis


## Session timeout default
server.servlet.session.timeout=600
## Session store to Redis key prefix
spring.session.redis.namespace=test:spring:session


## Redis config
spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.port=6379
server.port=8088

controller

kotlin 复制代码
package com.et.session.java.demo.et.demo.controller;


import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld");
        return map;
    }
    @RequestMapping("/get/{name}")
    public String getSesseion(HttpServletRequest request, @PathVariable("name") String name){
        HttpSession session = request.getSession();
        String value = (String)session.getAttribute(name);
        return "sessionId:"+session.getId()+" value:"+value;
    }
    @RequestMapping("/add/{name}/{value}")
    public String addSession(HttpServletRequest request,@PathVariable("name") String name,@PathVariable("value") String value){
        HttpSession session = request.getSession();
        session.setAttribute(name,value);
        return "sessionId:"+session.getId()+" name:"+name;
    }


}

DemoApplication.java

java 复制代码
package com.et.session.java.demo.et.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {


   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

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

代码仓库

4.测试

启动Spring Boot应用

创建Session

http://127.0.0.1:8088/add/aaa/111![98ce4092872478e6d77ea189ca779dfe.png](https://file.jishuzhan.net/article/1784123273508818946/80093b88847d829a8d272d157ce2dd2a.webp)

获取Session

http://127.0.0.1:8088/get/aaa

5.参考

相关推荐
想摆烂的不会研究的研究生6 小时前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
毕设源码-郭学长6 小时前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
梨落秋霜6 小时前
Python入门篇【文件处理】
android·java·python
N***H4867 小时前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
Java 码农7 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
哈库纳玛塔塔7 小时前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
追逐时光者7 小时前
精选 10 款 .NET 开源免费、功能强大的 Windows 效率软件
后端·.net
追逐时光者7 小时前
一款开源、免费的 WPF 自定义控件集
后端·.net
S***q3778 小时前
Spring Boot管理用户数据
java·spring boot·后端
BD_Marathon8 小时前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot