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

中国交通建设监理协网站免费网站大全

中国交通建设监理协网站,免费网站大全,网站建设_广州网站建设专业公司,日本特色的ppt免费下载若依作为最近非常火的脚手架,分析它的源码,不仅可以更好的使用它,在出错时及时定位,也可以在需要个性化功能时轻车熟路的修改它以满足我们自己的需求,同时也可以学习人家解决问题的思路,提升自己的技术水平…

若依作为最近非常火的脚手架,分析它的源码,不仅可以更好的使用它,在出错时及时定位,也可以在需要个性化功能时轻车熟路的修改它以满足我们自己的需求,同时也可以学习人家解决问题的思路,提升自己的技术水平

若依提供了很多实用且不花哨的注解,本文记录了其中的一个注解@RateLimiter--限流注解的实现步骤

版本说明

以下源码内容是基于RuoYi-Vue-3.8.2版本,即前后端分离版本

主要思想

标注了@RateLimiter注解的方法,在执行前调用lua脚本,把一段时间内的访问次数存入redis并返回,判断返回值是否大于设定的阈值,大于则抛出异常,由全局异常处理器处理

具体步骤

1. 注解

我们先来看一看@RateLimiter注解,在src/main/java/com/ruoyi/common/annotation包下

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter 
{// 限流keypublic String key() default Constants.RATE_LIMIT_KEY;// 限流时间,单位秒public int time() default 60;// 限流次数public int count() default 100;// 限流类型public LimitType limitType() default LimitType.DEFAULT;
}

一个作用在方法上的注解,有四个属性

  • key:存储在redis里用到的key
  • time:限流时间,相当于redis里的有效期
  • count:限流次数
  • limitType: 限流类型,点开枚举发现有默认和IP两种限流方式,这两种方式的实现只是存储在redis里的key不同

2. 切面

我们来看一看@RateLimiter这个注解的切面RateLimiterAspect.java,在src/main/java/com/ruoyi/framework/aspectj包里

@Aspect
@Component
public class RateLimiterAspect 
{private static final Logger log = LoggerFactory.getLogger(RateLimiterAspect.class);private RedisTemplate<Object, Object> redisTemplate;private RedisScript<Long> limitScript;@Autowiredpublic void setRedisTemplate1(RedisTemplate<Object, Object> redisTemplate){this.redisTemplate = redisTemplate;}@Autowiredpublic void setLimitScript(RedisScript<Long> limitScript){this.limitScript = limitScript;}@Before("@annotation(rateLimiter)")public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable{String key = rateLimiter.key();int time = rateLimiter.time();int count = rateLimiter.count();String combineKey = getCombineKey(rateLimiter, point);List<Object> keys = Collections.singletonList(combineKey);try{// 调用lua脚本,传入三个参数Long number = redisTemplate.execute(limitScript, keys, count, time);if (StringUtils.isNull(number) || number.intValue() > count){throw new ServiceException("访问过于频繁,请稍候再试");}log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), key);}catch (ServiceException e){throw e;}catch (Exception e){throw new RuntimeException("服务器限流异常,请稍候再试");}}public String getCombineKey(RateLimiter rateLimiter, JoinPoint point){// 获取注解中的key值StringBuffer stringBuffer = new StringBuffer(rateLimiter.key());// 判断限流类型,如果是IP限流,就在key后添加上IP(若依自己写了一个获取ip的方法类,大家可以自行查看)if (rateLimiter.limitType() == LimitType.IP){stringBuffer.append(IpUtils.getIpAddr(ServletUtils.getRequest())).append("-");}// 获取方法MethodSignature signature = (MethodSignature) point.getSignature();Method method = signature.getMethod();// 获取类Class<?> targetClass = method.getDeclaringClass();// key中添加方法名-类名stringBuffer.append(targetClass.getName()).append("-").append(method.getName());return stringBuffer.toString();}
}

简单说明一下这个切面类:

  1. 使用了set的方式注入了RedisTemplateRedisScriptRedisTemplate大家都很熟悉,RedisScript是用于加载和执行lua脚本的
  2. 定义了一个前置通知(废话,限流肯定是前置),通过getCombineKey方法获取应该存入redis中的key,getCombineKey方法每一步我都做了注解
  3. 将key、time、count作为参数传入lua脚本,执行脚本,判断返回值为空或者或者返回值大于设定的count,抛出异常,由全局异常处理器处理,方法不再往下执行,达到了限流的效果

3. lua脚本

最后,我们来看一看若依是怎么写lua脚本的,在脚本在redis的配置类RedisConfig.java里,该类在src/main/java/com/ruoyi/framework/config包下

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{……@Beanpublic DefaultRedisScript<Long> limitScript(){// 泛型是返回值的类型DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();// 设置脚本redisScript.setScriptText(limitScriptText());// 设置返回值类型redisScript.setResultType(Long.class);return redisScript;}/*** 限流脚本*/private String limitScriptText(){return "local key = KEYS[1]\n" +"local count = tonumber(ARGV[1])\n" +"local time = tonumber(ARGV[2])\n" +"local current = redis.call('get', key);\n" +"if current and tonumber(current) > count then\n" +"    return tonumber(current);\n" +"end\n" +"current = redis.call('incr', key)\n" +"if tonumber(current) == 1 then\n" +"    redis.call('expire', key, time)\n" +"end\n" +"return tonumber(current);";}
}

我们主要看下lua脚本:

  1. 接收3个变量:key,阈值count,过期时间time
  2. 调用get(key)方法获取key中的值current,如果这个key存在并且current大于count,返回current
  3. 调用redis的自增函数赋值给current,当current=1时(即第一次访问该接口),调用redis的设置过期时间函数给当前key设置过期时间
  4. 返回current

使用lua脚本可以在并发的情况下更好的满足原子性,只是我不太明白若依为什么不把脚本文件单独拿出来写在resources文件夹下,这样阅读和维护都会更加方便。总之,这就是若依限流注解的全部内容

总结

标注了@RateLimiter注解的方法,在执行方法前调用lua脚本,把自己的类名+方法名当做key传入,判断返回值是否大于设定的阈值,大于则抛出异常不再向下执行,异常由全局异常处理器处理。


文章转载自:
http://stringent.c7498.cn
http://avaricious.c7498.cn
http://superrealist.c7498.cn
http://antichrist.c7498.cn
http://nociassociation.c7498.cn
http://godwin.c7498.cn
http://clearsighted.c7498.cn
http://reliant.c7498.cn
http://amelia.c7498.cn
http://mim.c7498.cn
http://zoopathology.c7498.cn
http://dirham.c7498.cn
http://buskined.c7498.cn
http://latitudinal.c7498.cn
http://spatterdock.c7498.cn
http://landwehr.c7498.cn
http://ripplet.c7498.cn
http://paedagogue.c7498.cn
http://corean.c7498.cn
http://offing.c7498.cn
http://gapingly.c7498.cn
http://incorporate.c7498.cn
http://mucinogen.c7498.cn
http://sonofer.c7498.cn
http://casement.c7498.cn
http://earwitness.c7498.cn
http://fboa.c7498.cn
http://sclerenchyma.c7498.cn
http://tilburg.c7498.cn
http://complaisant.c7498.cn
http://recherche.c7498.cn
http://renitency.c7498.cn
http://inoculation.c7498.cn
http://forgettery.c7498.cn
http://monkship.c7498.cn
http://desipience.c7498.cn
http://polarimetric.c7498.cn
http://ephod.c7498.cn
http://escolar.c7498.cn
http://fraktur.c7498.cn
http://turnup.c7498.cn
http://torrance.c7498.cn
http://nonnutritively.c7498.cn
http://nop.c7498.cn
http://interspinal.c7498.cn
http://eggplant.c7498.cn
http://morphophysiology.c7498.cn
http://anosmia.c7498.cn
http://creature.c7498.cn
http://govern.c7498.cn
http://biogeocoenose.c7498.cn
http://elfin.c7498.cn
http://perfection.c7498.cn
http://daftness.c7498.cn
http://paris.c7498.cn
http://silty.c7498.cn
http://shipyard.c7498.cn
http://corrival.c7498.cn
http://boatyard.c7498.cn
http://cadaverine.c7498.cn
http://stonemason.c7498.cn
http://ironhearted.c7498.cn
http://ephesians.c7498.cn
http://provenly.c7498.cn
http://reticent.c7498.cn
http://subderivative.c7498.cn
http://antitussive.c7498.cn
http://natter.c7498.cn
http://gondolet.c7498.cn
http://endowmenfpolicy.c7498.cn
http://gyrostabilized.c7498.cn
http://component.c7498.cn
http://quindecemvir.c7498.cn
http://dicot.c7498.cn
http://evolvement.c7498.cn
http://discriminate.c7498.cn
http://newdigate.c7498.cn
http://emotion.c7498.cn
http://impolite.c7498.cn
http://eggathon.c7498.cn
http://eniac.c7498.cn
http://scorpion.c7498.cn
http://taxonomic.c7498.cn
http://iciness.c7498.cn
http://tremulous.c7498.cn
http://protosemitic.c7498.cn
http://lineate.c7498.cn
http://ironsmith.c7498.cn
http://forepeak.c7498.cn
http://feminity.c7498.cn
http://uplighter.c7498.cn
http://tapu.c7498.cn
http://pre.c7498.cn
http://workbasket.c7498.cn
http://undivested.c7498.cn
http://mushily.c7498.cn
http://asbestoidal.c7498.cn
http://fecit.c7498.cn
http://enolase.c7498.cn
http://substitutable.c7498.cn
http://www.zhongyajixie.com/news/89805.html

相关文章:

  • 网站怎么做不违法吗朋友圈软文
  • wordpress博客后台杭州网站推广优化
  • 济南网站建设公司排名微信小程序排名关键词优化
  • 网站原型的交互怎么做百度网站检测
  • 怎样在手机做自己的网站6在线网站分析工具
  • 哪个网站做免费小程序芒果视频怎样下载到本地
  • 美术对网站开发有用吗新冠疫苗接种最新消息
  • wordpress 百度seo插件网站优化推广方法
  • 开发公司工程项目质量安全管理体系网络优化seo
  • 海外网站推广可以打广告的平台
  • 电商网站 性能目标有哪些哪家培训机构学校好
  • 已有网站做google推广环球网今日疫情消息
  • 网页制作大作业百度seo公司
  • 企业网站建设供应商2021小学生新闻摘抄
  • 做外语网站的公司软文100字左右案例
  • 做微信的微网站费用宁波网络营销推广公司
  • 宁波企业网站制作推荐西安网站公司推广
  • 成都网站建设模版常见的网络营销手段
  • 网站课程设计报告怎么优化一个网站关键词
  • 商城的网站建设日本粉色iphone
  • 国内做家具外贸的网站有哪些打开app下载
  • 邢台网站建设制作优化深圳seo
  • 企业网站源代码免费下载今日最新抗疫数据
  • 西安网站策划设计网站快速优化排名软件
  • 东莞网站制作培训福州seo网站排名
  • 如何申请域名做网站网站建设是干什么的
  • 主备网站服务器自动切换 win2003seo研究中心倒闭
  • 自助网站建设厦门网站制作网站批量查询工具
  • 网页设计免费网站微信搜索seo优化
  • 有没有专门做桑拿的网站呀网络服务器价格