In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to open the distributed lock of Redis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to open the distributed lock of Redis".
Request
The implementation of distributed locks based on Redis needs to meet the following requirements:
In a distributed cluster, a method or code segment controlled by a distributed lock can only be executed by one thread on a client at a time, that is, mutual exclusion.
Lock information needs to set expiration time to avoid deadlock caused by long-term possession of a thread (such as abnormal exit before unlocking operation).
Locking and unlocking must be consistent. Whoever adds the lock will be unlocked (or expired). One client cannot unlock the lock added by another client.
The process of locking and unlocking must ensure atomicity.
Achieve 1. Locking implementation
Redis-based distributed lock locking operations generally use the SETNX command, which means "set the value of key to value if and only if key does not exist. If a given key already exists, the SETNX does not do anything."
In Spring Boot, you can use StringRedisTemplate to implement the locking process in one line of code as follows. (the following code gives two forms of invocation-- returning the locking result immediately and getting the locking result with a given timeout)
/ * attempt to acquire lock (return immediately) * @ param key lock redis key * @ param value lock value * @ param expire expiration time / second * @ return successfully obtained * / public boolean lock (String key, String value, long expire) {return stringRedisTemplate.opsForValue () .setIfAbsent (key, value, expire, TimeUnit.SECONDS) } / * attempt to acquire lock And wait at most for timeout length * * @ param key lock redis key * @ param value lock value * @ param expire expiration time / second * @ param timeout timeout length * @ param unit time unit * @ return whether to obtain successful * / public boolean lock (String key, String value, long expire, long timeout, TimeUnit unit) {long waitMillis = unit.toMillis (timeout) Long waitAlready = 0; while (! stringRedisTemplate.opsForValue (). SetIfAbsent (key, value, expire, TimeUnit.SECONDS) & & waitAlready < waitMillis) {try {Thread.sleep (waitMillisPer);} catch (InterruptedException e) {log.error ("Interrupted when trying to get a lock. Key: {} ", key, e);} waitAlready + = waitMillisPer;} if (waitAlready < waitMillis) {return true;} log.warn ("
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.