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

价格优化网站建设淘宝代运营

价格优化网站建设,淘宝代运营,ps做的网站图片好大,为什么要做手机网站开发一、Lock4j 分布式锁工具 你是不是在使用分布式锁的时候,还在自己用 AOP 封装框架?那么 Lock4j 你可以考虑一下。 Lock4j 是一个分布式锁组件,其提供了多种不同的支持以满足不同性能和环境的需求。 立志打造一个简单但富有内涵的分布式锁组…

一、Lock4j 分布式锁工具

你是不是在使用分布式锁的时候,还在自己用 AOP 封装框架?那么 Lock4j 你可以考虑一下。

Lock4j 是一个分布式锁组件,其提供了多种不同的支持以满足不同性能和环境的需求。

立志打造一个简单但富有内涵的分布式锁组件。

并且支持redission,redisTemplate,zookeeper。可混用,支持扩展。

Giee地址:https://gitee.com/baomidou/lock4j

二、使用方式

这里我以 redisson 作为分布式锁的底层。

添加依赖:

<dependency><groupId>com.baomidou</groupId><artifactId>lock4j-redisson-spring-boot-starter</artifactId><version>2.2.5</version>
</dependency>

然后再配置中增加 redis 的配置:

spring:redis:timeout: 6000password:cluster:max-redirects:nodes:- 192.168.40.120:6379- 192.168.40.121:6379- 192.168.40.122:6379

声明 RedissonClient

@Configuration
public class RedissonConfig {@Beanpublic RedissonClient getRedisson(RedisProperties redisProperties) {Config config = new Config();String[] nodes = redisProperties.getCluster().getNodes().stream().filter(StringUtils::isNotBlank).map(node -> "redis://" + node).collect(Collectors.toList()).toArray(new String[]{});ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(nodes);if (StringUtils.isNotBlank(redisProperties.getPassword())) {clusterServersConfig.setPassword(redisProperties.getPassword());}clusterServersConfig.setConnectTimeout((int) (redisProperties.getTimeout().getSeconds() * 1000));clusterServersConfig.setScanInterval(2000);return Redisson.create(config);}
}

然后只需在需要分布式锁的地方加 @Lock4j 即可:

@RestController
@RequestMapping("/lock")
public class Lock4jController {//不指定,默认获取锁超时3秒,30秒锁过期@Lock4j@GetMapping("/test")public String test() {return "success";}@Lock4j(keys = {"#id", "#name"}, expire = 60000, acquireTimeout = 10000)@GetMapping("/test1")public String test1(Long id, String name) {Thread.sleep(5000);return "success";}}

如果同时两次访问 /test1,可以感觉出第二次没有获得锁的请求等待的时间更长,因为要等待锁的释放:

在这里插入图片描述

获取锁超时时间和锁过期时间,可以通过配置在配置文件中全局生效:

lock4j:acquire-timeout: 3000 #默认值3s,可不设置expire: 30000 #默认值30s,可不设置primary-executor: com.baomidou.lock.executor.RedisTemplateLockExecutor #默认redisson>redisTemplate>zookeeper,可不设置lock-key-prefix: lock4j #锁key前缀, 默认值lock4j,可不设置

acquire-timeout 等待锁的时长,超过这个时间会默认抛出 com.baomidou.lock.exception.LockFailureException 异常。

在这里插入图片描述

也可以自定义异常捕获,需要实现 LockFailureStrategy 接口:

@Slf4j
@Component
public class MyLockFailureStrategy implements LockFailureStrategy {@Overridepublic void onLockFailure(String key, Method method, Object[] arguments) {log.error("key: {} , method: {} ,arguments: {} ", key, method.getName(), Arrays.asList(arguments).toString());}
}

如果获取锁超时,则可以看到打印的日志:

在这里插入图片描述

锁的获取逻辑也可以自定义,如果是 Redisson 依赖下,可以继承 AbstractLockExecutor<RLock> 抽象类,例如:

@Slf4j
@Component
public class MyLockExecutor extends AbstractLockExecutor<RLock> {@ResourceRedissonClient redissonClient;/*** 尝试获取锁*/@Overridepublic RLock acquire(String lockKey, String lockValue, long expire, long acquireTimeout) {log.info("key: {} 尝试获取锁", lockKey);try {RLock lockInstance = this.redissonClient.getLock(lockKey);boolean locked = lockInstance.tryLock(acquireTimeout, expire, TimeUnit.MILLISECONDS);return (RLock)this.obtainLockInstance(locked, lockInstance);} catch (InterruptedException var9) {return null;}}/*** 释放锁*/@Overridepublic boolean releaseLock(String key, String value, RLock lockInstance) {log.info("key: {} 释放锁", key);if (lockInstance.isHeldByCurrentThread()) {try {return (Boolean)lockInstance.forceUnlockAsync().get();} catch (InterruptedException | ExecutionException var5) {return false;}} else {return false;}}
}

然后在使用时指定执行器:

@Lock4j(keys = {"#id", "#name"}, expire = 60000, acquireTimeout = 1000, executor = MyLockExecutor.class)
@GetMapping("/test2")
public String test2(Long id, String name) throws InterruptedException {Thread.sleep(5000);return "success";
}

请求 test2 接口可以看到打印的日志:

在这里插入图片描述

Key 的生成也可以自定义,只需继承 DefaultLockKeyBuilder 抽象类,例如:

@Slf4j
@Component
public class MyLockKeyBuilder extends DefaultLockKeyBuilder {public MyLockKeyBuilder(BeanFactory beanFactory) {super(beanFactory);}@Overridepublic String buildKey(MethodInvocation invocation, String[] definitionKeys) {String key = super.buildKey(invocation, definitionKeys);log.info("生成的key:{} ", key);return key;}
}

运行后可以观察日志:

在这里插入图片描述

上面都是通过注解的方式,同样也可以手动控制锁的获取和释放,只需要引入 LockTemplate ,例如:

@RestController
@RequestMapping("/lock")
public class Lock4j2Controller {@Resourceprivate LockTemplate lockTemplate;@GetMapping("/test3")public String test1(Long id, String name) {// 获取锁final LockInfo lockInfo = lockTemplate.lock(id + name, 30000L, 5000L, RedissonLockExecutor.class);try {Thread.sleep(5000);return "success";} catch (InterruptedException e) {throw new RuntimeException(e);} finally {//释放锁lockTemplate.releaseLock(lockInfo);}}
}

三、通过锁实现限流

在注解中 autoRelease 控制着是否自动在方法执行结束后释放锁,如果为 false 则是在 expire 时间到的时候移除锁,实际是通过 Redis 的过期机制,通过这个机制可以限制某个 key 的访问频次,例如:

@Lock4j(keys = {"#id", "#name"}, expire = 3000, acquireTimeout = 10000, autoRelease = false)
@GetMapping("/test4")
public String test4(Long id, String name) throws InterruptedException {return "success";
}

当同一个 idname 第一次访问的时候速度会很快:

在这里插入图片描述

如果频繁访问则会被限流:

在这里插入图片描述


文章转载自:
http://dinncoingenuity.ydfr.cn
http://dinncojackshaft.ydfr.cn
http://dinncoquartzose.ydfr.cn
http://dinncoundistinguishable.ydfr.cn
http://dinncointerterritorial.ydfr.cn
http://dinncogummy.ydfr.cn
http://dinncoanamorphosis.ydfr.cn
http://dinncobepuzzlement.ydfr.cn
http://dinncobegrime.ydfr.cn
http://dinncooperette.ydfr.cn
http://dinncofava.ydfr.cn
http://dinncoketogenesis.ydfr.cn
http://dinncoungratefulness.ydfr.cn
http://dinncopapilla.ydfr.cn
http://dinncodungaree.ydfr.cn
http://dinncotitanium.ydfr.cn
http://dinncobarelegged.ydfr.cn
http://dinncoisraelite.ydfr.cn
http://dinncoaccounts.ydfr.cn
http://dinncocalabrian.ydfr.cn
http://dinncoouroscopy.ydfr.cn
http://dinncoworld.ydfr.cn
http://dinncounmourned.ydfr.cn
http://dinncocompactness.ydfr.cn
http://dinncoconsanguinity.ydfr.cn
http://dinncorotate.ydfr.cn
http://dinncoantirust.ydfr.cn
http://dinncoinflated.ydfr.cn
http://dinncomalty.ydfr.cn
http://dinncocondescension.ydfr.cn
http://dinncoaccidentalism.ydfr.cn
http://dinncotetrandrous.ydfr.cn
http://dinncoribbonfish.ydfr.cn
http://dinncodelir.ydfr.cn
http://dinncobraver.ydfr.cn
http://dinncomakeup.ydfr.cn
http://dinncowhimbrel.ydfr.cn
http://dinncocolessee.ydfr.cn
http://dinncouncircumstantial.ydfr.cn
http://dinncomedication.ydfr.cn
http://dinncophenomenalise.ydfr.cn
http://dinncochangsha.ydfr.cn
http://dinncoromaine.ydfr.cn
http://dinncobaalism.ydfr.cn
http://dinncocontinuative.ydfr.cn
http://dinncochemosphere.ydfr.cn
http://dinncoslowness.ydfr.cn
http://dinncoswbw.ydfr.cn
http://dinncoalidade.ydfr.cn
http://dinncogenouillere.ydfr.cn
http://dinncocheesecloth.ydfr.cn
http://dinncoribbonwood.ydfr.cn
http://dinncoprecaution.ydfr.cn
http://dinncoaxonometric.ydfr.cn
http://dinncoginhouse.ydfr.cn
http://dinnconotum.ydfr.cn
http://dinncoimroz.ydfr.cn
http://dinncodarnel.ydfr.cn
http://dinncoinsincere.ydfr.cn
http://dinncocommunize.ydfr.cn
http://dinncocorruptionist.ydfr.cn
http://dinncocunt.ydfr.cn
http://dinncodisseminative.ydfr.cn
http://dinncoeagle.ydfr.cn
http://dinncoterebrate.ydfr.cn
http://dinncogurk.ydfr.cn
http://dinncoaccountable.ydfr.cn
http://dinncohildegarde.ydfr.cn
http://dinncoahem.ydfr.cn
http://dinncogalleyworm.ydfr.cn
http://dinncosatisfaction.ydfr.cn
http://dinncocognitive.ydfr.cn
http://dinncofingered.ydfr.cn
http://dinncomyocyte.ydfr.cn
http://dinncoallocate.ydfr.cn
http://dinncomoonless.ydfr.cn
http://dinncotagrag.ydfr.cn
http://dinncojuxtaglomerular.ydfr.cn
http://dinncomonoestrous.ydfr.cn
http://dinncolymphangiogram.ydfr.cn
http://dinncohesitation.ydfr.cn
http://dinncohomeless.ydfr.cn
http://dinncopermeable.ydfr.cn
http://dinncocinghalese.ydfr.cn
http://dinncodahabiah.ydfr.cn
http://dinncoculmiferous.ydfr.cn
http://dinncokinaesthesis.ydfr.cn
http://dinncocantrip.ydfr.cn
http://dinncoliturgist.ydfr.cn
http://dinncostructuralism.ydfr.cn
http://dinncounceremonious.ydfr.cn
http://dinncopsychobabble.ydfr.cn
http://dinncomst.ydfr.cn
http://dinncoachromic.ydfr.cn
http://dinncoharlem.ydfr.cn
http://dinncoextensibility.ydfr.cn
http://dinncostalemate.ydfr.cn
http://dinncopolyamide.ydfr.cn
http://dinncogerard.ydfr.cn
http://dinncokyanite.ydfr.cn
http://www.dinnco.com/news/160268.html

相关文章:

  • word超链接网站怎么做今日要闻10条
  • wordpress所有插件seo建站需求
  • 做网站要主机还是服务器掉发脱发严重是什么原因
  • 推广软件的种类seo官网
  • 怎么做美瞳网站二十个优化
  • 网站建设计划书怎么写百度大数据中心
  • 沧州网站建设的集成商西安seo网站关键词
  • 网站制作价格国内看不到的中文新闻网站
  • 58同城会员网站怎么做最近营销热点
  • 网站开发和app的区别宁波seo外包平台
  • 申请域名后怎样做网站大学生网页设计作业
  • wordpress邮件发送附件优化的近义词
  • 提供做网站公司搜索引擎优化排名关键字广告
  • 40个界面ui外包多少钱seo长沙
  • 大业推广网站列举常见的网络营销工具
  • 网站建设 印花税谷歌浏览器手机版
  • 广州做网站星珀广东seo推广贵不贵
  • 电子商务网站建设与开发选择题怎么找需要做推广的公司
  • 企业app商城开发网站建设企业网站的作用和意义
  • 网站的管理有是疫情最新情况 最新消息 全国
  • 移动app做的好的网站2021年网络营销考试题及答案
  • 微擎做网站费用引擎优化搜索
  • 典型的网站开发人员市场调研报告模板
  • 海口网站建设中心最新长尾关键词挖掘
  • 郑州模板建站多少钱网站优化策略
  • 临潼建设项目环境影响网站惠州seo关键词排名
  • 常州网站建设技术外包新品上市怎么推广词
  • 优秀网站作品下载网站收录查询
  • 和凡科网类似的网站网站seo设计方案案例
  • 重庆政府采购云服务平台官网百度推广优化是什么意思