Springboot2. x series 74– Implementing distributed Session sharing in SpringBoot Session sharing is implemented in SpringBoot

  1. Creating a Web project

Following our previous experience, we created a Web application and transformed it into a Spring Boot project. The details are omitted.

2. Add a dependency package

3. Create the application.yml file

Redis spring: redis: host: 127.0.0.1 port: 6379 #password: 123456 Jedis: pool: max-idle: 8 min-idle: 0 max-active: 8 #max-wait: 60000 #timeout: 3000 # Timeout must be greater than 0 Session: # Set session storage type store-type: redis

You can set the store-type of multiple sessions:

Here, we choose to use Redis to centrally store sessions and realize session sharing.

4. Create a Session configuration class

package com.yyg.boot.config;

import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

/ * *

  • @author-Brother Sun
  • @Date Created in 2020/4/28
  • @description Enables the Redis Http Session

*/ @Configuration @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600) public class RedisHttpSessionConfiguration {

}

Here add @ EnableRedisHttpSession annotations, can pass maxInactiveIntervalInSeconds attribute set Session expiration time.

5. Create a Controller interface method

This interface method displays “User does not exist” if the user does not exist. Otherwise, “User Exists” is displayed.

package com.yyg.boot.web;

import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;

/ * *

  • @author-Brother Sun
  • @Date Created in 2020/4/28
  • @Description Description

*/ @Slf4j @RestController public class SessionController {

@RequestMapping("/session") public Object springSession(@RequestParam("username") String username, HttpServletRequest request, HttpSession session) { Cookie[] cookies = request.getCookies(); if (cookies ! = null && cookies.length > 0) { for (Cookie cookie : cookies) { log.warn(cookie.getName() + "=" + cookie.getValue()); } } Object value = session.getAttribute("username"); If (value == null) {log.warn(" user does not exist "); // Save session session.setAttribute("username", "{username: '+ username + ', age: 30}"); } else {log.warn(" user exists "); } return "username=" + value; }Copy the code

}

6. Create an entry class

package com.yyg.boot;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

/ * *

  • @author-Brother Sun
  • @Date Created in 2020/4/28
  • @Description Description

*/ @SpringBootApplication public class SpringSessionApplication {

Public static void main(String[] args){springApplication.run - This website can be sold. - Best Source of SpringApplication and related information. (SpringSessionApplication.class,args); }Copy the code

}

7. Complete project structure

8. Start the project for testing

When you log in to the browser for the first time, the username in the browser is null and the log information displayed in the console is user does not exist, indicating that no session has been created. However, after the first access, the session is created and stored in Redis for persistent storage. You can see the picture below:

On the second visit, you’ll see that username is already available for new information.

The user exists message is displayed in the log console.

And we can see in the Redis console, the TTL expiration time is 3660, refresh every 1 second, after 3600 seconds.

At this point, we can start processes 8080 and 8081 respectively to test the session interface on both processes.

# Go to the target directory of the project and execute the java-jar command to deploy our JAR package

F: \ onlineWorks \ boot – demos \ demo43_springsession \ target > Java jar demo43_springsession – 1.0 – the SNAPSHOT. The jar – server port = 8080

F: \ onlineWorks \ boot – demos \ demo43_springsession \ target > Java jar demo43_springsession – 1.0 – the SNAPSHOT. The jar – server port = 8081

In the browser, when accessing ports 8080 and 8081, we can see that there is a common Session information:

It can be seen that the same session information is accessed on two different process ports, indicating that session sharing in distributed processes is realized. As you can see, implementing Session sharing is still fairly easy with Spring Session.

This set of learning content written by Teacher Qian Feng, qian Feng Education Changsha Java Training Institute all copyright, welcome to reprint, please note the author. Thank you very much!