redis分布式锁 分布式锁的作用及实现(Redis)

编辑:
发布时间: 2020-12-27 02:41:07
分享:
一、什么是分布式锁?

要介绍分布式锁,首先要提到与分布式锁相对应的是线程锁、进程锁。

线程锁:主要用来给方法、代码块加锁。当某个方法或代码使用锁,在同一时刻仅有一个线程执行该方法或该代码段。线程锁只在同一JVM中有效果,因为线程锁的实现在根本上是依靠线程之间共享内存实现的,比如synchronized是共享对象头,显示锁Lock是共享某个变量。

进程锁:为了控制同一操作系统中多个进程访问某个共享资源,因为进程具有独立性,各个进程无法访问其他进程的资源,因此无法通过synchronized等线程锁实现进程锁。

分布式锁:当多个进程不在同一个系统中,用分布式锁控制多个进程对资源的访问。

二、分布式锁的使用场景。

线程间并发问题和进程间并发问题都是可以通过分布式锁解决的,但是强烈不建议这样做!因为采用分布式锁解决这些小问题是非常消耗资源的!分布式锁应该用来解决分布式情况下的多进程并发问题才是最合适的。

有这样一个情境,线程A和线程B都共享某个变量X。

如果是单机情况下,线程之间共享内存,只要使用线程锁就可以解决并发问题。

如果是分布式情况下,线程A和线程B很可能不是在同一JVM中,这样线程锁就无法起到作用了,这时候就要用到分布式锁来解决。

三、分布式锁的实现

分布式锁实现的关键是在分布式的应用服务器外,搭建一个存储服务器,存储锁信息,这时候我们很容易就想到了Redis。首先我们要搭建一个Redis服务器,用Redis服务器来存储锁信息。

在实现的时候要注意的几个关键点:

1、锁信息必须是会过期超时的,不能让一个线程长期占有一个锁而导致死锁;

2、同一时刻只能有一个线程获取到锁。

几个要用到的redis命令:

setnx:“set if not exits”,若该key-value不存在,则成功加入缓存并且返回1,否则返回0。

get:获得key对应的value值,若不存在则返回nil。

getset:先获取key对应的value值,若不存在则返回nil,然后将旧的value更新为新的value。

expire:设置key-value的有效期为seconds秒。

看一下流程图:

在这个流程下,不会导致死锁。

我采用Jedis作为Redis客户端的api,下面来看一下具体实现的代码。

首先要创建一个Redis连接池。

public class RedisPool { private static JedisPool pool;//jedis连接池 private static int maxTotal = 20;//最大连接数 private static int maxIdle = 10;//最大空闲连接数 private static int minIdle = 5;//最小空闲连接数 private static boolean testOnBorrow = true;//在取连接时测试连接的可用性 private static boolean testOnReturn = false;//再还连接时不测试连接的可用性 static { initPool;//初始化连接池 } public static Jedis getJedis{ return pool.getResource; } public static void close{ jedis.close; } private static void initPool{ JedisPoolConfig config = new JedisPoolConfig; config.setMaxTotal; config.setMaxIdle; config.setMinIdle; config.setTestOnBorrow; config.setTestOnReturn; config.setBlockWhenExhausted; pool = new JedisPool; }}

对Jedis的api进行封装,封装一些实现分布式锁需要用到的操作。

public class RedisPoolUtil { private RedisPoolUtil{} private static RedisPool redisPool; public static String get{ Jedis jedis = null; String result = null; try { jedis = RedisPool.getJedis; result = jedis.get; } catch { e.printStackTrace; } finally { if { jedis.close; } return result; } } public static Long setnx{ Jedis jedis = null; Long result = null; try { jedis = RedisPool.getJedis; result = jedis.setnx; } catch { e.printStackTrace; } finally { if { jedis.close; } return result; } } public static String getSet{ Jedis jedis = null; String result = null; try { jedis = RedisPool.getJedis; result = jedis.getSet; } catch { e.printStackTrace; } finally { if { jedis.close; } return result; } } public static Long expire{ Jedis jedis = null; Long result = null; try { jedis = RedisPool.getJedis; result = jedis.expire; } catch { e.printStackTrace; } finally { if { jedis.close; } return result; } } public static Long del{ Jedis jedis = null; Long result = null; try { jedis = RedisPool.getJedis; result = jedis.del; } catch { e.printStackTrace; } finally { if { jedis.close; } return result; } }}

分布式锁工具类

public class DistributedLockUtil { private DistributedLockUtil{ } public static boolean lock{//lockName可以为共享变量名,也可以为方法名,主要是用于模拟锁信息 System.out.println + "开始尝试加锁!"); Long result = RedisPoolUtil.setnx + 5000)); if == 1){ System.out.println + "加锁成功!"); RedisPoolUtil.expire; System.out.println + "执行业务逻辑!"); RedisPoolUtil.del; return true; } else { String lockValueA = RedisPoolUtil.get; if >= System.currentTimeMillis){ String lockValueB = RedisPoolUtil.getSet + 5000)); if ){ System.out.println + "加锁成功!"); RedisPoolUtil.expire; System.out.println + "执行业务逻辑!"); RedisPoolUtil.del; return true; } else { return false; } } else { return false; } } }}

相关阅读
热门精选
孩子 皮肤