Taodong E-commerce Project (30) - Solving the Distributed Session Sharing Problem

Introduction

This code has been submitted to Github (version number: 3b2359205b2d49f9eae313d8cca119082dd7bc99). Interested students can download it to see: https://github.com/ylw-github/taodong-shop

This article mainly solves the distributed Session problem.

Text directory structure:
l_uu Introduction
L_uu 1. Distributed Session s
L_uu 2. Solutions
l____ 3. spring-session
L_uu 4. Testing
l_uu Summary

1. Distributed Session s

1. First copy a portal-web in the "Taodong E-commerce" project and modify the port number to 8081 to simulate the portal cluster, as follows:

2. The project adds controller as validation:

@RestController
public class TestSessionController {

    @Value("${server.port}")
    private Integer projectPort;// Project Port

    @RequestMapping("/createSession")
    public String createSession(HttpSession session, String name) {
        session.setAttribute("name", name);
        return "Current project port:" + projectPort + " current sessionId :" + session.getId() + "stay Session Save in Success!";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpSession session) {
        return "Current project port:" + projectPort + " current sessionId :" + session.getId() + " Name Obtained:" + session.getAttribute("name");
    }
}

3.Nginx load balancing configuration:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;


    keepalive_timeout  65;
  
    upstream  backServer{
	    server 192.168.18.166:8080;
	    server 192.168.18.166:8081;
	}
    server {
        listen       8099;
        server_name  192.168.166.137;
        location / {
           proxy_pass http://backServer;
           index  index.html index.htm;
        }      
    }
}

4. Browser request for session, browser request http://192.168.162.137:8099/getSession:

Request once Request twice

You can see that two Session s have inconsistent id s, which is not what we want.So how can we solve that?Here's the solution.

2. Solutions

There are several solutions:

  • Use cookie s to do this (disadvantage: unsafe, unreliable)
  • Using the ip binding policy in Nginx, the same ip can only be accessed on the same machine specified (disadvantage: load balancing is not supported)
  • Synchronize session s with databases (disadvantage: inefficient)
  • Use session synchronization built into tomcat (disadvantage: synchronization can cause delays)
  • Use token instead of session (there's a good solution, spring-session)
  • We use spring-session s and integrated solutions to store in redis (recommended).

3. spring-session

step1: first introduce the maven dependency of spring-session:

<!--spring session and redis Apply Basic Environment Configuration,Need to be turned on redis Then it can be used, otherwise start Spring boot Errors will occur -->
<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

step2: Accessory Redis:

spring:
  redis:
    host: 192.168.162.136
    port: 6379
    jedis:
      pool:
        max-idle: 100
        min-idle: 1
        max-active: 1000
        max-wait: -1

step3: Startup class declaration allows Session sharing:

//maxInactiveIntervalInSeconds is the expiration time of SpringSession in seconds
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class AppPortalWeb {

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

4. Testing

Start Project:

First the browser creates the session and sets the content to session: http://192.168.162.137:8099/createSession?name='ylw'

Browser request: http://192.168.162.137:8099/getSession , you can see that both sessionid s are: f8b9a9ab-d285-4b04-8d98-66715cbdc132

Request 8080 Request 8081

Open Medis and you can see that redis has saved the values of the related redis:

summary

This article mainly explains how to use spring-session to solve session sharing in a distributed environment.

2669 original articles were published. 5,063 won. 490,000 visits+
His message board follow

Tags: Session Spring Redis github

Posted on Thu, 12 Mar 2020 22:24:40 -0400 by onepixel