In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you an example of the implementation of redis distributed locks, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Redisson
Redisson and the following are the differences between the two methods of self-encapsulation (scenarios):
If the redisson does not acquire the lock, it will wait until the lock is acquired.
If the lock is not acquired in the other two ways, it will be abandoned and the business code will not be executed.
Org.redisson redisson-spring-boot-starter 3.13.6@Autowiredprivate Redisson redisson;@GetMapping ("/ redissonLock") public String redissonLock () {log.info ("entered the method"); RLock lock = redisson.getLock ("redissonLock"); try {lock.lock (30, TimeUnit.SECONDS); Thread.sleep (10000); System.out.println ("I am your Big Brother") } catch (InterruptedException e) {e.printStackTrace ();} finally {/ / if not released, other threads will automatically wake up after timeout and expiration, and will not execute the business code lock.unlock ();} return "end of run";}
RedisTemplate encapsulated redis Lock (1)
Org.springframework.boot spring-boot-starter-data-redispackage com.util;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.connection.RedisStringCommands;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.script.RedisScript;import org.springframework.data.redis.core.types.Expiration;import org.springframework.stereotype.Component;import java.util.Arrays @ Componentpublic class RedisLock {private static RedisTemplate redisTemplate; private static String script = "if redis.call (\" get\ ", KEYS [1]) = = ARGV [1] then\ n" + "\ treturn redis.call (\" del\ ", KEYS [1])\ n" + "else\ n" + "\ treturn 0\ t\ n" + "end" Public RedisLock (RedisTemplate redisTemplate) {RedisLock.redisTemplate = redisTemplate;} public static Boolean getLock (String key, String value, Long expireTime) {RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent (); Expiration expiration = Expiration.seconds (expireTime) RedisCallback booleanRedisCallback = new RedisCallback () {@ Override public Boolean doInRedis (RedisConnection connection) throws DataAccessException {return connection.set (redisTemplate.getKeySerializer () .serialize (key), redisTemplate.getValueSerializer () .serialize (value), expiration, setOption);}; return (Boolean) redisTemplate.execute (booleanRedisCallback) } public static Boolean unLock (String key, String value) {RedisScript redisScript = RedisScript.of (script, Boolean.class); return (Boolean) redisTemplate.execute (redisScript, Arrays.asList (key), value);} @ GetMapping ("/ redisLock") public String redisLock () {log.info ("entered method"); String key = "redisLock"; String uuid = UUID.randomUUID (). ToString () Try {if (RedisLock.getLock (key, uuid, 30L)) {log.info ("entered the lock"); Thread.sleep (10000);}} catch (InterruptedException e) {e.printStackTrace ();} finally {RedisLock.unLock (key, uuid);} log.info ("method execution complete"); return "Program ends";}
RedisTemplate encapsulated redis Lock (2)
Package com.util;import lombok.extern.slf4j.Slf4j;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.connection.RedisStringCommands;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.script.RedisScript;import org.springframework.data.redis.core.types.Expiration;import java.util.Arrays;import java.util.UUID;@Slf4jpublic class HighRedisLock implements AutoCloseable {private RedisTemplate redisTemplate Private String key; private String value; private Long expireTime; private static String script = "if redis.call (\" get\ ", KEYS [1]) = = ARGV [1] then\ n" + "\ treturn redis.call (\" del\ ", KEYS [1])\ n" + "else\ n" + "\ treturn 0\ t\ n" + "end" Public HighRedisLock (RedisTemplate redisTemplate, String key, Long expireTime) {this.redisTemplate = redisTemplate; this.key = key; this.value = UUID.randomUUID () .toString (); this.expireTime = expireTime;} public Boolean getLock () {RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent (); Expiration expiration = Expiration.seconds (expireTime) RedisCallback booleanRedisCallback = new RedisCallback () {@ Override public Boolean doInRedis (RedisConnection connection) throws DataAccessException {return connection.set (redisTemplate.getKeySerializer () .serialize (key), redisTemplate.getValueSerializer () .serialize (value), expiration, setOption);}; return (Boolean) redisTemplate.execute (booleanRedisCallback) } public Boolean unLock () {RedisScript redisScript = RedisScript.of (script, Boolean.class); return (Boolean) redisTemplate.execute (redisScript, Arrays.asList (key), value);} @ Override public void close () throws Exception {unLock ();}} @ Autowiredprivate RedisTemplate redisTemplate;@GetMapping ("/ highRedisLock") public String highRedisLock () {log.info ("entered method") Try (HighRedisLock redisLock = new HighRedisLock (redisTemplate, "highRedisLock", 30L)) {if (redisLock.getLock ()) {log.info ("entered the lock"); Thread.sleep (10000);} catch (InterruptedException e) {e.printStackTrace ();} catch (Exception e) {e.printStackTrace ();} log.info ("method execution complete") Return "Program ends";} these are all the contents of the article "implementation examples of redis distributed locks". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.