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

深圳网站建设公司那家好网站优化设计的基础是网站基本要素及每个细节的优化

深圳网站建设公司那家好,网站优化设计的基础是网站基本要素及每个细节的优化,移动互联网开发专业就业方向,青岛网络推广方案集群分布式场景高并发 1.negix配置代理和路由 高并发场景超卖问题 1.使用原生redis控制超卖时(若是商品,则可以将商品id作为锁对象),会遇到的问题 问题一:若直接使用:将获取锁的对象和设置的超时的时间分开,则不能控…

集群分布式场景高并发

1.negix配置代理和路由

高并发场景超卖问题

1.使用原生redis控制超卖时(若是商品,则可以将商品id作为锁对象),会遇到的问题

问题一:若直接使用:将获取锁的对象和设置的超时的时间分开,则不能控制原子性,如下所示

         Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "zhuge");
        stringRedisTemplate.expire(lockKey, 10, TimeUnit.SECONDS);

 问题二:若直接使用:将获取锁的对象和设置的超时的时间放在一个原子操作里执行时,在临界条件下,当程序执行到最后准备释放锁时候,锁的超时时间已到,则此时的锁成为已过期,则释放不了锁而当下一个线程也来执行任务时,前一个任务将这个任务所拿的所给释放掉了(释放掉不属于自己的锁对象);则引入redisson分布式锁来解决当前的问题,redisson具有锁续命机制

@RestController
public class IndexController {@Autowiredprivate Redisson redisson;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedisTemplate redisTemplate;@RequestMapping("/deduct_stock")public String deductStock() {String lockKey = "lock:product_101";//Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "zhuge");//stringRedisTemplate.expire(lockKey, 10, TimeUnit.SECONDS);String clientId = UUID.randomUUID().toString();Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.SECONDS); //jedis.setnx(k,v)if (!result) {return "error_code";}try {int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock")); // jedis.get("stock")if (stock > 0) {int realStock = stock - 1;stringRedisTemplate.opsForValue().set("stock", realStock + ""); // jedis.set(key,value)System.out.println("扣减成功,剩余库存:" + realStock);} else {System.out.println("扣减失败,库存不足");}} finally {if (clientId.equals(stringRedisTemplate.opsForValue().get(lockKey))) {stringRedisTemplate.delete(lockKey);}}return "end";}

使用分布式锁redisson

redisson使用

引入对应的redission的jar包

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

 设置redission配置


@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Beanpublic Redisson redisson() {// 此为单机模式Config config = new Config();config.useSingleServer().setAddress("redis://localhost:6379").setDatabase(0);return (Redisson) Redisson.create(config);}}

 redission的基本使用


@RestController
public class IndexController {@Autowiredprivate Redisson redisson;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedisTemplate redisTemplate;@RequestMapping("/deduct_stock")public String deductStock() {String lockKey = "lock:product_101";//Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "zhuge");//stringRedisTemplate.expire(lockKey, 10, TimeUnit.SECONDS);/*String clientId = UUID.randomUUID().toString();Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.SECONDS); //jedis.setnx(k,v)if (!result) {return "error_code";}*///获取锁对象RLock redissonLock = redisson.getLock(lockKey);//加分布式锁redissonLock.lock();  //  .setIfAbsent(lockKey, clientId, 30, TimeUnit.SECONDS);try {int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock")); // jedis.get("stock")if (stock > 0) {int realStock = stock - 1;stringRedisTemplate.opsForValue().set("stock", realStock + ""); // jedis.set(key,value)System.out.println("扣减成功,剩余库存:" + realStock);} else {System.out.println("扣减失败,库存不足");}} finally {/*if (clientId.equals(stringRedisTemplate.opsForValue().get(lockKey))) {stringRedisTemplate.delete(lockKey);}*///解锁redissonLock.unlock();}return "end";}

Redission执行的逻辑流程

 Redission分布式锁加锁源码分析

        redissonLock.lock(); 

加锁 

 @Overridepublic void lockInterruptibly() throws InterruptedException {lockInterruptibly(-1, null);}

执行 lockInterruptibly加锁逻辑

@Overridepublic void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException {long threadId = Thread.currentThread().getId();Long ttl = tryAcquire(leaseTime, unit, threadId); //尝试去加锁,返回的时加锁后的过期时间// lock acquiredif (ttl == null) {  //若ttl为null ,则表示加锁成功 ;若ttl不为null ,则往下走return;}RFuture<RedissonLockEntry> future = subscribe(threadId); //发布订阅,订阅前者执行的任务若提前执行完,则唤醒机制,去重新获取锁commandExecutor.syncSubscription(future);try {while (true) { //进入循环ttl = tryAcquire(leaseTime, unit, threadId); //再次尝试获取锁,返回加锁成功后的过期时间// lock acquiredif (ttl == null) {  //若ttl为null ,则表示加锁成功 ;若ttl不为null ,则往下走break;}// waiting for messageif (ttl >= 0) { 若ttl大于ogetEntry(threadId).getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS);
//进行间歇性锁自旋逻辑,不占用cpu资源} else {getEntry(threadId).getLatch().acquire();}}} finally {unsubscribe(future, threadId);}
//        get(lockAsync(leaseTime, unit));}

执行tryAcquire(leaseTime, unit, threadId)尝试加锁逻辑

 private <T> RFuture<Long> tryAcquireAsync(long leaseTime, TimeUnit unit, final long threadId) {if (leaseTime != -1) {return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_LONG);}
//leaseTime 默认设置为-1RFuture<Long> ttlRemainingFuture = tryLockInnerAsync(commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG);
//执行加锁逻辑ttlRemainingFuture.addListener(new FutureListener<Long>() { //异步执行,锁续命逻辑@Overridepublic void operationComplete(Future<Long> future) throws Exception {if (!future.isSuccess()) { //若加锁不成功,则退出return;}Long ttlRemaining = future.getNow(); //若加锁成功,则ttlRemaining 为null// lock acquiredif (ttlRemaining == null) {scheduleExpirationRenewal(threadId); //加锁成功,则执行锁续命逻辑}}});return ttlRemainingFuture;}

执行tryLockInnerAsync(commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG); 加锁逻辑

  <T> RFuture<T> tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {
//通过lua脚本来执行加锁逻辑,来保证原子性internalLockLeaseTime = unit.toMillis(leaseTime);return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, command,//执行加锁逻辑 (key,argv与下面所传参数一一对应)"if (redis.call('exists', KEYS[1]) == 0) then " +      //KEY[1]表示下面的getName(),"redis.call('hset', KEYS[1], ARGV[2], 1); " +    //ARGV[2]表示下面的getLockName(threadId)"redis.call('pexpire', KEYS[1], ARGV[1]); " + //ARGV[1]表示下面的internalLockLeaseTime"return nil; " +"end; " +
//执行锁重入逻辑(一个线程对同一个锁对象进行多次加锁,此为重入锁逻辑)"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +"redis.call('hincrby', KEYS[1], ARGV[2], 1); " +"redis.call('pexpire', KEYS[1], ARGV[1]); " +"return nil; " +"end; " +"return redis.call('pttl', KEYS[1]);", //设置返回过期时间Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId));}

执行scheduleExpirationRenewal(threadId);锁续命逻辑

private void scheduleExpirationRenewal(final long threadId) {if (expirationRenewalMap.containsKey(getEntryName())) {return;}Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {@Overridepublic void run(Timeout timeout) throws Exception {//执行锁续命逻辑RFuture<Boolean> future = commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
//KEYS[1]表示getName(),ARGV[2]表示锁对象getLockName(threadId), ARGV[1]表示过期时间
//判断当前锁的对象,为当前的线程对象,那么则当前的锁的对象设置原始的过期时间,以达到续命效果"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +"redis.call('pexpire', KEYS[1], ARGV[1]); " +"return 1; " +"end; " +"return 0;",Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId));//异步监听执行        future.addListener(new FutureListener<Boolean>() {@Overridepublic void operationComplete(Future<Boolean> future) throws Exception {expirationRenewalMap.remove(getEntryName());if (!future.isSuccess()) {log.error("Can't update lock " + getName() + " expiration", future.cause());return;}//判断是否任然持有锁,是的话,则getNow()为nullif (future.getNow()) {// reschedule itselfscheduleExpirationRenewal(threadId);//再次执行续命逻辑}}});}}, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);if (expirationRenewalMap.putIfAbsent(getEntryName(), task) != null) {task.cancel();}}

在外层未获取到锁的线程  getEntry(threadId).getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS);间隙自旋获取锁对象

getLatch()表示信号量,信号量为1,则表示阻塞状态,最终通过发布订阅方式来唤醒当前被阻塞的线程,唤醒后则执行获取锁的逻辑 doAcquireSharedNanos(arg, nanosTimeout);

http://www.ds6.com.cn/news/98506.html

相关文章:

  • 做皮革网站seo自学网app
  • 给漫画网站做推广百度指数查询手机版app
  • 建站公司网站用什么好独立站建站平台
  • 河北建设厅网站查询完美动力培训价格表
  • linux卸载wordpress河南seo外包
  • 网站后缀意思公司网站设计与制作
  • 怎么用flash做游戏下载网站做网站公司哪家比较好
  • 公司网站 百度淘宝关键词查询工具哪个好
  • 服饰东莞网站建设电商的推广方式有哪些
  • asp.net网站项目建设公司网络推广网站
  • wordpress 标签描述绍兴seo公司
  • 高端网站制作建设网站关键词推广优化
  • 源码怎么做网站谷歌seo是指什么意思
  • 大气简洁企业通用网站模板白帽seo公司
  • 重庆做网站建设的公司百度搜索高级搜索
  • 怎么做网站广告赚钱上海短视频推广
  • 网站推广定义seo网络推广软件
  • 鄂州网站建设设计宣传推广方案模板
  • 做陶瓷公司网站百度公司在哪里
  • 做网站公司哪家正规网络推广专员是做什么的
  • 德阳做网站google search
  • 做军事网站的项目背景图片最近时事热点新闻评论及点评
  • 把给公司做的设计放到自己的网站上百度竞价效果怎么样
  • 中英文网站是咋做的关键词排名优化顾问
  • 网站栏目百度搜索推广创意方案
  • www.qd2008 网站建设一站式推广平台
  • 网站建设有没有做的必要韩国比分预测
  • 贵港网站建设千锋教育培训多少钱
  • 沧州网站优化网络整合营销的特点有
  • 将二级域名 网站目录公司网址有哪些