Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)
1 概念
应用场景
应用场景:
- 防止用户重复下单,锁住用户id
- 防止商品超卖问题
- 锁住账户,防止并发操作
package main
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/kataras/iris/v12"
context2 "github.com/kataras/iris/v12/context"
"myTest/demo_home/redis_demo/distributed_lock/constant"
service2 "myTest/demo_home/redis_demo/distributed_lock/other_svc/service"
"sync"
)
func main() {
constant.RedisCli = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
DB: 0,
})
_, err := constant.RedisCli.Set(context.TODO(), constant.AppleKey, 500, -1).Result()
if err != nil && err != redis.Nil {
panic(err)
}
app := iris.New()
xLock2 := new(sync.Mutex)
app.Get("/consume", func(c *context2.Context) {
xLock2.Lock()
defer xLock2.Unlock()
service2.GoodsService2.Consume()
c.JSON("ok port:9999")
})
app.Listen(":9999", nil)
}
分布式锁必备特性
分布式锁需要具备的特性:
2 思路分析
宕机与过期
//通过lua脚本保证加锁与设置过期时间的原子性
func (r *RedisLock) TryLock() bool {
//通过lua脚本加锁[hincrby如果key不存在,则会主动创建,如果存在则会给count数加1,表示又重入一次]
lockCmd := "if redis.call('exists', KEYS[1]) == 0 or redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
"then " +
" redis.call('hincrby', KEYS[1], ARGV[1], 1) " +
" redis.call('expire', KEYS[1], ARGV[2]) " +
" return 1 " +
"else " +
" return 0 " +
"end"
result, err := r.redisCli.Eval(context.TODO(), lockCmd, []string{r.key}, r.Id, r.expire).Result()
if err != nil {
log.Errorf("tryLock %s %v", r.key, err)
return false
}
i := result.(int64)
if i == 1 {
//获取锁成功&自动续期
go r.reNewExpire()
return true
}
return false
}
防止误删key
func (r *RedisLock) Unlock() {
//通过lua脚本删除锁
//1. 查看锁是否存在,如果不存在,直接返回
//2. 如果存在,对锁进行hincrby -1操作,当减到0时,表明已经unlock完成,可以删除key
delCmd := "if redis.call('hexists', KEYS[1], ARGV[1]) == 0 " +
"then " +
" return nil " +
"elseif redis.call('hincrby', KEYS[1], ARGV[1], -1) == 0 " +
"then " +
" return redis.call('del', KEYS[1]) " +
"else " +
" return 0 " +
"end"
resp, err := r.redisCli.Eval(context.TODO(), delCmd, []string{r.key}, r.Id).Result()
if err != nil && err != redis.Nil {
log.Errorf("unlock %s %v", r.key, err)
}
if resp == nil {
fmt.Println("delKey=", resp)
return
}
}
Lua保证原子性
//lock 加锁&设置过期时间
"if redis.call('exists', KEYS[1]) == 0 or redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
"then " +
" redis.call('hincrby', KEYS[1], ARGV[1], 1) " +
" redis.call('expire', KEYS[1], ARGV[2]) " +
" return 1 " +
"else " +
" return 0 " +
"end"
//unlock解锁
delCmd := "if redis.call('hexists', KEYS[1], ARGV[1]) == 0 " +
"then " +
" return nil " +
"elseif redis.call('hincrby', KEYS[1], ARGV[1], -1) == 0 " +
"then " +
" return redis.call('del', KEYS[1]) " +
"else " +
" return 0 " +
"end"
//自动续期
renewCmd := "if redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
"then " +
" return redis.call('expire', KEYS[1], ARGV[2]) " +
"else " +
" return 0 " +
"end"
可重入锁
//通过hset&hincrby 保证可重入(记录加锁次数)
lockCmd := "if redis.call('exists', KEYS[1]) == 0 or redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
"then " +
" redis.call('hincrby', KEYS[1], ARGV[1], 1) " +
" redis.call('expire', KEYS[1], ARGV[2]) " +
" return 1 " +
"else " +
" return 0 " +
"end"
delCmd := "if redis.call('hexists', KEYS[1], ARGV[1]) == 0 " +
"then " +
" return nil " +
"elseif redis.call('hincrby', KEYS[1], ARGV[1], -1) == 0 " +
"then " +
" return redis.call('del', KEYS[1]) " +
"else " +
" return 0 " +
"end"
自动续期
// 判断锁是否存在,如果存在(表明业务还未完成),重新设置过期时间(自动续期)
renewCmd := "if redis.call('hexists', KEYS[1], ARGV[1]) == 1 " +
"then " +
" return redis.call('expire', KEYS[1], ARGV[2]) " +
"else " +
" return 0 " +
"end"
3 代码
3.1 项目结构解析
- constant模块:定义分布式锁名称、业务Key(用于模拟扣减数据库)
- lock模块:核心模块,实现分布式锁
- Lock
- TryLock
- UnLock
- NewRedisLock
- other_svc:在其他端口启另外一个服务,用于本地模拟分布式
- service:业务类,扣减商品数量(其中的扣减操作涉及分布式锁)
- main:提供iris web服务
3.2 全部代码
constant/const.go
package constant
import "github.com/go-redis/redis/v8"
var (
BizKey = "XXOO"
AppleKey = "apple"
RedisCli *redis.Client
)
lock/redis_lock.go
package service
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/ziyifast/log"
"myTest/demo_home/redis_demo/distributed_lock/constant"
"myTest/demo_home/redis_demo/distributed_lock/lock"
"strconv"
)
type goodsService struct {
}
var GoodsService = new(goodsService)
func (g *goodsService) Consume() {
redisLock := lock.NewRedisLock(constant.RedisCli, constant.BizKey)
redisLock.Lock()
defer redisLock.Unlock()
//consume goods
result, err := constant.RedisCli.Get(context.TODO(), constant.AppleKey).Result()
if err != nil && err != redis.Nil {
panic(err)
}
i, err := strconv.ParseInt(result, 10, 64)
if err != nil {
panic(err)
}
if i < 0 {
log.Infof("no more apple...")
return
}
_, err = constant.RedisCli.Set(context.TODO(), constant.AppleKey, i-1, -1).Result()
if err != nil && err != redis.Nil {
panic(err)
}
log.Infof("consume success...appleID:%d", i)
}
service/goods_service.go
package service
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/ziyifast/log"
"myTest/demo_home/redis_demo/distributed_lock/constant"
"myTest/demo_home/redis_demo/distributed_lock/lock"
"strconv"
)
type goodsService struct {
}
var GoodsService = new(goodsService)
func (g *goodsService) Consume() {
redisLock := lock.NewRedisLock(constant.RedisCli, constant.BizKey)
redisLock.Lock()
defer redisLock.Unlock()
//consume goods
result, err := constant.RedisCli.Get(context.TODO(), constant.AppleKey).Result()
if err != nil && err != redis.Nil {
panic(err)
}
i, err := strconv.ParseInt(result, 10, 64)
if err != nil {
panic(err)
}
if i < 0 {
log.Infof("no more apple...")
return
}
_, err = constant.RedisCli.Set(context.TODO(), constant.AppleKey, i-1, -1).Result()
if err != nil && err != redis.Nil {
panic(err)
}
log.Infof("consume success...appleID:%d", i)
}
main.go
package main
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/kataras/iris/v12"
context2 "github.com/kataras/iris/v12/context"
"myTest/demo_home/redis_demo/distributed_lock/constant"
"myTest/demo_home/redis_demo/distributed_lock/service"
)
func main() {
constant.RedisCli = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
DB: 0,
})
_, err := constant.RedisCli.Set(context.TODO(), constant.AppleKey, 500, -1).Result()
if err != nil && err != redis.Nil {
panic(err)
}
app := iris.New()
//xLock := new(sync.Mutex)
app.Get("/consume", func(c *context2.Context) {
//xLock.Lock()
//defer xLock.Unlock()
service.GoodsService.Consume()
c.JSON("ok port:8888")
})
app.Listen(":8888", nil)
}