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

长春电商网站建设费用cnzz数据统计

长春电商网站建设费用,cnzz数据统计,软件推广平台有哪些?哪个比较好,wordpress网站重做大技术使用Redisson使用Redisson在秒杀服务中有两个作用,一个是作为分布式锁来确保多个秒杀服务同时在线时同时上架秒杀商品,只允许有一个秒杀服务成功上架秒杀商品,其他的上架失败。第二个作用是作为分布式信号量,每个秒杀商品在…

大技术

使用Redisson

使用Redisson在秒杀服务中有两个作用,一个是作为分布式锁来确保多个秒杀服务同时在线时同时上架秒杀商品,只允许有一个秒杀服务成功上架秒杀商品,其他的上架失败。第二个作用是作为分布式信号量,每个秒杀商品在存到Redis中时都设置一个分布式信号量,把每个秒杀商品的数量作为信号量的值,这是为了防止秒杀的时候出现穿库的情况(就是只设置了3个秒杀数量,结果秒杀结束后秒杀数量是5个,这就亏本了)

1、导入依赖

<!-- 以后使用redisson作为分布式锁,分布式对象等功能框架 -->
<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.16.8</version>
</dependency>

2、设置Redission配置类

package com.saodai.saodaimall.saodaimall.seckill.config;import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.io.IOException;/**
* redission分布式锁配置类
*/
@Configuration
public class MyRedissonConfig {/*** 所有对Redisson的使用都是通过RedissonClient* @return* @throws IOException*/@Bean(destroyMethod="shutdown")public RedissonClient redisson() throws IOException {//1、创建配置Config config = new Config();//配置虚拟机的地址config.useSingleServer().setAddress("redis://192.168.241.128:6379");//2、根据Config创建出RedissonClient实例(单个实例)//Redis url should start with redis:// or rediss://RedissonClient redissonClient = Redisson.create(config);return redissonClient;}
}

3、Redission作为分布式锁

package com.saodai.saodaimall.saodaimall.seckill.scheduled;
import com.saodai.saodaimall.saodaimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;/**
* 秒杀商品定时上架
*  每天晚上3点,上架最近三天需要三天秒杀的商品
*  当天00:00:00 - 23:59:59
*  明天00:00:00 - 23:59:59
*  后天00:00:00 - 23:59:59
*/@Slf4j
@Service
public class SeckillScheduled @Autowiredprivate SeckillService seckillService;@Autowiredprivate RedissonClient redissonClient;//秒杀商品上架功能的锁private final String upload_lock = "seckill:upload:lock";/**保证幂等性问题**///     @Scheduled(cron = "*/5 * * * * ? ") //秒 分 时 日 月 周@Scheduled(cron = "0 0 1/1 * * ? ") public void uploadSeckillSkuLatest3Days() {//1、重复上架无需处理log.info("上架秒杀的商品...");//分布式锁RLock lock = redissonClient.getLock(upload_lock);try {//加锁(指定锁定时间为10s)lock.lock(10, TimeUnit.SECONDS);seckillService.uploadSeckillSkuLatest3Days();} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}
}

使用Redisson作为分布式锁是为了确保多个秒杀服务同时在线时同时上架秒杀商品,只允许有一个秒杀服务成功上架秒杀商品,其他的上架失败

4、Redission实现分布式信号量

作为分布式信号量,每个秒杀商品在存到Redis中时都设置一个分布式信号量,把每个秒杀商品的数量作为信号量的值,这是为了防止秒杀的时候出现穿库的情况(就是只设置了3个秒杀数量,结果秒杀结束后秒杀数量是5个,这就亏本了)

/*** 封装秒杀活动的关联商品信息到缓存里* @param sessions 秒杀活动信息*/private void saveSessionSkuInfo(List<SeckillSessionWithSkusVo> sessions) {if (sessions!=null){sessions.stream().forEach(session -> {//准备hash操作,绑定hash值seckill:skusBoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(SECKILL_CHARE_PREFIX);//遍历秒杀活动中的商品项(seckillSkuVo表示的就是每个遍历的商品项)session.getRelationSkus().stream().forEach(seckillSkuVo -> {//生成随机码String token = UUID.randomUUID().toString().replace("-", "");//查看redis中有没有这个key (秒杀场次id_秒杀商品id)String redisKey = seckillSkuVo.getPromotionSessionId().toString() + "-" + seckillSkuVo.getSkuId().toString();if (!operations.hasKey(redisKey)) {//缓存我们商品信息(SeckillSkuRedisTo是存入缓存中的对象)SeckillSkuRedisTo redisTo = new SeckillSkuRedisTo();Long skuId = seckillSkuVo.getSkuId();//1、先查询sku的基本信息,调用远程服务R info = productFeignService.getSkuInfo(skuId);if (info.getCode() == 0) {SkuInfoVo skuInfo = info.getData( "skuInfo",new TypeReference<SkuInfoVo>(){});redisTo.setSkuInfo(skuInfo);}//2、sku的秒杀信息BeanUtils.copyProperties(seckillSkuVo,redisTo);//3、设置当前商品的秒杀时间信息redisTo.setStartTime(session.getStartTime().getTime());redisTo.setEndTime(session.getEndTime().getTime());//4、设置商品的随机码(防止恶意攻击)redisTo.setRandomCode(token);//序列化json格式存入Redis中String seckillValue = JSON.toJSONString(redisTo);//秒杀活动的商品项的详细信息存入redis/**格式是key:4_47 value:SeckillSkuRedisTo对象的String类型**/operations.put(seckillSkuVo.getPromotionSessionId().toString() + "-" + seckillSkuVo.getSkuId().toString(),seckillValue);//如果当前这个场次的商品库存信息已经上架就不需要上架/**5、使用库存作为分布式Redisson信号量(限流)**///把每个秒杀商品的总数量作为信号量存入redis缓存,信号量标识seckill:stock:+随机(相当于key)RSemaphore semaphore = redissonClient.getSemaphore(SKU_STOCK_SEMAPHORE + token);/**库存的格式key:seckill:stock:5d1df46618d34f9f9808f25cda60ba01 value:秒杀商品的总数量 其中5d1df46618d34f9f9808f25cda60ba01是随机码**/semaphore.trySetPermits(seckillSkuVo.getSeckillCount());}});});}else {log.error("没有秒杀活动");}}
@Autowired
private RedissonClient redissonClient;/**5、使用库存作为分布式Redisson信号量(限流)**/
//获取分布式信号量,信号量名称为seckill:stock:+随机码
RSemaphore semaphore = redissonClient.getSemaphore(SKU_STOCK_SEMAPHORE + token);
// 秒杀商品的库存数量作为信号量的值(允许同时seckillSkuVo.getSeckillCount()个用户获取到信号量)
semaphore.trySetPermits(seckillSkuVo.getSeckillCount());

实现uploadSeckillSkuLatest3Days方法中saveSessionSkuInfo(封装秒杀活动的关联商品信息到缓存里,每一个Redis缓存中具体sku信息用的是HashMap结构),通过HashMap结构把每个秒杀商品的详细信息以下面的格式存到Redis中,然后通过Redisson实现分布式信号量来把秒杀商品的库存总数量作为信号量存入redis缓存,每一个Redis缓存中具体sku信息的格式如下:

  • hash值是seckill:skus key: 4_47 value: SeckillSkuRedisTo对象

  • 其中key的4表示秒杀的场次id,47表示秒杀商品的skuId,由于用的是hashMap结构,其中hash值是seckill:skus

  • Redission实现分布式信号量设置时就会把信号量以key-value的格式存到reids缓存中,Redis缓存中信号量信息的格式如下:

  • key: seckill:stock:随机码 value:每个秒杀商品的总数量

  • 其中key的seckill:stock是固定前缀,随机码就是随机成功的uuid值,把每个秒杀商品的总数量作为信号量的值

  • 准备hash操作,绑定seckill:skus关键字的hash

  • 遍历封装存入redis的秒杀活动的秒杀商品项

  • 生成随机码

  • 封装SeckillSkuRedisTo对象并序列化后存入redis缓存

  • 远程调用product商品服务

  • 使用Redission实现分布式信号量来把秒杀商品的库存总数量作为信号量存入redis缓存(限流)

  • 秒杀活动的商品项的详细信息存入redis缓存

  • 设置商品的随机码(防止恶意攻击)

  • 设置当前商品的秒杀时间信息

  • 封装秒杀活动中秒杀商品项信息

4、秒杀时具体实现

@Autowired
private RedissonClient redissonClient;//分布式锁
RSemaphore semaphore = redissonClient.getSemaphore(key);
//尝试快速拿到信号量,100毫秒没有用拿到就返回false
//在指定的时间内尝试地获取1个许可,如果获取不到就返回false
boolean semaphoreCount = semaphore.tryAcquire(num, 100, TimeUnit.MILLISECONDS);


文章转载自:
http://dinncothyrsus.tpps.cn
http://dinncodaredeviltry.tpps.cn
http://dinncoferrel.tpps.cn
http://dinncohight.tpps.cn
http://dinncobaudrons.tpps.cn
http://dinncoropemaking.tpps.cn
http://dinncoclassical.tpps.cn
http://dinncominicell.tpps.cn
http://dinncohydrogenise.tpps.cn
http://dinncoconge.tpps.cn
http://dinncohummock.tpps.cn
http://dinncoalarmism.tpps.cn
http://dinncochromiderosis.tpps.cn
http://dinncocraniometrical.tpps.cn
http://dinncosouthbound.tpps.cn
http://dinncoadespota.tpps.cn
http://dinncoephod.tpps.cn
http://dinncomicropublishing.tpps.cn
http://dinncoweathering.tpps.cn
http://dinncobarracuda.tpps.cn
http://dinncorechannel.tpps.cn
http://dinncoundertenant.tpps.cn
http://dinncomaidless.tpps.cn
http://dinncocutler.tpps.cn
http://dinncovertebrae.tpps.cn
http://dinncomicrotransmitter.tpps.cn
http://dinncoferetory.tpps.cn
http://dinncorubbings.tpps.cn
http://dinncocountersink.tpps.cn
http://dinncodraconic.tpps.cn
http://dinncodecathlete.tpps.cn
http://dinncoredeliver.tpps.cn
http://dinncoamalgamator.tpps.cn
http://dinncoanalcite.tpps.cn
http://dinncopediatry.tpps.cn
http://dinncosaucer.tpps.cn
http://dinncosynchronicity.tpps.cn
http://dinncostripline.tpps.cn
http://dinncofirstling.tpps.cn
http://dinncolatifolious.tpps.cn
http://dinncounderslept.tpps.cn
http://dinncohoopskirt.tpps.cn
http://dinncodysprosium.tpps.cn
http://dinncochurchlike.tpps.cn
http://dinncometacarpus.tpps.cn
http://dinncocounterfort.tpps.cn
http://dinncogynaecium.tpps.cn
http://dinncohickwall.tpps.cn
http://dinncoscrivener.tpps.cn
http://dinncokarakorum.tpps.cn
http://dinncoelliptically.tpps.cn
http://dinncoraysistor.tpps.cn
http://dinncowallop.tpps.cn
http://dinncouneath.tpps.cn
http://dinncoresemble.tpps.cn
http://dinncoeuphoria.tpps.cn
http://dinncopoppethead.tpps.cn
http://dinncodiphthongize.tpps.cn
http://dinncoonus.tpps.cn
http://dinncowonderfully.tpps.cn
http://dinncoburweed.tpps.cn
http://dinncoerumpent.tpps.cn
http://dinncovitellogenesis.tpps.cn
http://dinncohamstring.tpps.cn
http://dinncocharisma.tpps.cn
http://dinncobuckpassing.tpps.cn
http://dinncoamphisbaena.tpps.cn
http://dinncomishook.tpps.cn
http://dinncobiramose.tpps.cn
http://dinncobowie.tpps.cn
http://dinncoeditorship.tpps.cn
http://dinncovaluative.tpps.cn
http://dinncoorganogenesis.tpps.cn
http://dinncoboldhearted.tpps.cn
http://dinncoperistaltic.tpps.cn
http://dinncobarque.tpps.cn
http://dinncopredisposition.tpps.cn
http://dinncocaravansary.tpps.cn
http://dinncoaustronesia.tpps.cn
http://dinncospectrobolometer.tpps.cn
http://dinncohardenable.tpps.cn
http://dinncoopprobrious.tpps.cn
http://dinncorespectant.tpps.cn
http://dinncopenultima.tpps.cn
http://dinncosulphatase.tpps.cn
http://dinncoethnography.tpps.cn
http://dinncomedication.tpps.cn
http://dinncomegalocardia.tpps.cn
http://dinncogrundyism.tpps.cn
http://dinncobogus.tpps.cn
http://dinncoteresina.tpps.cn
http://dinncoexteriorly.tpps.cn
http://dinncoax.tpps.cn
http://dinncovet.tpps.cn
http://dinncobucktooth.tpps.cn
http://dinncolaura.tpps.cn
http://dinncosecular.tpps.cn
http://dinncobiophile.tpps.cn
http://dinncosalmagundi.tpps.cn
http://dinncounplantable.tpps.cn
http://www.dinnco.com/news/89829.html

相关文章:

  • 电商网站制作排名优化网站
  • 用cms创建自己带数据库的网站和在本机搭建网站运行平台的心得体会seo关键词大搜
  • vs2013做网站山东今日头条新闻
  • wordpress批量提交表单百度关键词怎么优化
  • 网站免费源码不用下载自助建站系统哪个好
  • 绵阳安州区做网站的有哪些百度学术官网首页
  • Wordpress竞拍长沙哪里有网站推广优化
  • 手机上怎么注册公司营业执照河北seo关键词排名优化
  • wordpress 作品集是什么seo内容优化是什么意思
  • 网站的创建历程怎么写关键字广告
  • 什么是php网站开发东莞网站自动化推广
  • 自己做的网站怎么传入外网镇江网络
  • 网站设计包括什么美国疫情最新情况
  • 大连企业网站设计2345网址导航电脑版官网
  • 网站做管制户外刀具数据分析系统
  • 厦门网站建设哪家好线下推广公司
  • 建设银行网站注册企业北京seo顾问外包
  • 创建网站服务器上海的重大新闻
  • 小型企业网站有哪些泰安网站优化公司
  • 泗洪县城乡建设局网站百度快速查询
  • 国内vps做网站要备案吗石家庄seo优化公司
  • 网站简介模板武汉seo排名扣费
  • b2b旅游网站建设代哥seo
  • 做外链哪个网站好it培训机构靠谱吗
  • h5自响应式网站模版推广引流渠道
  • 网站建站seo知识培训
  • 网上做批发有哪些网站有哪些厦门seo排名优化
  • 商场网站建设客户管理软件crm排名
  • 微信链接的微网站怎么做的策划营销推广方案
  • 网站永久镜像怎么做济南百度推广代理商