SpringBoot2.x integrated spring session redis achieve shared session

Shared use Redis achieve Session

1 What is Session

Since the HTTP protocol is stateless protocol, so when the end of the service requires the recording state of the user, requires some mechanism to identify a specific user. Session recording is another mechanism of customer status, except Cookie is stored in the client browser, and Session saved on the server. The client browser access to the server, the server to the client record information in some form on the server, this is the Session. Just look for the Session of the client when the client browser access again from the state on it.

2 Why do you need synchronization session?

When the user when larger than a tomcat may not be able to handle more requests, more than the capacity of a single tomcat, waiting for the user may occur, leading to serious downtime tomcat.
Here Insert Picture Description

This time we may use more backend tomcat to process the request, dispatch requests, request to have multiple different tomcat share the processing
time may login using a tomca1, when orders may be used tomcat2 so on and so.

If there is no synchronization session sharing, may be signed in the next tomcat1 request is dispatched to the tomcat2, this time users will need to log in again.

In practice, we recommend using an external cache device to share Session, hang up to avoid a single node and affect the service, the use of external cache Session, we will share the data into the external cache container, the service itself will become stateless the service, free to increase or decrease based on equipment size of the load flow.

Current mainstream Distributed Session Management there are two options.

1 Session Copy

Part of the Web server can support Session replication, such as Tomcat. Users can modify the configuration file for the Web server, allowing the Web server Session copy, keeping each server node Session data can reach agreement.

Realization of this program depends on the Web server, Web server needs to have Session replication. When a larger number of Web applications Session, each server node needs to have a portion of memory used to store Session, it will take up a lot of memory resources. While a large number of the Session object replication transmitted over the network, not only take up network resources, but also because replication synchronization delays, resulting in run error.

In the micro-service architecture, service often requires N side to jointly support services do not recommend using this program.

This session management, refer to this article: Portal

2 Session centralized storage

Used on a separate server or cache server cluster technology, such as Session Redis data store, manage all Session, all of the Web server accesses the corresponding Session from the storage medium realize Session sharing. After the Session information is stripped out of the application, in fact, it reached a stateless service, thus to facilitate expansion in the level of business development speed.

Spring Session

Spring Session provides a Servlet HttpSession creation and management of the program. Spring Session provides a cluster Session (Clustered Sessions) function defaults to external Redis to store Session data, in order to solve shared problems Session.

Spring Session brings innovation for enterprise Java applications Session management, making it easier to achieve the following functions:

API and implementation for managing user sessions; HttpSession, allows application container (i.e. Tomcat) Alternatively HttpSession neutral manner;
the saved state Session unloading storing Session to a particular external, as in Redis or Apache Geode, how between the client and the server control Session ID; they can be independent of the application server cluster to provide high quality manner; supports the use of multiple Session on each browser, which can easily build a richer end-user experience exchange, so can be easily prepared Restful API, because it can be acquired from the HTTP header information SessionID, without having to rely on Cookie; ebSocket when the user sends a request, it is possible to maintain HttpSession active. It is important that, Spring Session of the core project does not depend on the Spring Framework, therefore, we are not even able to apply it using the Spring Framework project needs note.

3 Project Preparation

3.1 pom file to add dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

3.2 configuration file to add a link redis

spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=5000

server.port=8081

3.3 Add redis session configuration

package com.crecgec.cas.frontend.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration  
@EnableRedisHttpSession
public class RedisSessionConfig {  
}

3.4 acquire sessionid Controller

package com.crecgec.cas.frontend.controller;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpSession;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class SessionController {
 
    @RequestMapping(value = "/setsession")
    public Object setSession(@RequestParam(required=false) String value, HttpSession session) {
        session.setAttribute("value", value);
        return session.getId();
    }
    
    @RequestMapping(value = "/getsession")
    public Object getSession(HttpSession session) {
        Object value = session.getAttribute("value");
        Map<String, Object> map = new HashMap<>();
        map.put("sessionId", session.getId());
        map.put("value", value);
        return map;
    }
 
}

3.5 Test

Visit http: // localhost: 8080 / setsession
SpringBoot2.x integrated spring session redis achieve shared session
start 8081 HTTP: // localhost: 8081 / getSession
SpringBoot2.x integrated spring session redis achieve shared session
Original Source: Original Source

Published 115 original articles · won praise 90 · views 180 000 +

Guess you like

Origin blog.csdn.net/zhaokejin521/article/details/104243739
Recommended