Httpsessions, which are created and managed by the Servlet container, will be stored in memory. Redis will be used to solve the problem of session sharing.

Create a project

Add the pom

Add the related Maven

<? 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.3.1. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < name > demo < / name > < description > demo projectforSpring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <! -- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> < version > 2.3.1. RELEASE < / version > < / dependency > <! -- https://mvnrepository.com/artifact/io.lettuce/lettuce-core --> <dependency> <groupId>io.lettuce</groupId> < artifactId > lettuce - core < / artifactId > < version > 6.0.0. M1 < / version > < / dependency > <! -- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> The < artifactId > jedis < / artifactId > < version > 3.3.0 < / version > < / dependency > <! -- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> < version > 2.3.0. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Copy the code

Configure redis connections

Configure redis connections

Spring: redis: database: 0 host: 106.53.115.12 port: 6379 password: 12345678 Jedis: Pool: max-active: 8 max-idle: 8 max-wait: -1ms min-idle: 0Copy the code

Create controllers to perform test operations

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class HelloController {
    @PostMapping("/save")
    public String saveName(String name, HttpSession session){
        session.setAttribute("name", name);
        return "8080";
    }
    
    @GetMapping("/get")
    public String getName(HttpSession httpSession){
        return httpSession.getAttribute("name").toString(); }}Copy the code

Nginx load balancing

mingming@xiaoming-pc:~$ sudo apt-get install nginx
Copy the code

Modifying a Configuration File

Upstream sang.com {server 192.168.0.1:8080 weight = 1; Server 192.168.0.2:8080 weight = 1; } server { listen 80; server_name localhost; location / { proxy_pass http://sang.com; proxy_redirect default; }}Copy the code

Request distribution

Save the data

To get the data

Wechat official account