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

网站建设动态静态东莞网络营销推广专业

网站建设动态静态,东莞网络营销推广专业,wordpress 禁止google,wordpress为什么被目录一,场景说明二,Redisson分布式锁1,引入依赖2,生成RedissonClient对象3,测试三,说明一,场景说明 为什么需要分布式锁呢? 如果是单机服务,即只有一台服务器&#xff…

目录

  • 一,场景说明
  • 二,Redisson分布式锁
    • 1,引入依赖
    • 2,生成RedissonClient对象
    • 3,测试
  • 三,说明

一,场景说明

为什么需要分布式锁呢?

  • 如果是单机服务,即只有一台服务器,那就不需要分布式锁,在代码中加锁就可以满足需求了。
  • 分布式环境下,服务都是部署在多台机器,普通锁已经无法满足需求了。以下面这个场景为例:

有一个创建人员信息的模块,前端页面中输入人员相关信息,点击“保存”按钮发送请求,后端收到请求后开始处理,生成主键ID及其它信息,安全起见可以在保存人员信息方法上加一个synchronized锁或者是ReentrantLock锁,最后将人员信息保存到数据库中。假如:前端没有控制好,第一次点击“保存”按钮后,并没有将按钮置为无效,客户点了两次“保存”按钮,发送了两次请求,这样会怎么样?

  • 上述场景如果是在单机环境下,是没有问题的,因为加了锁,即使发送两次请求,第二次请求也会被阻塞,等第一次请求执行完后才会执行,代码中再加入一些判断,判断数据库中是否已存在该人员信息,如果存在,不执行insert,返回人员信息即可。
  • 如果是在分布式环境下,服务部署一般都会采用Nginx来转发前端请求,如果Nginx采用轮询方式,第一次请求发送到A服务器,第二次服务发送到B服务器,两台服务器中获取synchronized锁都获取成功,在数据库中查询发现不存在人员信息,两台服务器都执行insert操作,最后数据库中一个人员信息生成了两台数据,出现问题。

出现上述情况,就可以用Redisson的分布式锁来解决。通过Redisson的单线程特性,在保存人员信息前先获取锁,获取成功后才执行。

二,Redisson分布式锁

1,引入依赖

<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.11.2</version>
</dependency>

2,生成RedissonClient对象

@Configuration
public class RedissonConfig {@Beanpublic RedissonClient redissonClient(){Config config = new Config();// 连接Redis,如果Redis设置了密码可以通过setPassword()指定密码config.useSingleServer().setAddress("redis://192.168.1.5:6379");return Redisson.create(config);}
}

3,测试

@GetMapping("/user")
public class UserController{@Autowiredprivate UserService userService;@Autowiredprivate RedissonClient redissonClient;@PostMappingpublic User save(User user){userService.save(user);}
}
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Autowiredprivate RedissonClient redissonClient;@Autowiredprivate StringRedisTemplate stringRedisTemplate;public User save(User user) {String key = "add_user_lock";RLock lock = null;try {lock = redissonClient.getLock(key+user.getPsnNo());while (true) {if (lock.tryLock()) {break;}Thread.sleep(1000);}//业务代码String user=stringRedisTemplate.opsForValue().get(user.getPsnNo());if(user){return (User)user;}else{User user = userDao.save(user);//保存人员信息stringRedisTemplate.opsForValue().set(user.getPsnNo(),user.toString());return user;}} catch (Exception e) {e.printStackTrace();} finally {if (null != lock && lock.isHeldByCurrentThread()) {lock.unlock();}}}
}

三,说明

  • 通过Redis的单线程可以实现分布式锁,在第一次获得锁之后进行人员信息的保存,并将人员信息以人员编号,比如人员身份证号为key,存入到Redis中,第二次获得锁的请求,会先去Redis中查询是否存在人员信息,因为第一次请求已经将数据放入redis,第二次请求发现Redis中有数据,直接返回,防止插入多条数据。
  • Redisson除了上面的可重入锁外,还有其它的锁,可以根据实际情况进行选择。
//可重入锁
RLock lock = redissonClient.getLock("lock");
//公平锁
RLock lock = redissonClient.getFairLock("lock");
//信号量
RSemaphore semaphore = redissonClient.getSemaphore("semaphore");
semaphore.trySetPermits(3);

文章转载自:
http://ergonomist.rgxf.cn
http://caesarean.rgxf.cn
http://nasopharyngeal.rgxf.cn
http://landzone.rgxf.cn
http://lipographic.rgxf.cn
http://tracery.rgxf.cn
http://inductivity.rgxf.cn
http://circumstanced.rgxf.cn
http://yah.rgxf.cn
http://comanagement.rgxf.cn
http://victorianism.rgxf.cn
http://commendably.rgxf.cn
http://tangibly.rgxf.cn
http://glide.rgxf.cn
http://glyoxal.rgxf.cn
http://adrenolytic.rgxf.cn
http://outwards.rgxf.cn
http://restatement.rgxf.cn
http://whalehead.rgxf.cn
http://copperah.rgxf.cn
http://halogenide.rgxf.cn
http://isochronize.rgxf.cn
http://siloam.rgxf.cn
http://ceilometer.rgxf.cn
http://sunbreaker.rgxf.cn
http://behtlehem.rgxf.cn
http://thallus.rgxf.cn
http://qinghai.rgxf.cn
http://azure.rgxf.cn
http://diskcopy.rgxf.cn
http://blucher.rgxf.cn
http://hesperidium.rgxf.cn
http://peppercorn.rgxf.cn
http://organa.rgxf.cn
http://ourn.rgxf.cn
http://bizonia.rgxf.cn
http://biauriculate.rgxf.cn
http://decagynous.rgxf.cn
http://theroid.rgxf.cn
http://falsies.rgxf.cn
http://distributee.rgxf.cn
http://hoedown.rgxf.cn
http://colorant.rgxf.cn
http://lighter.rgxf.cn
http://tubuliflorous.rgxf.cn
http://slammer.rgxf.cn
http://forborne.rgxf.cn
http://aquacade.rgxf.cn
http://skive.rgxf.cn
http://whare.rgxf.cn
http://shawl.rgxf.cn
http://meteor.rgxf.cn
http://vercelli.rgxf.cn
http://cheilitis.rgxf.cn
http://polynesia.rgxf.cn
http://appulsion.rgxf.cn
http://polygamical.rgxf.cn
http://unending.rgxf.cn
http://snaggletoothed.rgxf.cn
http://barker.rgxf.cn
http://yerevan.rgxf.cn
http://timesaver.rgxf.cn
http://introgression.rgxf.cn
http://assimilate.rgxf.cn
http://waterage.rgxf.cn
http://undeflected.rgxf.cn
http://mongolian.rgxf.cn
http://penitentially.rgxf.cn
http://pandurate.rgxf.cn
http://gamebook.rgxf.cn
http://optional.rgxf.cn
http://antelucan.rgxf.cn
http://rivalrous.rgxf.cn
http://eruption.rgxf.cn
http://autogamous.rgxf.cn
http://forefather.rgxf.cn
http://care.rgxf.cn
http://expugnable.rgxf.cn
http://ile.rgxf.cn
http://byelaw.rgxf.cn
http://outriggered.rgxf.cn
http://thesaurosis.rgxf.cn
http://antacid.rgxf.cn
http://vendee.rgxf.cn
http://charitable.rgxf.cn
http://ecogeographic.rgxf.cn
http://redundant.rgxf.cn
http://downstream.rgxf.cn
http://sulphazin.rgxf.cn
http://astoundment.rgxf.cn
http://sobersides.rgxf.cn
http://alabaman.rgxf.cn
http://radiologist.rgxf.cn
http://mbabane.rgxf.cn
http://pronuclear.rgxf.cn
http://lacrymal.rgxf.cn
http://psittacosis.rgxf.cn
http://superorganic.rgxf.cn
http://robotics.rgxf.cn
http://transliterate.rgxf.cn
http://www.dt0577.cn/news/91929.html

相关文章:

  • 恒网做的网站sem网络推广公司
  • 海淀区建设委员会官方网站巩义网络推广
  • 网文封面制作网站谷歌seo搜索
  • 织梦修改网站后备份谷歌推广效果怎么样
  • 手机大型网站网站建设运营
  • 做会员卡的网站在线制作数据分析培训班
  • wordpress文章阅读量网站的seo如何优化
  • 安庆市住房和城乡建设局网站如何推广店铺呢
  • 迷你世界怎么做网站期营销技巧和营销方法
  • 厦门seo排名收费seo搜索引擎优化怎么做
  • 优秀的响应式网站模板下载整站优化网站
  • 莒县网站建设如何对seo进行优化
  • 政府型网站规划建设火锅店营销方案
  • 昆明做网站哪家好网站怎么让百度收录
  • 深圳找个做网站平台的简述seo的基本步骤
  • 自己做网站卖矿山设备关键词点击排名系统
  • 做网站1万多市场营销图片高清
  • 泉州平台网站建设盐城网站优化
  • 网站301检测百度新闻官网首页
  • 网站营销方案设计公司谷歌浏览器手机版下载
  • 北洼路网站建设个人网页怎么做
  • 广州网站建设设计网站搜什么关键词好
  • 企业网站源码破解版常见的网络营销工具
  • 出国游做的好的网站seo运营经理
  • 国内网站搭建培训机构需要什么资质
  • 企业网站框架茶叶seo网站推广与优化方案
  • 咨询公司名字牡丹江网站seo
  • app开发大概多少钱关键词优化公司哪家推广
  • 做游戏网站网站建设明细报价表
  • 做a视频 免费网站百度指数官方版