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

The use of redis in springboot and how to solve the problem of distributed session sharing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about the use of redis in springboot and how to solve the problem of distributed session sharing. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Pom dependency

2 add redis configuration class

This configuration class can also configure cache expiration time and so on.

3 configure redis service

Because I am connected to a local (windows) testing machine, the password is empty by default, so you can configure it according to your own situation.

4 unit test

1. Set value (string)

Put a value with key as test001 and value as test001 to redis, and then check redis

2. Set value (object)

Put objects with key as user1 and user2 into redis, and the user2 setting expires in 5 seconds, and the thread waits for 6 seconds before completing. The expected result: there is user1 in redis, but there is no user2 bingodictions!

5 solve session sharing

Use spring-session-data-redis to implement session sharing, introduce this dependency in pom (added above), and add SessionConfig configuration class

Yes, I read it right. That's all I need. The longest valid time can be configured at will according to your own situation.

6 Test

Write a simple Controller, as follows

You can see sessionId here:

Look in redis.

You can see the failure time, sessionId, etc.

7 shared session

Find another machine, follow this configuration again, and automatically enable session sharing, because the sessionId is all in the same redis.

After reading the above, do you have any further understanding of the use of redis in springboot and how to solve the problem of distributed session sharing? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report