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

查询网站备案服务商网络舆情监测 toom

查询网站备案服务商,网络舆情监测 toom,ui界面图片,张家界网络文章目录 前言一、秒杀系统的设计二、缓存预热1.缓存结构设计2、上架 三、秒杀业务实现 前言 本篇基于谷粒商城的秒杀服务#xff0c;介绍设计一个秒杀系统的要素#xff0c;包括缓存预热、商品随机码、动静分离、消息队列削峰等。对应视频P311-P325#xff08;只介绍系统设… 文章目录 前言一、秒杀系统的设计二、缓存预热1.缓存结构设计2、上架 三、秒杀业务实现 前言 本篇基于谷粒商城的秒杀服务介绍设计一个秒杀系统的要素包括缓存预热、商品随机码、动静分离、消息队列削峰等。对应视频P311-P325只介绍系统设计和后端代码的关键部分 一、秒杀系统的设计 对于短时间内高并发的秒杀场景在系统的架构方面首先应该做到服务自治。即拆分一个专门的微服务去应对秒杀相关的业务请求具体创建订单扣减库存支付可以远程调用其他服务。这样做的目的是为了即使秒杀服务扛不住压力崩溃了也不会对其他的服务造成影响也是单一职责的体现。   其次在安全方面需要对秒杀的链接进行加密或为每一个秒杀的商品生成随机码用户请求时不仅需要带着商品id还需要加上随机码防止恶意攻击以及在网关层识别非法攻击请求并且拦截。   在流量控制方面可以使用验证码等手段进行流量分担用户输入验证码的速度有快有慢以及引入消息队列只将请求的关键信息放入消息队列然后返回给用户提示信息让队列自己去消费。最后还应该做好熔断降级。   除了上述几点为了提高系统的响应速度还需要进行缓存预热将秒杀的商品信息场次信息库存信息提前存入Redis中避免大量的请求全部访问数据库以及Nginx做好动静分离。   附一个使用AES实现链接加密的简单案例   AES加密工具类: import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64;public class AesEncryptionUtil {private static final String ALGORITHM AES;public static String encrypt(String data, String secret) throws Exception {SecretKeySpec key new SecretKeySpec(secret.getBytes(), ALGORITHM);Cipher cipher Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, key);byte[] encrypted cipher.doFinal(data.getBytes());return Base64.getEncoder().encodeToString(encrypted);}public static String decrypt(String encryptedData, String secret) throws Exception {SecretKeySpec key new SecretKeySpec(secret.getBytes(), ALGORITHM);Cipher cipher Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, key);byte[] decrypted cipher.doFinal(Base64.getDecoder().decode(encryptedData));return new String(decrypted);} } Controller: import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;RestController public class ResourceController {private static final String SECRET_KEY 1234567890123456; // 16位密钥GetMapping(/encrypt)public String encrypt(RequestParam String id) {try {String encryptedId AesEncryptionUtil.encrypt(id, SECRET_KEY);return Encrypted ID: encryptedId;} catch (Exception e) {e.printStackTrace();return Error encrypting ID;}}GetMapping(/resource)public String getResource(RequestParam String id) {try {String decryptedId AesEncryptionUtil.decrypt(id, SECRET_KEY);return Resource ID: decryptedId;} catch (Exception e) {e.printStackTrace();return Error decrypting ID;}} } 在访问资源接口/resource前首先访问/encrypt连接获取加密ID http://localhost:8080/encrypt?id123前端保存加密后的ID带着这个ID去访问资源接口 http://localhost:8080/resource?idencryptedId二、缓存预热 1.缓存结构设计 在本项目中选择将秒杀场次、库存量、商品信息进行缓存预热 秒杀场次设计为List结构key是开始事件的毫秒值_结束时间的毫秒值value是场次_skuId。库存量设计为String结构key是固定前缀:随机码value则是具体的库存。商品信息设计为hash结构key是场次_skuIdvalue则是具体商品信息的对象。 2、上架 本项目中使用缓存预热的方式是定时任务提前将今明后三天的秒杀信息放入缓存并且设计上使用了双检锁模式。在定时任务执行处使用分布式缓存锁防止多实例同时运行并且在执行相关业务代码的时候再次进行了判断如果缓存中已经有了对应的key则不再重复向Redis中保存。限流也使用了Redisson中的semaphore防止并发问题。 // 每天凌晨 3 点执行Scheduled(cron 0 0 3 * * *)public void executeTaskAt3AMUpSeckillSku() {//加分布式锁防止多实例重复执行RLock lock redissonClient.getLock(UPLOAD_LOCK);try {lock.lock();secKillSkuService.uploadSecKillSkuInfo();} finally {lock.unlock();}}Overridepublic void uploadSecKillSkuInfo() {ListSeckillSessionPojo lasted3SeckillInfo couponRemoteServiceClient.getLasted3SeckillInfo();if (!CollectionUtils.isEmpty(lasted3SeckillInfo)) {//将活动信息进行缓存 key:前缀开始事件_结束时间 value SkuIdthis.saveSessionInfos(lasted3SeckillInfo);//保存商品信息 key:前缀 value 商品信息this.saveSessionSkuInfos(lasted3SeckillInfo);}}private void saveSessionInfos(ListSeckillSessionPojo lasted3SeckillInfo) {lasted3SeckillInfo.forEach(seckillSessionPojo - {long start seckillSessionPojo.getStartTime().getTime();long end seckillSessionPojo.getEndTime().getTime();String key SESSION_CACHE_PREFIX start _ end;Boolean hasKey stringRedisTemplate.hasKey(key);if (!hasKey) {//活动场次id_商品idListString ids seckillSessionPojo.getSkuRelationEntities().stream().map(seckillSkuRelationPojo -seckillSkuRelationPojo.getPromotionSessionId().toString()_seckillSkuRelationPojo.getSkuId().toString()).collect(Collectors.toList());stringRedisTemplate.opsForList().leftPushAll(key, ids);}});}private void saveSessionSkuInfos(ListSeckillSessionPojo lasted3SeckillInfo) {lasted3SeckillInfo.forEach(seckillSessionPojo - {//准备hash操作一个活动场次一个hash操作BoundHashOperationsString, Object, Object operations stringRedisTemplate.boundHashOps(SKUKILL_CACHE_PREFIX);ListSeckillSkuRelationEntity relations seckillSessionPojo.getSkuRelationEntities();relations.forEach(relation - {String token UUID.randomUUID().toString().replace(-, );if (Boolean.FALSE.equals(operations.hasKey(relation.getPromotionSessionId().toString()_relation.getSkuId().toString()))) {//缓存商品一个活动场次对应的具体商品SecKillSkuRedisTO secKillSkuRedisTO new SecKillSkuRedisTO();BeanUtils.copyProperties(relation, secKillSkuRedisTO);//还应该设置商品详细信息 远程调用product服务SkuInfoPojo skuInfo null;try {skuInfo productRemoteServiceClient.getSkuInfo(relation.getSkuId());} catch (Exception e) {log.info(根据skuId{}查询商品服务错误, relation.getSkuId(), e);}secKillSkuRedisTO.setSkuInfo(skuInfo);//设置商品的开始事件和结束时间secKillSkuRedisTO.setStartTime(seckillSessionPojo.getStartTime().getTime());secKillSkuRedisTO.setEndTime(seckillSessionPojo.getEndTime().getTime());//设置随机码secKillSkuRedisTO.setRandomCode(token);String result JSON.toJSONString(secKillSkuRedisTO);operations.put(relation.getPromotionSessionId().toString()_relation.getSkuId().toString(), result);//限流 相比较于固定的skuId每次的随机码都不一样RSemaphore semaphore redissonClient.getSemaphore(SKU_STOCK_SEMAPHORE token);//商品可以秒杀的总量作为信号量semaphore.trySetPermits(relation.getSeckillCount().intValue());}});});}三、秒杀业务实现 在秒杀业务的具体实现上 在拦截器中判断用户是否登录。进行场次判断是否在秒杀时间段中。参数中的商品随机码和场次_skuId是否与缓存中预热的一致。校验用户是否已经参加过该场次该商品的秒杀使用Redis的setNX命令。从信号量中扣去库存尝试扣去库存使用有超时时间的获取超过时间获取不到就自己放弃不会死等。向Rabbit MQ发送消息订单服务监听消费消息进行订单创建。 Overridepublic String kill(String killId, String key, Integer num) {MemberRespVO memberRespVO LoginInterceptor.threadLocal.get();Long userId memberRespVO.getId();String timeId IdWorker.getTimeId();//首先校验用户是否登录在拦截器中已经实现//校验信息是否合法BoundHashOperationsString, Object, Object operations stringRedisTemplate.boundHashOps(SKUKILL_CACHE_PREFIX);//获取killId的场次信息String json (String) operations.get(killId);if (!StringUtils.isBlank(json)){SecKillSkuRedisTO secKillSkuRedisTO JSON.parseObject(json, SecKillSkuRedisTO.class);Long startTime secKillSkuRedisTO.getStartTime();Long endTime secKillSkuRedisTO.getEndTime();long time new Date().getTime();//校验时间if (startTime time || endTime time) {return null;}String randomCode secKillSkuRedisTO.getRandomCode();Long promotionSessionId secKillSkuRedisTO.getPromotionSessionId();Long skuId secKillSkuRedisTO.getSkuId();//校验参数中的随机码和场次_skuId与redis中的是否一致if (!key.equals(randomCode) || !killId.equals(promotionSessionId_skuId)) {return null;}//校验该用户是否已经秒杀过String userKey new StringBuffer().append(userId).append(_).append(promotionSessionId).append(_).append(skuId).toString();//setIfAbsent 只有不存在才会创建Boolean aBoolean stringRedisTemplate.opsForValue().setIfAbsent(userKey, num.toString(), 100, TimeUnit.MILLISECONDS);if (!aBoolean) {return null;}//扣减库存RSemaphore semaphore redissonClient.getSemaphore(SKU_STOCK_SEMAPHORE randomCode);try {//利用有超时时间的获取超过时间获取不到就自己放弃不会死等boolean b semaphore.tryAcquire(num, 100, TimeUnit.MILLISECONDS);if (!b){return null;}//向rabbitMQ发消息创建订单SecKillRabbitTO secKillRabbitTO new SecKillRabbitTO();secKillRabbitTO.setMemberId(userId);secKillRabbitTO.setNum(num);secKillRabbitTO.setPromotionSessionId(promotionSessionId);secKillRabbitTO.setSkuId(skuId);secKillRabbitTO.setOrderNo(timeId);rabbitTemplate.convertAndSend(order-event-exchange,order.seckill.order,secKillRabbitTO);} catch (InterruptedException e) {return null;}}return timeId;}
http://www.hkea.cn/news/14584643/

相关文章:

  • 涿州网站网站建设手机app开发技术
  • 山东网站排名优化公司什么是网站关键字优化
  • 网站开发常用的技术个人主页类网站开发背景
  • 中华智能自建代理网站福州制作网站提供商
  • 平面设计网站制作开发者选项
  • 枣庄市建设项目环评备案网站学校网站建设工作目标
  • php网站开发实训指导书学校建设微网站的方案设计
  • 自己做的旅游网站简介做网站的域名
  • 全国新农村建设中心网站有网打不开网页咋回事
  • 做网站优化如何遍文章深圳p2p网站建设
  • 开原网站制作公司cms 网站后台内容管理系统模板
  • 伊春网站推广华为vi设计手册ppt
  • 盐城网站优化推广工作室突发 佛山出大事
  • 网站建设的市场调研免费培训学校网站源码
  • 找个人给我做电影网站好北京网站建设哪家好
  • 网站建设公司的客户一级造价工程师
  • 什么网站可以接单做海报网站备案系统登录
  • 关于静态网站开发相关新闻seo课程哪个好
  • 自己设置免费网站设计平台外链网站分类
  • 广东购物网站建设价格台州品牌网站建设
  • 电子商务网站接口费率展厅公司
  • 公司网站建设详细方案淘宝客网站怎么做的人少了
  • 网站新闻被百度收录京东网站建设的详细策划
  • 上海做网站的公司有哪些东莞营销网站建设哪个平台好
  • 做视频大赛推广的网站南宁百度网站建设
  • 传统网站建设团队网页制作可以用手机吗
  • 高密 网站建设科技术语
  • 怎么建设自己产品网站南宁建站方案
  • 镇平微网站开发网页版式设计分析
  • 网站建设问题怎么设计logo图片