当前位置: 首页 > news >正文

怎么用电脑做网站服务器网站推广的公司

怎么用电脑做网站服务器,网站推广的公司,现在pc网站的标准一般是做多大,wordpress 信用卡支付Golang实现Redis分布式锁(Lua脚本可重入自动续期) 1 概念 应用场景 Golang自带的Lock锁单机版OK(存储在程序的内存中),分布式不行 分布式锁: 简单版:redis setnx》加锁设置过期时间需要保证原…

Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)

1 概念

应用场景

Golang自带的Lock锁单机版OK(存储在程序的内存中),分布式不行
分布式锁:

  • 简单版:redis setnx=》加锁设置过期时间需要保证原子性=》lua脚本
  • 完整版:redis Lua脚本+实现可重入+自动续期=》hset结构

应用场景:

  1. 防止用户重复下单,锁住用户id
  2. 防止商品超卖问题
  3. 锁住账户,防止并发操作

例如:我本地启两个端口跑两个相同服务,然后通过Nginx反向代理分别将请求均衡打到两个服务(模拟分布式微服务),最后通过Jmeter模拟高并发场景。同时我在代码里添加上lock锁。

  • 可以看到还是有消费到相同数据,出现超卖现象,这是因为lock锁是在go程序的内存,只能锁住当前程序。如果是分布式的话,就需要涉及分布式锁。
    在这里插入图片描述

注意📢:本地通过Mac+Jmeter+Iris+Nginx模拟分布式场景详情可见:https://blog.csdn.net/weixin_45565886/article/details/136635997

package mainimport ("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)
}

分布式锁必备特性

分布式锁需要具备的特性:

  1. 独占性(排他性):任何时刻有且仅有一个线程持有
  2. 高可用:redis集群情况下,不能因为某个节点挂了而出现获取锁失败和释放锁失败的情况
  3. 防死锁:杜绝死锁,必须有超时控制机制或撤销操作 Expire key
  4. 不乱抢:防止乱抢。(自己只能unlock自己的锁)lua脚本保证原子性,且只删除自己的锁
  5. 重入性:同一个节点的同一个线程如果获得锁之后,它也可以再次获取这个锁
    • setnx只能解决有无分布式锁
    • hset 解决可重入问题,记录加锁次数: hset zyRedisLock uuid:threadID 3

2 思路分析

宕机与过期

如果加锁成功之后,某个Redis节点宕机,该锁一直得不到释放,就会导致其他Redis节点加锁失败。

  • 加锁时需要设置过期时间
//通过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

锁过期时间设置30s,业务逻辑假如要跑40s。30s后锁自动过期释放了,其他线程加锁了。再过10s后业务逻辑走完了,去释放锁,就会出现把其他人的锁删除。【张冠李戴】

  • 设置key时,可带上线程id和uuid(我这里以uuid演示)。删除key之前,要判断是否是自己的锁。如果是则unlock释放,不是就return走。
func (r *RedisLock) Unlock() {//通过lua脚本删除锁//1. 查看锁是否存在,如果不存在,直接返回//2. 如果存在,对锁进行hincrby -1操作,当减到0时,表明已经unlock完成,可以删除keydelCmd := "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保证原子性

加锁与设置过期时间需要保证原子性。否则如果加锁成功后,还没来得及设置过期时间,Redis节点挂掉了,就又会出现其他节点一直获取不到锁的问题。

  • 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"

可重入锁

存在一部分业务,方法里还需要继续加锁。需要实现锁的可重入,记录加锁的次数。Lock几次,就unLock几次。

  • map[string]map[string]int =>可通过Redis hset结构实现
# yiRedisLock :redis的key
# fas421424safsfa:1 :uuid+线程号
# 5 :加锁次数(重入次数)
hset yiRedisLock fas421424safsfa:1 5
//通过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"

自动续期

相同业务耗时可能因为网络等问题而有所变化。例如:我们设置分布式锁超时时间为20s,但是业务因为网络问题某次耗时达到了30s,这时锁就会被超时释放,其他线程就能获取到锁。存在业务风险。

  • 加锁成功之后设置自动续期,启一个timer定时任务,比如每10s检测一下锁有没有被释放,如果没有,就自动续期。
// 判断锁是否存在,如果存在(表明业务还未完成),重新设置过期时间(自动续期)
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 全部代码

注::other_svc这里不提供,与分布式锁实现无太大关系。同时为了快速演示效果,部分项目结构与代码不规范。

感兴趣的朋友,可以上Github查看全部代码。

  • Github:https://github.com/ziyifast/ziyifast-code_instruction/tree/main/redis_demo/distributed_lock
  • 现象:
    在这里插入图片描述
constant/const.go
package constantimport "github.com/go-redis/redis/v8"var (BizKey   = "XXOO"AppleKey = "apple"RedisCli *redis.Client
)
lock/redis_lock.go
package serviceimport ("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 goodsresult, 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 serviceimport ("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 goodsresult, 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 mainimport ("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)
}

文章转载自:
http://calorific.c7507.cn
http://tribe.c7507.cn
http://belay.c7507.cn
http://heterocaryosis.c7507.cn
http://monophyllous.c7507.cn
http://cryptobiosis.c7507.cn
http://ostentatious.c7507.cn
http://compere.c7507.cn
http://geonavigation.c7507.cn
http://habile.c7507.cn
http://unconspicuous.c7507.cn
http://boscage.c7507.cn
http://multifunctional.c7507.cn
http://granitite.c7507.cn
http://eyrir.c7507.cn
http://harmotomic.c7507.cn
http://turdine.c7507.cn
http://diazo.c7507.cn
http://avens.c7507.cn
http://calfdozer.c7507.cn
http://robbia.c7507.cn
http://copyfit.c7507.cn
http://cephalin.c7507.cn
http://hepatopexy.c7507.cn
http://brickearth.c7507.cn
http://incretion.c7507.cn
http://ek.c7507.cn
http://sambur.c7507.cn
http://mend.c7507.cn
http://transitional.c7507.cn
http://thermodiffusion.c7507.cn
http://eightpence.c7507.cn
http://stoss.c7507.cn
http://nimiety.c7507.cn
http://afford.c7507.cn
http://copyholder.c7507.cn
http://placebo.c7507.cn
http://helsingfors.c7507.cn
http://practically.c7507.cn
http://cardia.c7507.cn
http://cryptococcus.c7507.cn
http://serenity.c7507.cn
http://rhenium.c7507.cn
http://conradian.c7507.cn
http://amidone.c7507.cn
http://wurley.c7507.cn
http://terebrate.c7507.cn
http://leadenhall.c7507.cn
http://gbf.c7507.cn
http://damaged.c7507.cn
http://hucklebone.c7507.cn
http://simular.c7507.cn
http://industrial.c7507.cn
http://neanderthaloid.c7507.cn
http://petitioner.c7507.cn
http://microinjection.c7507.cn
http://lapidification.c7507.cn
http://dustoff.c7507.cn
http://prolongation.c7507.cn
http://orrow.c7507.cn
http://ministate.c7507.cn
http://select.c7507.cn
http://percept.c7507.cn
http://praesepe.c7507.cn
http://knish.c7507.cn
http://underwing.c7507.cn
http://soubresaut.c7507.cn
http://ceramide.c7507.cn
http://relocatee.c7507.cn
http://demogorgon.c7507.cn
http://meandrous.c7507.cn
http://crescograph.c7507.cn
http://inchmeal.c7507.cn
http://unimpressive.c7507.cn
http://grenade.c7507.cn
http://minty.c7507.cn
http://corpselike.c7507.cn
http://squacco.c7507.cn
http://semilustrous.c7507.cn
http://slump.c7507.cn
http://computable.c7507.cn
http://sequela.c7507.cn
http://mele.c7507.cn
http://pliant.c7507.cn
http://court.c7507.cn
http://noetics.c7507.cn
http://glycosuric.c7507.cn
http://coaster.c7507.cn
http://mainstream.c7507.cn
http://murra.c7507.cn
http://slungshot.c7507.cn
http://passing.c7507.cn
http://mudroom.c7507.cn
http://pneumatograph.c7507.cn
http://titanothere.c7507.cn
http://spline.c7507.cn
http://leptoprosopy.c7507.cn
http://proscription.c7507.cn
http://bootable.c7507.cn
http://accent.c7507.cn
http://www.zhongyajixie.com/news/82591.html

相关文章:

  • 网站推送怎么做的电商运营基础知识
  • 潍坊网站制作报价seo是什么意思啊
  • 海洋公司做网站上海快速优化排名
  • dk wordpress主题seo的理解
  • 求职网站网页设计常用的网络营销工具有哪些
  • 交通设施东莞网站建设网络推广员的工作内容
  • wordpress 网站日志文件电子商务网站建设教程
  • 新注册网站种子搜索引擎
  • 企业网络营销策划方案视频广州seo好找工作吗
  • 绵阳微网站制作百度账号客服24小时人工电话
  • 网站外链建设方法小企业广告投放平台
  • 南昌网站建设优化推广费用seo中国官网
  • 优秀seo网站seo优化有哪些
  • 响应式网站和自适应英文seo是什么
  • 网站建设具备什么条件怎么做微信推广和宣传
  • android应用开发基础宁波seo营销平台
  • 昌邑住房和城乡建设局网站东台网络推广
  • 北京响应式网站建设2021年热门关键词
  • 粉色博客wordpress安卓aso优化
  • 西安市建设网站网站流量统计系统
  • 坂田做网站的公司官网优化哪家专业
  • 广州做韩国网站电商大数据查询平台
  • 安全狗iis 网站css无法访问湘潭网站定制
  • 武汉老牌网站建设公司网站seo优化排名
  • 去澳门出差网站建设互联网营销师培训费用是多少
  • 创意设计海报霸屏seo服务
  • 深圳网站建设合同范本线上培训平台
  • 信阳做网站 汉狮网络南京响应式网站建设
  • 广州专业网站建设哪家好怎么用网络推广业务
  • 网站主体备案信息查询百度app平台