文章目录
Cookie Sessionid Session 战略图
Spring session 简述
Springboot 集成SpringSession
第一步 创建一个如下所示的项目
第二步导入依赖 (改pom)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第三步 写application.yml
spring.redis.host=localhost
spring.redis.port=6379
server.port=9400
更详细的配置说明
第四步写业务类
package com.example.demo2.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class Test {
@GetMapping("/set")
public String set(HttpSession session){
session.setAttribute("mykey","我的Session数据");
return "Session 设置成功了";
}
@GetMapping("/get")
public String get(HttpSession session){
String data= (String)session.getAttribute("mykey");
return data;
}
}
第五步 测试本机测试 单项目测试
第六步打jar包 在jar中不同的端口代表不同的项目
jar包中内嵌有tomcat服务器,可以作为一个应用单独的启动
第七步 启动架包 开始 p2p测试
别忘开启Redis服务器