SpringBoot实现Redis分布式锁


转自:

https://www.jianshu.com/p/750ac97eb29e

 

什么是分布式锁

锁是什么我们当然知道,在多线程程序中,不予许多个线程同时操作某个变量或者同时执行某一代码块,我们就需要用来实现。在Java中,可以用synchronized或Lock接口的实现类来实现。那么什么是分布式锁呢?当我们的应用通过分布式部署,每个应用部署在不同的机器上,但是我们要保证这些不同机器上的同一方法在同一时间不能被多个线程执行,这时候就要用到分布式锁。分布式锁有很多种实现方式,这里我们介绍Redis实现方式。

基于 redis的 SETNX()、EXPIRE() 方法做分布式锁

-SETNX()
setnx接收两个参数key,value。如果key存在,则不做任何操作,返回0,若key不存在,则设置成功,返回1。
-EXPIRE()
expire 设置过期时间,要注意的是 setnx 命令不能设置 key 的超时时间,只能通过 expire() 来对 key 设置。
首先去redis官网下载redis,将文件解压,运行redis-server.exe启动redis服务。新建一个SpringBoot项目,添加redis依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 

在application.yml配置文件加上redis配置

spring: redis: host: 127.0.0.1 port: 6379 

新建redis配置类RedisConfig.java

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.*; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @author 清风 * @email 1737543007@qq.com * @date 19-3-5 */ 

在实现redis分布式锁之前,我们先分析一下需要注意的问题:
1.加锁过程必须设置过期时间,加锁和设置过期时间过程必须是原子操作
如果没有设置过期时间,那么就发生死锁,锁永远不能被释放。如果加锁后服务宕机或程序崩溃,来不及设置过期时间,同样会发生死锁。
2.解锁必须是解除自己加上的锁
试想一个这样的场景,服务A加锁,但执行效率非常慢,导致锁失效后还未执行完,但这时候服务B已经拿到锁了,这时候服务A执行完毕了去解锁,把服务B的锁给解掉了,其他服务C、D、E...都可以拿到锁了,这就有问题了。加锁的时候我们可以设置唯一value,解锁时判断是不是自己先前的value就行了。
redis锁代码

import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStringCommands; import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.core.RedisConnectionUtils; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.types.Expiration; import org.springframework.stereotype.Repository; import java.nio.charset.Charset; import java.util.UUID; import java.util.concurrent.TimeUnit; /** * @author 清风 * @email 1737543007@qq.com * @date 19-3-5 */ @Repository public class RedisLock { /** * 解锁脚本,原子操作 */ private static final String unlockScript = "if redis.call(\"get\",KEYS[1]) == ARGV[1]\n" + "then\n" + " return redis.call(\"del\",KEYS[1])\n" + "else\n" + " return 0\n" + "end"; private StringRedisTemplate redisTemplate; public RedisLock(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } /** * 加锁,有阻塞 * @param name * @param expire * @param timeout * @return */ public String lock(String name, long expire, long timeout){ long startTime = System.currentTimeMillis(); String token; do{ token = tryLock(name, expire); if(token == null) { if((System.currentTimeMillis()-startTime) > (timeout-50)) break; try { Thread.sleep(50); //try 50 per sec } catch (InterruptedException e) { e.printStackTrace(); return null; } } }while(token==null); return token; } /** * 加锁,无阻塞 * @param name * @param expire * @return */ public String tryLock(String name, long expire) { String token = UUID.randomUUID().toString(); RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection conn = factory.getConnection(); try{ Boolean result = conn.set(name.getBytes(Charset.forName("UTF-8")), token.getBytes(Charset.forName("UTF-8")), Expiration.from(expire, TimeUnit.MILLISECONDS), RedisStringCommands.SetOption.SET_IF_ABSENT); if(result!=null && result) return token; }finally { RedisConnectionUtils.releaseConnection(conn, factory); } return null; } /** * 解锁 * @param name * @param token * @return */ public boolean unlock(String name, String token) { byte[][] keysAndArgs = new byte[2][]; keysAndArgs[0] = name.getBytes(Charset.forName("UTF-8")); keysAndArgs[1] = token.getBytes(Charset.forName("UTF-8")); RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection conn = factory.getConnection(); try { Long result = (Long)conn.scriptingCommands().eval(unlockScript.getBytes(Charset.forName("UTF-8")), ReturnType.INTEGER, 1, keysAndArgs); if(result!=null && result>0) return true; }finally { RedisConnectionUtils.releaseConnection(conn, factory); } return false; } } 

使用

        String token = null; try{ token = redisLock.lock("lock_name", 10000, 11000); if(token != null) { System.out.print("我拿到了锁哦") // 执行业务代码 } else { System.out.print("我没有拿到锁唉") } } finally { if(token!=null) { redisLock.unlock("lock_name", token); } } 

That's all



作者:清清清清风
链接:https://www.jianshu.com/p/750ac97eb29e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM