Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of distributed Cluster sharing Session in SpringBoot Development

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

SpringBoot development of distributed cluster sharing Session example analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Preface

In distributed systems, in order to improve system performance, a single project is usually split into multiple function-based micro-services, and if possible, a single micro-service may be extended horizontally to ensure high service availability.

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

Case

Here take the order as an example, the user Xiaoming took a picture of a doll on Tmall, felt good, bought it decisively, chose the size, picked the height, then confirmed the choice, hurriedly submitted the order, and then jumped to the login page! Xiaoming said that he was depressed, the question mark in uppercase.

Xiaoming enters the doll page and the request is sent to the business department through the proxy service. Xiao Ming chooses the size and height, and this operation does not send a request to the backend service. Xiaoming submitted the order, at this time the request was sent to the business system 2 through the agent service, but the second system did not query Xiaoming's login information at this time, so it was mercilessly redirected to the login page.

Scheme

HttpSession uses memory to manage Session by default, and usually the server stores user information in its own Jvm memory. So Xiaoming couldn't find the login information when he placed the order, so why don't I store the user information centrally?

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

Pom.xml introduces dependencies:

Org.springframework.session

Spring-session-data-redis

Org.springframework.boot

Spring-boot-starter-data-redis

Configure redis parameters, and install the software by yourself:

# # redis#session Storage Type spring.session.store-type=redisspring.redis.database=0spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=123456spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.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 ("incorrect password")

}}}

Configure the proxy 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;}}

Log in to the system after the configuration is successful, and query the user information in redis:

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

After reading the above, have you mastered the method of example analysis of distributed cluster sharing Session in SpringBoot development? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report