Distributed Cluster Sharing Session of SpringBook Development Case

Preface

In distributed systems, in order to improve system performance, individual projects are usually split into multiple function-based micro-services. If conditions permit, single micro-services may be scaled horizontally to ensure high availability of services.

So the question is, what kind of problems will we encounter if we use the traditional way of managing Session?

case

Here take the list as an example. User Xiaoming took a picture of a doll on Tiancat. He thought it was good. He purchased it decisively, chose the size and height, then confirmed the choice, submitted the order quickly, and then jumped to the login page! Xiao Ming said he was depressed, capitalized question mark???

  • Xiaoming enters the doll page and requests to be sent to Business System 1 through proxy service.
  • Small size, high height, this operation did not send a request to the back-end service.
  • Xiao Ming submitted the order, at this time the request was sent to business system 2 through proxy service, but goose, the second system did not inquire Xiao Ming's login information at this time, was relentlessly jumped to the login page.

programme

By default, HttpSession uses memory to manage Session. Usually the server stores user information in its own Jvm memory. So Xiaoming can't find the login information when he orders, so why don't we store the user information centrally!?

In order to test the effect, here we build a demonstration case involving SpringBoot, spring-session, redis, nginx and other related components.

pom.xml introduces dependencies:

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

Configure redis parameters and install the software by itself:

## redis
#session storage type
spring.session.store-type=redis
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=3000

Simple user login implementation, omitting part of the code:

@RequestMapping(value="login",method=RequestMethod.POST)
public Result login(String username,String password,HttpServletRequest request,HttpServletResponse response) throws Exception {
        SysUser user = userService.getUser(username);
        if(user==null) {
            return Result.error("user does not exist");
        }else {
            if(user.getPassword().equals(password)) {
                request.getSession().setAttribute("user", user);
                return Result.ok();
            }else {
                return Result.error("Password error");
            }
        }
}

Configuration agent implementation based on Nginx:

server {
        listen       80;
        server_name  blog.52itstyle.vip;
        location / {
            proxy_pass http://192.168.1.2:8080; 
        }
        location /cart {
             proxy_pass http://192.168.1.3:8080$request_uri;
        }
       location /order {
             proxy_pass http://192.168.1.4:8080$request_uri;
        }
  }

After successful configuration, login system and query user information in redis:

127.0.0.1:6379> keys *
1) "spring:session:expirations:1562577660000"
2) "spring:session:sessions:1076c2bd-95b1-4f23-abd4-ab3780e32f6f"
3) "spring:session:sessions:expires:1076c2bd-95b1-4f23-abd4-ab3780e32f6f"

Summary

In this way, Xiao Ming can be happy to buy dolls!

Keywords: Database Spring Redis Session Nginx

Added by pacome on Tue, 30 Jul 2019 08:30:56 +0300