Spring Boot multi-site use Redis achieve Session Sharing

How to share session Session between different sites (web service process) it, the principle is very simple, that is in one place, all the sites are read from this place this Session Session isolated storage.

Usually we use Redis to solve this problem

  • Spring Boot 2.1.8
  • Redis 5.0.3

session sharing scheme

The project github source code download

Chapter addresses the previous article Spring Boot achieve a production environment using the nginx pseudo heat update generated session sharing problem.

1 Redis ready

This example uses Redis 5.0.3 operating system for the Mac, on how to install redis your own search.

2 Establish Spring Boot Test Project

2.1 New Spring Boot Maven example projects

Note: IDEA is a development tool for

  1. File> New> Project, select the figure below Spring Initializrand then click [Next] Next
  2. Fill GroupId(package name), Artifact(project name) can be. Click Next
    groupId = com.fishpro
    artifactId = sharedsession
  3. The choice depends Spring Web Starterfront tick.
  4. Project name is set spring-boot-study-sharedsession.

2.2 dependent on the introduction of Pom

<dependencies>
        <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.44</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
    </dependencies>

2.3 application configuration file to configure

server:
  port: 8084
#2.x版本中由于引入了不同客户端,需要指定配置哪种连接池
#jedis客户端
spring:
  cache:
    type: redis
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    database: 0
    jedis:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0

2.4 adding test code

Open RedisSession

RedisSessionConfig new class using @EnableRedisHttpSession (maxInactiveIntervalInSeconds = 3600) annotation, wherein maxInactiveIntervalInSeconds represents the default time Session

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
public class RedisSessionConfig {
}

Controller set to write a file and read Session

@EnableRedisHttpSession
@RestController
public class IndexController {

    @GetMapping("")
    public String index(HttpServletRequest request){
        request.getSession().setAttribute("username", "公众号 程序鱼");
        request.getSession().setMaxInactiveInterval(10*1000);
        String username = (String)request.getSession().getAttribute("username");

        return "username"+username+ " session_id:"+request.getSession().getId();
    }
}

2.5 Test results

The project started at different check input port

  • http://localhost:8084/
  • http://localhost:8087/

session sharing different sites


Read related

Guess you like

Origin www.cnblogs.com/fishpro/p/spring-boot-study-sharedsession.html