In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to understand the source code of Redisson distributed locks". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Redis publish and subscribe
As mentioned earlier, there are actually only three core functions of distributed locks: locking, unlocking, and setting lock timeouts. These three functions are also the direction of our research on the principle of Redisson distributed lock.
Before we learn, it is necessary to understand one point of knowledge, that is, the publish and subscribe function of Redis.
Redis publish subscription (pub/sub) is a kind of message communication mode: sender (pub) sends messages, subscribers (sub) receive messages, publishers can send messages to designated channels (channel), subscribers can receive messages if they subscribe to this channel, so as to achieve the communication effect of multiple clients.
The subscription command is SUBSCRIBE channel [channel...], and you can subscribe to one or more channels. When a new message is sent to the channel through the PUBLISH command, the subscriber can receive the message, like this:
Open two clients, one subscribe to the channel channel1, and the other sends a message through PUBLISH, the subscriber can receive it, and the communication between different clients can be realized by this mode.
We will not expand on the wonderful scenarios of this communication mode. You can check and learn online by yourself. our protagonist is still Redisson. After the warm-up, it's time for the main course.
Redisson source code
Before using Redisson to lock, you need to get a RLock instance object. With this object, you can call lock and tryLock methods to complete the locking function.
Config config = new Config (); config.useSingleServer () .setPassword ("") .setAddress ("redis://127.0.0.1:6379"); RedissonClient redisson = Redisson.create (config); / / RLock object RLock lock = redisson.getLock ("myLock")
After configuring the corresponding host, you can create a RLock object. RLock is an interface, and the specific synchronizer needs to implement this interface. When we call redisson.getLock (), the program initializes a default synchronization executor RedissonLock.
Several parameters are initialized in this.
CommandExecutor: asynchronous Executor executor. All commands in Redisson are executed through... Executor.
Id: unique ID, which is created with UUID during initialization
InternalLockLeaseTime: wait for the lock time to be acquired, which is defined by default in the configuration class. The time is 30 seconds.
At the same time, I also marked a method getEntryName in the picture, which returns a string of "ID: lock name", which represents that the current thread holds an identity of the corresponding lock. These parameters need to make an impression, which often appears in the later source code parsing.
After talking about the initialization, we can begin to learn the source code for locking and unlocking.
Add lock
There are two locking methods for Redisson, tryLock and lock. The difference in use is that tryLock can set the expiration time leaseTime and wait time waitTime of the lock. The core processing logic is similar. Let's start with tryLock.
TryLock
The code is a little long. It's not convenient to put it into a picture. Just post it.
/ * @ param waitTime time to wait for a lock * @ time to hold a param leaseTime lock * @ param unit time unit * @ return * @ throws InterruptedException * / public boolean tryLock (long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {/ / remaining time to wait for a lock long time = unit.toMillis (waitTime); long current = System.currentTimeMillis () Final long threadId = Thread.currentThread () .getId (); / / attempt to acquire the lock. If the lock is not acquired, the remaining timeout of the lock is returned Long ttl = tryAcquire (leaseTime, unit, threadId); / / ttl is null, indicating that the lock can be grabbed, and true if (ttl = = null) {return true is returned } / / if the waitTime has timed out, return false, which means that the application for lock failed time-= (System.currentTimeMillis ()-current); if (time)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.