Springboot distributed session sharing


1、 In distributed development, the traditional method of nginx load balancing is to manually save the session to redis

1. common practices redis

The client initiates a request. After the request reaches nginx, nginx forwards it to tomcata, and then tomcata saves a copy of data (redis) to the session. Next time, another request is distributed to tomcatb, and the session is read from redis. This method can realize session sharing.

2.spring session

Use springsession. Springsession is a proxy filter in spring. It intercepts all sessions and automatically synchronizes data to sessions, or automatically reads sessions from redis without manual access.

2、 Usage

1. in pom XML add dependency

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


The spring boot version I use here is 2.1.4. If I use the latest version of spring boot2.1.5, in addition to the above dependencies, I need to add additional spring security dependencies (other operations are not affected, just one more dependency, and of course some default authentication processes of spring security)

2. configure redis

spring.redis.host= spring.redis.port=6379 spring.redis.password=123 spring.redis.database=0

3. use

@RestController public class HelloController { @Value(“${server.port}”) Integer port; @GetMapping(“/set”) public String set(HttpSession session) { session.setAttribute(“user”, “javaboy”); return String.valueOf(port); } @GetMapping(“/get”) public String get(HttpSession session) { return session.getAttribute(“user”) + “:” + port; } }

Considering that the spring boot will be started in a cluster later, in order to obtain which spring boot service each request is provided by, the port number of the current service needs to be returned at each request. Therefore, I injected server Port.

4. introduce nginx

Enter the conf directory of the installation directory of nginx (the default is /usr/local/nginx/conf), and edit nginx Conf file:

1. upstream means configuring upstream servers

2. javaboy Org indicates the name of the server cluster, which can be chosen at will

3. separate services are configured in upstream

4. weight indicates the weight of the service, which means how many requests will be forwarded from nginx to the service

5. proxy in location_ Pass indicates the address of the request forwarding, / indicates that all requests are intercepted and forwarded to the newly configured service cluster

6. proxy_ Redirect indicates that when a redirect request occurs, nginx will automatically correct the response header data (the default is that Tomcat returns the redirect. At this time, the redirected address is the address of Tomcat. We need to modify it to make it the address of nginx).

Recommended Today

Vue3 + typescript + Pinia build an enterprise level development scaffold

From my first contactvue3It has been one year since the release. Becausevue3.2Release of version,<script setup&Amp; Amp; Gt; The experimental mark of has been removed, and many companies have started to use itvue3.2Development project. This article will help you how to use it quicklyvue3.x,typeScript, viteSet up an enterprise level development scaffold. Don’t talk too much nonsense, […]