1.What’s Spring Session?
HTTP The protocol itself is stateless, for To save session information, the browser Cookie pass SessionID Identifies the session request, the server starts with SessionID for key to store session information.In single-instance applications, you can consider the application process’s own storage. As the application volume grows, horizontal expansion is required. Multi-instance session Sharing issues arise.
- Spring Session Just to solve the problem of multi-process session Shared questions
- Spring Session Supports storage in Hazelcast 、Redis、MongoDB、 Relational database, this article mainly discusses session save at Redis。
2.redis environment setup
docker run -itd --name redis-6379 -p 6379:6379 redis --requirepass 123456
3.code engineering
Experiment purpose: throught redis to accomplish session shared
pom.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
## 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
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
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);
}
}
The above are just some key codes. For all codes, please see the code repository below.
code repository
4.test
start up Spring Boot application
create Session
http://127.0.0.1:8088/add/aaa/111