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


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:

public class TestSessionController {

    private Integer projectPort;// Project Port

    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!";

    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 {
        listen       8099;
        location / {
           proxy_pass http://backServer;
           index  index.html index.htm;

4. Browser request for session, browser request

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 -->

step2: Accessory Redis:

    port: 6379
        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:'ylw'

Browser request: , 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:


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