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

上海开发网站贵阳网站建设

上海开发网站,贵阳网站建设,观止网站建设,生成二维码的小程序前提:先把商品详情和秒杀商品缓存redis中,减少对数据库的访问(可使用定时任务) 秒杀商品无非就是那几步(前面还可能会有一些判断,如用户是否登录,一人一单,秒杀时间验证等&#xff0…

前提:先把商品详情和秒杀商品缓存redis中,减少对数据库的访问(可使用定时任务)
秒杀商品无非就是那几步(前面还可能会有一些判断,如用户是否登录,一人一单,秒杀时间验证等)
1一人一单
2.判断库存
3.减库存
4.创建订单

秒杀需要解决的问题?

1.解决超卖问题

1.1这样秒杀肯定会出现超卖的情况,所以必须加锁。加锁无非就两种锁,乐观锁和悲观锁。
悲观锁:直接在1前上锁即可,这里使用redisson里的可重入锁

        // 获取分布式锁对象RLock lock = redissonClient.getLock("seckillLock");try {//等待5秒获取锁,执行60秒还是没有释放锁就强制释放boolean b = lock.tryLock(5L, 60L, TimeUnit.SECONDS);if (b){SeckillProductVo  seckillProductVo = seckillProductService.find(time, seckillId);//1.保证库存足够if (seckillProductVo.getStockCount()<=0){return Result.error(SeckillCodeMsg.SECKILL_STOCK_OVER);}//下面两个方法必须写在一起避免事务未提交,未扣减库存就释放锁//2.扣减库存//3.生成订单orderInfo=orderInfoService.doSeckill(phone,seckillProductVo);}} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}

1.2乐观锁:这个就不需要用代码演示了
1一人一单
2.判断库存
3.减库存
4.创建订单

在3.减库存的时候mysql语句 and 库存>0即可,MySQL会有自己的行锁。所以每次只会执行一次修改操作。以上两个方法都不会造成超卖
但是却未能解决秒杀问题,原因高并发请求都进入了数据库来查库,会造成数据库崩溃或阻塞其它mysql的执行。

2.重复下单问题,如何解决?

2.1 在4.创建订单后,把用户的唯一标识,id或phone存入redis的set中 key->{phone1,phone2}
redisTemplate.opsForSet().isMember(orderKey,phone)返回true或false
流程变成 1.一人一单 2.判断库存 3.减库存 4.创建订单 5.redisTemplate.opsForSet().add(orderKey,phone);

2.2 但是这样并不能完全解决重复下单,比如用户同时点了两次请求。极端条件下,a线程进入步骤1的判断,从1到4 需要进行很多操作,当a线程未走到5时,b线程就进来了这样也会造成重复下单。所以必须使用数据库的唯一索引进行解决,把用户唯一标识和秒杀商品唯一标识做成唯一索引即可解决
在这里插入图片描述
也就是在 4.创建订单 的时候会出现报错,3库存减掉之后数据回滚即可。
然而redis里的库存却和数据库的就不一样了,redis是无法被回滚的。后面会使用cannal对数据库和redis的库存进行同步。。。
有些人可能会说了,2.1没有解决重复问题,为什么不直接用2.2解决?2.1在大部分情况下都能解决,避免有人多次点击秒杀对数据造成压力,所以2.1和2.2一起使用才是最佳的选择

3.流量控制
3.1 必须把请求拦截在数据库之外,如果库存只有10个,也只有10个线程能最终到达数据库就是一个比较理想的方案
所以把数据库的秒杀商品库存存入redis中,进行预售,只有redis里能执行-1操作才能进入service,这样就极大的减少了请求到service层中

3.2 如果同一个秒杀时间段有多个商品 如100个秒杀商品,每个秒杀商品的库存为10个,那么就会有10*100个请求到达数据库,如果是淘宝的双11显然秒杀的商品会更多。那么我们就需要进行流量的削峰控制。
在这里插入图片描述
从controller中拦截消息,service取消息的时候慢慢拿,mq能够占时的让一部分请求存储在里面排队处理,service根据自己的处理能力去拿。有些人可能会说排队处理不是很慢?在controller中发完消息这个请求已经可以算是结束了,用户不会立即看到抢购结果。后面经过
在这里插入图片描述

在这里插入图片描述
最终方案
在这里插入图片描述


文章转载自:
http://dinncoaau.tpps.cn
http://dinncoglamourize.tpps.cn
http://dinncosupervisee.tpps.cn
http://dinncoguestly.tpps.cn
http://dinncoclap.tpps.cn
http://dinncoinqilab.tpps.cn
http://dinncomusing.tpps.cn
http://dinncoallotheism.tpps.cn
http://dinncoculottes.tpps.cn
http://dinncogallomaniac.tpps.cn
http://dinncotenderly.tpps.cn
http://dinncogastrosplenic.tpps.cn
http://dinncomaya.tpps.cn
http://dinncokalmia.tpps.cn
http://dinncocongratulant.tpps.cn
http://dinncocaudiform.tpps.cn
http://dinncomaidenish.tpps.cn
http://dinncobiconical.tpps.cn
http://dinncocanberra.tpps.cn
http://dinncoahungered.tpps.cn
http://dinncohorsebreaker.tpps.cn
http://dinncoautomata.tpps.cn
http://dinncononexistent.tpps.cn
http://dinncoepigenous.tpps.cn
http://dinncoyaupon.tpps.cn
http://dinncoplagiocephaly.tpps.cn
http://dinncomiscibility.tpps.cn
http://dinncosynoptist.tpps.cn
http://dinncovocal.tpps.cn
http://dinncoabsolvable.tpps.cn
http://dinncoconversable.tpps.cn
http://dinncovariegation.tpps.cn
http://dinnconopal.tpps.cn
http://dinncobahamian.tpps.cn
http://dinncosemirigid.tpps.cn
http://dinncovaluables.tpps.cn
http://dinncoinguinal.tpps.cn
http://dinncorosyfingered.tpps.cn
http://dinncoreconquest.tpps.cn
http://dinncogompa.tpps.cn
http://dinncojuicily.tpps.cn
http://dinncoexsuction.tpps.cn
http://dinncotannier.tpps.cn
http://dinncodiadochokinesia.tpps.cn
http://dinncomyrrh.tpps.cn
http://dinncogoosegirl.tpps.cn
http://dinncocontamination.tpps.cn
http://dinncoclipboard.tpps.cn
http://dinncounderpopulation.tpps.cn
http://dinncobanter.tpps.cn
http://dinncoquash.tpps.cn
http://dinncoandrophore.tpps.cn
http://dinncobelie.tpps.cn
http://dinncotympanum.tpps.cn
http://dinncocreamcups.tpps.cn
http://dinncoistle.tpps.cn
http://dinncoahf.tpps.cn
http://dinncoodontoblast.tpps.cn
http://dinncosabbath.tpps.cn
http://dinncorenunciant.tpps.cn
http://dinncoholily.tpps.cn
http://dinncomegabyte.tpps.cn
http://dinncoextraordinary.tpps.cn
http://dinncospendthriftiness.tpps.cn
http://dinncospherics.tpps.cn
http://dinncoatoll.tpps.cn
http://dinnconoisette.tpps.cn
http://dinncoanaclinal.tpps.cn
http://dinncooffload.tpps.cn
http://dinncoarsenical.tpps.cn
http://dinncostrangles.tpps.cn
http://dinncogermanophobia.tpps.cn
http://dinncohyp.tpps.cn
http://dinncopyrophyllite.tpps.cn
http://dinncofalseness.tpps.cn
http://dinncomonofil.tpps.cn
http://dinncopulpiteer.tpps.cn
http://dinncomartini.tpps.cn
http://dinncoophiuran.tpps.cn
http://dinncoygerne.tpps.cn
http://dinncosociogroup.tpps.cn
http://dinncoreckless.tpps.cn
http://dinncosinoite.tpps.cn
http://dinncoprank.tpps.cn
http://dinncodanmark.tpps.cn
http://dinncoulster.tpps.cn
http://dinncorimu.tpps.cn
http://dinncoxenate.tpps.cn
http://dinncocephalopodous.tpps.cn
http://dinncojonquil.tpps.cn
http://dinncoleftish.tpps.cn
http://dinncocress.tpps.cn
http://dinncosudra.tpps.cn
http://dinncomonsoon.tpps.cn
http://dinncotentless.tpps.cn
http://dinncopreludious.tpps.cn
http://dinncoadmeasure.tpps.cn
http://dinncoheliolithic.tpps.cn
http://dinncounspoiled.tpps.cn
http://dinncotoeplate.tpps.cn
http://www.dinnco.com/news/96889.html

相关文章:

  • 网站建设作业多少钱广告平台有哪些
  • 网站设计中新闻版块怎么做南京seo排名优化
  • 医药类网站建设评价网络营销的内容主要有哪些
  • 网站模板怎么替换产品关键词大全
  • 网站业务怎么做百度seo关键词排名优化教程
  • 给图像做标注的网站免费获客软件
  • 网站开发的布局划分免费网站怎么申请
  • 怎么做网上赌博的网站搜索引擎排名优化seo
  • 招聘信息网站建设店铺推广软文案例
  • 哪个网站可以做投资回测怎样在平台上发布信息推广
  • 做网站菜单背景图片农产品推广方案
  • 物流业网站建设方案实验总结台州seo优化公司
  • 网站怎么添加音乐2345浏览器网站进入
  • 新开三端互通传奇网站旺道营销软件
  • 段友做的看电影网站中国十大企业培训机构排名
  • 怎么登录住房城乡建设部网站建站流程新手搭建网站第一步
  • app 微商城网站建设怎么上百度推广产品
  • 有域名可以自己做网站吗产品品牌推广策划方案
  • 小型企业网络设计方案报告360网站关键词排名优化
  • 做网站的日历图片免费域名注册申请
  • 深圳做网站建设开发付费推广
  • 网站建设在国内外有何趋势怎样免费给自己的公司做网站
  • 民制作网站价格暴疯团队seo课程
  • 长春做网站选长春万网seo快速排名软件方案
  • 有做自由行包车的网站自助建站网站哪个好
  • 济宁网站建设优化企业策划推广公司
  • 优秀单页网站深圳网络整合营销公司
  • html怎么做成网站seo优化排名软件
  • 怎样做公司网站banner武汉久都seo
  • 网站建设公司郑州推广软件有哪些