分布式锁在集群的架构中发挥着重要的作用。以下有主要的使用场景
1.在秒杀、抢购等高并发场景下,多个用户同时下单同一商品,可能导致库存超卖。
2.支付、转账等金融操作需保证同一账户的资金变动是串行执行的。
3.分布式环境下,多个节点可能同时触发同一任务(如定时报表生成)。
4.用户因网络延迟重复提交表单,可能导致数据重复插入。
目录
自定义分布式锁
获取锁
比如一下一个场景,需要对订单号为 order-88888944010的订单进行扣款处理,因为后端是多节点的,防止出现用户重复点击导致扣款请求到不用的集群节点,所以需要同时只有一个节点处理该订单。
public static async Task<(bool Success, string LockValue)> LockAsync(string cacheKey, int timeoutSeconds = 5)
{
var lockKey = GetLockKey(cacheKey);
var lockValue = Guid.NewGuid().ToString();
var timeoutMilliseconds = timeoutSeconds * 1000;
var expiration = TimeSpan.FromMilliseconds(timeoutMilliseconds);
bool flag = await _redisDb.StringSetAsync(lockKey, lockValue, expiration, When.NotExists);
return (flag, flag ? lockValue : string.Empty);
}
public static string GetLockKey(string cacheKey)
{
return $"MyApplication:locker:{cacheKey}";
}
上述代码是在请求时将订单号作为redis key的一部分存储到redis中,并且生成了一个随机的lockValue作为值。只有当redis中不存在该key的时候才能够成功设置,即为获取到该订单的分布式锁了。
await LockAsync("order-88888944010",30); //获取锁,并且设置超时时间为30秒
释放锁
public static async Task<bool> UnLockAsync(string cacheKey, string lockValue)
{
var lockKey = GetLockKey(cacheKey);
var script = @"local invalue = @value
local currvalue = redis.call('get',@key)
if(invalue==currvalue) then redis.call('del',@key)
return 1
else
return 0
end";
var parameters = new { key = lockKey, value = lockValue };
var prepared = LuaScript.Prepare(script);
var result = (int)await _redisDb.ScriptEvaluateAsync(prepared, parameters);
return result == 1;
}
释放锁采用了lua脚本先判断lockValue是否是同一个处理节点发过来的删除请求,即判断加锁和释放锁是同一个来源。
用lua脚本而不是直接使用API执行删除的原因:
1.A获取锁后因GC停顿或网络延迟导致锁过期,此时客户端B获取了锁。若A恢复后直接调用DEL,会错误删除B持有的锁。
2.脚本在Redis中单线程执行,确保GET和DEL之间不会被其他命令打断。
自动续期
一些比较耗时的任务,可能在指定的超时时间内无法完成业务处理,需要存在自动续期的机制。
/// <summary>
/// 自动续期
/// </summary>
/// <param name="redisDb"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="milliseconds">续期的时间</param>
/// <returns></returns>
public async static Task Delay(IDatabase redisDb, string key, string value, int milliseconds)
{
if (!AutoDelayHandler.Instance.ContainsKey(key))
return;
var script = @"local val = redis.call('GET', @key)
if val==@value then
redis.call('PEXPIRE', @key, @milliseconds)
return 1
end
return 0";
object parameters = new { key, value, milliseconds };
var prepared = LuaScript.Prepare(script);
var result = await redisDb.ScriptEvaluateAsync(prepared, parameters, CommandFlags.None);
if ((int)result == 0)
{
AutoDelayHandler.Instance.CloseTask(key);
}
return;
}
保存自动续期任务的处理器
public class AutoDelayHandler
{
private static readonly Lazy<AutoDelayHandler> lazy = new Lazy<AutoDelayHandler>(() => new AutoDelayHandler());
private static ConcurrentDictionary<string, (Task, CancellationTokenSource)> _tasks = new ConcurrentDictionary<string, (Task, CancellationTokenSource)>();
public static AutoDelayHandler Instance => lazy.Value;
/// <summary>
/// 任务令牌添加到集合中
/// </summary>
/// <param name="key"></param>
/// <param name="task"></param>
/// <returns></returns>
public bool TryAdd(string key, Task task, CancellationTokenSource token)
{
if (_tasks.TryAdd(key, (task, token)))
{
task.Start();
return true;
}
else
{
return false;
}
}
public void CloseTask(string key)
{
if (_tasks.ContainsKey(key))
{
if (_tasks.TryRemove(key, out (Task, CancellationTokenSource) item))
{
item.Item2?.Cancel();
item.Item1?.Dispose();
}
}
}
public bool ContainsKey(string key)
{
return _tasks.ContainsKey(key);
}
}
在申请带有自动续期的分布式锁的完整代码
/// <summary>
/// 获取锁
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="timeoutSeconds">超时时间</param>
/// <param name="autoDelay">是否自动续期</param>
/// <returns></returns>
public static async Task<(bool Success, string LockValue)> LockAsync(string cacheKey, int timeoutSeconds = 5, bool autoDelay = false)
{
var lockKey = GetLockKey(cacheKey);
var lockValue = Guid.NewGuid().ToString();
var timeoutMilliseconds = timeoutSeconds * 1000;
var expiration = TimeSpan.FromMilliseconds(timeoutMilliseconds);
bool flag = await _redisDb.StringSetAsync(lockKey, lockValue, expiration, When.NotExists);
if (flag && autoDelay)
{
//需要自动续期,创建后台任务
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
var autoDelaytask = new Task(async () =>
{
while (!cancellationTokenSource.IsCancellationRequested)
{
await Task.Delay(timeoutMilliseconds / 2);
await Delay(lockKey, lockValue, timeoutMilliseconds);
}
}, cancellationTokenSource.Token);
var result = AutoDelayHandler.Instance.TryAdd(lockKey, autoDelaytask, cancellationTokenSource);
if (!result)
{
autoDelaytask.Dispose();
await UnLockAsync(cacheKey, lockValue);
return (false, string.Empty);
}
}
return (flag, flag ? lockValue : string.Empty);
}
Redis的过期时间精度约为1秒,且过期检查是周期性执行的(默认每秒10次)。选择TTL/2的间隔能:
确保在Redis下一次过期检查前完成续期。
兼容Redis的主从同步延迟(通常<1秒)
StackExchange.Redis分布式锁
获取锁
string lockKey = "order:88888944010:lock";
string lockValue = Guid.NewGuid().ToString(); // 唯一标识锁持有者
TimeSpan expiry = TimeSpan.FromSeconds(10); // 锁自动过期时间
// 尝试获取锁(原子操作)
bool lockAcquired = db.LockTake(lockKey, lockValue, expiry);
释放锁
bool released = await ReleaseLockAsync(db, lockKey, lockValue);
自动续期
同样需要自己实现