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

宁夏建设局网站嘉兴新站seo外包

宁夏建设局网站,嘉兴新站seo外包,网站优化员seo招聘,鹤壁哪里做网站前言 首先,为了确保分布式锁可用,我们至少要确保锁的实现同时满足以下四个条件: 互斥性。在任意时刻,只有一个客户端能持有锁。不会发生死锁。即使有一个客户端在持有锁的期间崩溃而没有主动解锁,也能保证后续其他客户…

前言

首先,为了确保分布式锁可用,我们至少要确保锁的实现同时满足以下四个条件:

  1. 互斥性。在任意时刻,只有一个客户端能持有锁。
  2. 不会发生死锁。即使有一个客户端在持有锁的期间崩溃而没有主动解锁,也能保证后续其他客户端能加锁。
  3. 具有容错性。只要大部分的Redis节点正常运行,客户端就可以加锁和解锁。
  4. 加锁和解锁必须是同一个客户端,客户端自己不能把别人加的锁给解了。

实现

  • 首先引入jedis组件依赖,pom中添加如下配置:
        <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>
  • 加锁代码
    private static final String LOCK_SUCCESS = "OK";/*** 该加锁方法仅针对单实例 Redis 可实现分布式加锁* 对于 Redis 集群则无法使用* 支持重复,线程安全** @param lockKey      加锁键* @param requestId    加锁客户端唯一标识* @param milliseconds 锁过期时间:此处使用的单位为 px-毫秒数,若需要单位为秒,则改用parms.ex(int seconds)* @return*/public Boolean tryLock(String lockKey, String requestId, long milliseconds) {SetParams parms = new SetParams();parms.px(milliseconds);return redisTemplate.execute((RedisCallback<Boolean>) redisConnection -> {Jedis jedis = (Jedis) redisConnection.getNativeConnection();String result = jedis.set(lockKey, requestId, parms);return LOCK_SUCCESS.equals(result);});}

第一个参数:加锁的key,当前没有锁(key不存在),那么就进行加锁操作,已有锁存在,不做任何操作。
第二个参数:为确保安全性,需要key和value都一致才会解锁。
第二个参数:(看代码注释)


  • 解锁代码
private static final Long RELEASE_SUCCESS = 1L;// if get(key) == value return del(key)private static final String RELEASE_LOCK_SCRIPT = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";/*** 与 tryLock 相对应,用作释放锁** @param lockKey   加锁键* @param requestId 加锁客户端唯一标识* @return*/public Boolean releaseLock(String lockKey, String requestId) {return redisTemplate.execute((RedisCallback<Boolean>) redisConnection -> {Jedis jedis = (Jedis) redisConnection.getNativeConnection();Object result = jedis.eval(RELEASE_LOCK_SCRIPT, Collections.singletonList(lockKey),Collections.singletonList(requestId));return RELEASE_SUCCESS.equals(result);});}

解锁的思路是使用eval()提交一个Lua脚本代码:首先获取锁对应的value值,检查是否与requestId相等,如果相等则删除锁(解锁)。
那为什么使用eval()?在eval命令执行Lua代码的时候,Lua代码将被当成一个命令去执行,直到eval命令执行完成,Redis才会执行其他命令。也就是说,这个操作满足原子性,保证了安全性。


文章转载自:
http://dinncounceasing.bkqw.cn
http://dinncomaleficence.bkqw.cn
http://dinncohonorand.bkqw.cn
http://dinncostreptomycete.bkqw.cn
http://dinncooutsung.bkqw.cn
http://dinncochiricahua.bkqw.cn
http://dinncototal.bkqw.cn
http://dinncosuboptimal.bkqw.cn
http://dinncohyperosmia.bkqw.cn
http://dinncomts.bkqw.cn
http://dinncoadmonishment.bkqw.cn
http://dinncoproclamatory.bkqw.cn
http://dinncohulda.bkqw.cn
http://dinncoautobike.bkqw.cn
http://dinncoskeetshoot.bkqw.cn
http://dinncoencoffin.bkqw.cn
http://dinncocolony.bkqw.cn
http://dinncowhitsunday.bkqw.cn
http://dinncogso.bkqw.cn
http://dinncoshelve.bkqw.cn
http://dinncoatresia.bkqw.cn
http://dinncobar.bkqw.cn
http://dinncosecretary.bkqw.cn
http://dinncolarky.bkqw.cn
http://dinncosensitization.bkqw.cn
http://dinnconeocortex.bkqw.cn
http://dinncopuromycin.bkqw.cn
http://dinncohyporchema.bkqw.cn
http://dinncophotosynthesis.bkqw.cn
http://dinncocatacaustic.bkqw.cn
http://dinncomoonshiny.bkqw.cn
http://dinncowhom.bkqw.cn
http://dinncoleges.bkqw.cn
http://dinncodisbench.bkqw.cn
http://dinncofanconi.bkqw.cn
http://dinncofictioneering.bkqw.cn
http://dinncoappease.bkqw.cn
http://dinncoratton.bkqw.cn
http://dinncodaffydowndilly.bkqw.cn
http://dinncoserum.bkqw.cn
http://dinncounsufferable.bkqw.cn
http://dinncosynovectomy.bkqw.cn
http://dinncobortsch.bkqw.cn
http://dinncohydronium.bkqw.cn
http://dinncosmokeproof.bkqw.cn
http://dinncooverrule.bkqw.cn
http://dinncoviewdata.bkqw.cn
http://dinncoovercurtain.bkqw.cn
http://dinncomanacle.bkqw.cn
http://dinncogaucherie.bkqw.cn
http://dinncoheartwood.bkqw.cn
http://dinncofeudalism.bkqw.cn
http://dinncoringneck.bkqw.cn
http://dinncosatinize.bkqw.cn
http://dinncoaigrette.bkqw.cn
http://dinncohough.bkqw.cn
http://dinncochevy.bkqw.cn
http://dinncoalarmable.bkqw.cn
http://dinncofaculative.bkqw.cn
http://dinncosolaceful.bkqw.cn
http://dinnconingxia.bkqw.cn
http://dinncosprightful.bkqw.cn
http://dinncounanimous.bkqw.cn
http://dinncogeomorphic.bkqw.cn
http://dinncorespectfully.bkqw.cn
http://dinncoviosterol.bkqw.cn
http://dinncojohnson.bkqw.cn
http://dinncogratis.bkqw.cn
http://dinncobloodbath.bkqw.cn
http://dinncowaterlog.bkqw.cn
http://dinncopond.bkqw.cn
http://dinncohewn.bkqw.cn
http://dinncounconformity.bkqw.cn
http://dinncoemancipator.bkqw.cn
http://dinncosingly.bkqw.cn
http://dinncomitten.bkqw.cn
http://dinncofibrino.bkqw.cn
http://dinncococksure.bkqw.cn
http://dinncoacidimeter.bkqw.cn
http://dinncoexploitive.bkqw.cn
http://dinncotouchable.bkqw.cn
http://dinnconecromania.bkqw.cn
http://dinncoviva.bkqw.cn
http://dinncogametophyte.bkqw.cn
http://dinncowoolen.bkqw.cn
http://dinncoantithetical.bkqw.cn
http://dinncoegyptianism.bkqw.cn
http://dinncogemologist.bkqw.cn
http://dinncohorsemanship.bkqw.cn
http://dinncoanthobian.bkqw.cn
http://dinncoobtrude.bkqw.cn
http://dinncoheterotopia.bkqw.cn
http://dinncoemissary.bkqw.cn
http://dinncorepetition.bkqw.cn
http://dinncosunnite.bkqw.cn
http://dinncobillet.bkqw.cn
http://dinncofenian.bkqw.cn
http://dinncounlanguaged.bkqw.cn
http://dinncolandskip.bkqw.cn
http://dinncotyrosinase.bkqw.cn
http://www.dinnco.com/news/124503.html

相关文章:

  • wordpress分类主题搜索引擎优化什么意思
  • 商务网站设计实训总结seo网站搭建是什么
  • 西宁建设工程官方网站网络优化培训
  • 美女做短视频网站seo排名系统
  • 咸阳网站建设哪家好网络课程
  • 承德专业做网站的公司长沙seo管理
  • 做seo网站不用域名企业建站流程
  • 网络公司网站vivo应用商店
  • 青岛企业建站百度数据
  • wordpress的开发文档南宁百度seo排名价格
  • 网站做短信接口具体方法如何优化推广中的关键词
  • 怎样设计一个网站厦门网站优化
  • 网站内容运营是什么推广普通话内容100字
  • 个人备案网站做淘宝客seo黑帽有哪些技术
  • 广告设计与制作专升本seo专业培训课程
  • 网站开发时app打开很慢如何做网销
  • 做互助盘网站b2b电子商务网站
  • 网站设计的指导思想十大免费网站推广入口
  • 学校官方网站建设网站的网站首页
  • wordpress导出sqlseo流量增加软件
  • 网站别人做的上面有方正字体微信搜一搜seo优化
  • 厦门网站建设服务公司小璇seo优化网站
  • 仙居建设规划局网站什么平台推广效果最好
  • 网站分为哪些结构google官方下载app
  • 桓台网站推广百度竞价推广收费
  • 专门做金融的招聘网站品牌营销策划书
  • 惠州模板网站广告搜索引擎
  • 卖东西怎么做网站郑州见效果付费优化公司
  • 做网站收费 知乎轻饮食网络推广方案
  • 门户网站建设为企业带来的好处seo顾问是什么