Cluster session Solution

session workaround cluster state

In a clustered environment, requests may be forwarded to a different server process up, this will lead, after server A fine, while other tasks are assigned to On Server B, Server A case session does not exist on the server on B, will let users log on again, so the user experience is certainly not acceptable.

solution

  1. session replication (if a cluster environment is too large, not recommended)
  2. sharing session
  3. Stored in the cookie (not recommended)

The way to solve the shared use of session

  • Test environment: spring boot + redis
  • Chrome browser testing tool
  • Development Tools idea
  • Project build tools maven

 

1: First create springboot project

    

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--spring session 核心依赖-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <!--spring session redis dependent operation -> 
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</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>
@GetMapping("/get")
    public String index(HttpServletRequest request) {
        Object name = request.getSession().getAttribute("name");
        return name != null ? "success" : "false";
    }

    @GetMapping("/set")
    public String index(HttpServletRequest request,HttpServletResponse response){
        Object name = request.getSession().getAttribute("name");
        if (null==name)
            request.getSession().setAttribute("name", "zs");
        return "set success";
    }

Springboot running two projects, one at port 8080, a port 8081, simulation cluster environment

 

 

 8080,8081 two projects access ports are false, there is no session description at this time

 

 At this point after a session manually set value, another visit to get, return to success

 

 

 Access Port 8081 project

 

 Return success, to explain the success of obtaining session

 

There is value just deposited redis session, the session description to obtain a successful cluster.

 

  

Guess you like

Origin www.cnblogs.com/yjp372928571/p/12667519.html