使用redis来实现分布式锁
在实际的工作中,有部分的特定场景需要使用到分布式锁来进行跨服务器资源的统一调配。之前在一家医疗互联网公司,因为黄牛抢号等原因,造成同一个患者同一时段在同一个医生处,挂到了两个及以上的号,我对之前我司实现的代码进行了封装和改进,在github上提供了源码,有需要的朋友,可以下载代码,并用maven打包成jar包,使用起来比较方便。
源码地址:https://github.com/mantuliu/distributed
核心feature:
redis的setnx()方法,此方法提供了一个原子操作,可以保证有且只有一个分布式的调用返回值为1,在分布式锁的概念里,则代表此锁被此次调用的线程占用;
redis的expire()方法,通过此方法来设置此锁的过期时间;
对于死锁的情况,封装的分布式锁包可以自动解锁;
每次获取锁的线程,都会被标识,当此线程还没有释放锁时,此线程继续调用trylock方法,还可以获得该锁,只有获取该锁的线程才有unlock()释放锁的权利;
支持trylock()和trylock(long timeout, TimeUnit unit)两种方式;
使用应用服务器提供的jedis实例;
对于当前服务器试图获取该锁的线程数量进行监控,当数量大于阀值时,后续线程在trylock(long timeout, TimeUnit unit)及trylock()时,直接返回失败,阀值可以设置;
锁的过期时间默认值是5秒,可以根据实际情况进行设置;
锁的键值前缀默认值是mantu:dislock:,可以根据实际情况进行设置;
通过LockSupport类来获取锁,使得试图获取同一把锁的线程得到的对象是同一个。
源码解析:
CommonType类
public class CommonType { public static int WAITLOCKERS = 2;//当前服务器等待锁的线程数量,如果超过或等于此值,当前线程直接返回,不再等待锁 public static String REDISKEY="mantu:dislock:";//redis下key前缀 public static int LOCKEXPIRETIME = 5;//锁的过期时间,单位秒,默认5秒过期 }
DisLock接口
1 2 3 4 5 6 | public interface DisLock{ boolean tryLock(Jedis jedis); boolean tryLock( long time, TimeUnit unit,Jedis jedis) throws InterruptedException; void unlock(Jedis jedis); } |
RedisDisLock类,实际的管理锁的类
public class RedisDisLock implements DisLock{ private static final Logger LOG = LoggerFactory.getLogger(RedisDisLock.class); private transient Thread exclusiveOwnerThread; String lockKey=""; AtomicInteger waitToLock=new AtomicInteger(0); public RedisDisLock(String lockKey){ this.lockKey=CommonType.REDISKEY+lockKey; } public boolean tryLock(Jedis jedis) { Thread thread = Thread.currentThread(); if(thread==this.getExclusiveOwnerThread()){ return true; } Long i = jedis.setnx(lockKey, System.currentTimeMillis()+""); if(i.intValue()==1){ jedis.expire(lockKey, CommonType.LOCKEXPIRETIME); setExclusiveOwnerThread(thread); return true; } else{//对于可能性非常低的死锁情况进行解锁 String initTime = jedis.get(lockKey); if(initTime==null){ LOG.debug("initTime's value is null"); return false; } long iniTime=0L; try{ iniTime = Long.parseLong(initTime); } catch(NumberFormatException nfex){ LOG.warn(nfex.getMessage()); jedis.expire(lockKey, 1); return false; } if(((System.currentTimeMillis()-iniTime)/1000-CommonType.LOCKEXPIRETIME-1)>0){ String oldTime = jedis.getSet(lockKey, System.currentTimeMillis()+"");//对于及其极端的情况,lock被线程1处理掉了,但是又被线程2getset新的值了,通过下一次调用trylock()方法处理 if(oldTime==null){ LOG.info("oldTime is null"); return false; } if(initTime.equals(oldTime)){ release(jedis); } } } return false; } public boolean tryLock(long timeout, TimeUnit unit,Jedis jedis) throws InterruptedException { long nanosTimeout = unit.toNanos(timeout); long lastTime = System.nanoTime(); if(tryLock(jedis)){ return true; } try{ int waitLockers = waitToLock.getAndIncrement(); if(waitLockers>=CommonType.WAITLOCKERS){ LOG.debug("wait the lock' thread num is much,so return flase"); return false; } for(;;){ if(tryLock(jedis)){ return true; } if (nanosTimeout <= 0){ LOG.debug("getlock timeout"); return false; } if(nanosTimeout>100000){ LockSupport.parkNanos(100000);//中断100毫秒 } long now = System.nanoTime(); nanosTimeout -= now - lastTime; lastTime = now; if (nanosTimeout <= 0){ LOG.debug("getlock timeout"); return false; } if (Thread.interrupted()){ throw new InterruptedException(); } } } finally{ waitToLock.decrementAndGet(); } } public void unlock(Jedis jedis) { Thread thread = Thread.currentThread(); if(thread==this.getExclusiveOwnerThread()){ LOG.debug("unlock the thread {}",thread.getId()); release(jedis); } } private void release(Jedis jedis){ setExclusiveOwnerThread(null); jedis.del(lockKey); } /** * Sets the thread that currently owns exclusive access. A * <tt>null</tt> argument indicates that no thread owns access. * This method does not otherwise impose any synchronization or * <tt>volatile</tt> field accesses. */ protected final void setExclusiveOwnerThread(Thread t) { exclusiveOwnerThread = t; } /** * Returns the thread last set by * <tt>setExclusiveOwnerThread</tt>, or <tt>null</tt> if never * set. This method does not otherwise impose any synchronization * or <tt>volatile</tt> field accesses. * @return the owner thread */ protected final Thread getExclusiveOwnerThread() { return exclusiveOwnerThread; } }
LockSupport类,实际的业务代码首先要通过LockSupport来获取redis锁的对象,再使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class LockSupport { static ConcurrentHashMap <String,RedisDisLock>lockMap = new ConcurrentHashMap<String,RedisDisLock>(); public static DisLock getRedisLock(String lockKey){ RedisDisLock lock= null ; if (lockMap.contains(lockKey)){ lock = lockMap.get(lockKey); } else { RedisDisLock lockN = new RedisDisLock(lockKey); lock = lockMap.putIfAbsent(lockKey, lockN); if (lock== null ){ lock=lockN; } } return lock; } } |
RedisDisLockTest类是使用此jar的demo代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | public class RedisDisLockTest { public static void main(String [] args){ RedisDisLockTest test = new RedisDisLockTest(); //test.testOrder(); //test.testOrder2(); //test.testNOUnlock(); test.testOtherUnlock(); } public void testOrder(){ JedisPool jp = new JedisPool( "127.0.0.1" , 6379 ); for ( int i= 0 ;i< 5 ;i++){ Jedis jedis = jp.getResource(); OrderThread th = new OrderThread( "123456" ,jedis); th.start(); } } public void testOrder2(){ JedisPool jp = new JedisPool( "127.0.0.1" , 6379 ); for ( int i= 0 ;i< 5 ;i++){ Jedis jedis = jp.getResource(); OrderThread th = new OrderThread( "1234567" ,jedis); th.start(); } } public void testNOUnlock(){ JedisPool jp = new JedisPool( "127.0.0.1" , 6379 ); for ( int i= 0 ;i< 5 ;i++){ Jedis jedis = jp.getResource(); TestNOUnlock th = new TestNOUnlock( "12345678" ,jedis); th.start(); } } public void testOtherUnlock(){ JedisPool jp = new JedisPool( "127.0.0.1" , 6379 ); for ( int i= 0 ;i< 5 ;i++){ Jedis jedis = jp.getResource(); TestOtherUnlock th = new TestOtherUnlock( "unlock" ,jedis); th.start(); } } class OrderThread extends Thread{ String lockKey= "" ; Jedis jedis; public OrderThread(String lockKey,Jedis jedis){ this .lockKey=lockKey; this .jedis=jedis; } public void run(){ DisLock lock = LockSupport.getRedisLock(lockKey); try { if (lock.tryLock( 2 ,TimeUnit.SECONDS,jedis)){ System.out.println( "订单" +lockKey+ "创建成功!" ); lock.unlock(jedis); } else { System.out.println( "没有成功获取到锁" ); } } catch (InterruptedException e) { e.printStackTrace(); } } } class TestNOUnlock extends Thread{ String lockKey= "" ; Jedis jedis; public TestNOUnlock(String lockKey,Jedis jedis){ this .lockKey=lockKey; this .jedis=jedis; } public void run(){ DisLock lock = LockSupport.getRedisLock(lockKey); try { if (lock.tryLock( 2 ,TimeUnit.SECONDS,jedis)){ System.out.println( "订单" +lockKey+ "创建成功!" ); //lock.unlock(jedis);//no unlock } else { System.out.println( "没有成功获取到锁" ); } } catch (InterruptedException e) { e.printStackTrace(); } } } class TestOtherUnlock extends Thread{ String lockKey= "" ; Jedis jedis; public TestOtherUnlock(String lockKey,Jedis jedis){ this .lockKey=lockKey; this .jedis=jedis; } public void run(){ DisLock lock = LockSupport.getRedisLock(lockKey); if (lock.tryLock(jedis)){ System.out.println( "订单" +lockKey+ "创建成功!" ); //lock.unlock(jedis);//no unlock } else { lock.unlock(jedis); System.out.println( "TestOtherUnlock没有成功获取到锁" ); } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?