Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-02 03:34:49 09月26日 Update

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.

Tags: Configuration content time testing distributed problem situation longest valid above unit character string password object more machine light knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Apple Linux NVidia