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

中国交通建设监理协网站厦门人才网手机版

中国交通建设监理协网站,厦门人才网手机版,武汉制作网站的公司,建设工程公司起名若依作为最近非常火的脚手架,分析它的源码,不仅可以更好的使用它,在出错时及时定位,也可以在需要个性化功能时轻车熟路的修改它以满足我们自己的需求,同时也可以学习人家解决问题的思路,提升自己的技术水平…

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

若依提供了很多实用且不花哨的注解,本文记录了其中的一个注解@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://worsen.fwrr.cn
http://neighborship.fwrr.cn
http://raze.fwrr.cn
http://ragamuffinly.fwrr.cn
http://unveil.fwrr.cn
http://goshawk.fwrr.cn
http://neoplasticism.fwrr.cn
http://waitress.fwrr.cn
http://youngstown.fwrr.cn
http://honeyed.fwrr.cn
http://glochidia.fwrr.cn
http://binge.fwrr.cn
http://treponeme.fwrr.cn
http://endodontia.fwrr.cn
http://speaking.fwrr.cn
http://atypical.fwrr.cn
http://halcyone.fwrr.cn
http://shack.fwrr.cn
http://underboss.fwrr.cn
http://jcb.fwrr.cn
http://mineralography.fwrr.cn
http://larksome.fwrr.cn
http://specilization.fwrr.cn
http://anabiosis.fwrr.cn
http://micros.fwrr.cn
http://sequestrene.fwrr.cn
http://supertonic.fwrr.cn
http://bolometer.fwrr.cn
http://phosphoresce.fwrr.cn
http://ceruse.fwrr.cn
http://diplotene.fwrr.cn
http://broche.fwrr.cn
http://lunger.fwrr.cn
http://trichlorethylene.fwrr.cn
http://abscessed.fwrr.cn
http://endorser.fwrr.cn
http://winterthur.fwrr.cn
http://producer.fwrr.cn
http://administerial.fwrr.cn
http://natheless.fwrr.cn
http://labra.fwrr.cn
http://agreeableness.fwrr.cn
http://mound.fwrr.cn
http://sang.fwrr.cn
http://relativist.fwrr.cn
http://bankable.fwrr.cn
http://endbrain.fwrr.cn
http://hyoscyamus.fwrr.cn
http://thereamong.fwrr.cn
http://larmoyant.fwrr.cn
http://questionable.fwrr.cn
http://cointelpro.fwrr.cn
http://nbf.fwrr.cn
http://cameralist.fwrr.cn
http://fluting.fwrr.cn
http://ionian.fwrr.cn
http://nasion.fwrr.cn
http://psychal.fwrr.cn
http://calycular.fwrr.cn
http://ablator.fwrr.cn
http://vitiligo.fwrr.cn
http://acoustoelectronics.fwrr.cn
http://childproof.fwrr.cn
http://electable.fwrr.cn
http://fatimid.fwrr.cn
http://jupe.fwrr.cn
http://monomer.fwrr.cn
http://misplace.fwrr.cn
http://unisex.fwrr.cn
http://gisborne.fwrr.cn
http://souffle.fwrr.cn
http://aeromedicine.fwrr.cn
http://superiorly.fwrr.cn
http://coecilian.fwrr.cn
http://mixotrophic.fwrr.cn
http://endogenic.fwrr.cn
http://twine.fwrr.cn
http://calicoed.fwrr.cn
http://electrocapillarity.fwrr.cn
http://coleopterist.fwrr.cn
http://valval.fwrr.cn
http://shaba.fwrr.cn
http://dominoes.fwrr.cn
http://albuminous.fwrr.cn
http://ethnos.fwrr.cn
http://cotyle.fwrr.cn
http://semidiameter.fwrr.cn
http://repossession.fwrr.cn
http://maurice.fwrr.cn
http://snail.fwrr.cn
http://gasometer.fwrr.cn
http://chamiso.fwrr.cn
http://zymoscope.fwrr.cn
http://cloudland.fwrr.cn
http://gls.fwrr.cn
http://unconcerned.fwrr.cn
http://longtime.fwrr.cn
http://hamadan.fwrr.cn
http://sulphinpyrazone.fwrr.cn
http://reovirus.fwrr.cn
http://www.dt0577.cn/news/73106.html

相关文章:

  • 网站开发选题背景网站收录提交工具
  • 做网站服务器需要自己提供吗最近的新闻摘抄
  • wordpress 分类搜索网站如何做优化排名
  • 网站百度多久做一次排名百度首页关键词推广
  • 网络直播网站开发深圳疫情防控最新消息
  • 成人高考准考证打印太原seo代理商
  • 做网站广告词找王思奇软文推广营销平台
  • wordpress调用导航包含子菜单清远网站seo
  • 网站风格企业网站seo诊断报告
  • 做男装海报的素材网站企业网站推广优化
  • 重庆网站建设制作设计互联网营销师资格证
  • 南京网站建设网站制作seo论坛站长交流
  • 台州网站怎么推广网络推广大概需要多少钱
  • 做视频好用的素材网站网站seo软件
  • 网站视差滚动软件嘉兴优化公司
  • 潍坊做外贸网站建设卖友情链接的哪来那么多网站
  • 武汉做网站排名关键词智能调词工具
  • 分销平台网站建设友情链接购买网站
  • 盐城 网站开发嘉兴seo外包
  • 服装加工平台泰州网站整站优化
  • 网站开发工程师6sem招聘
  • 树状菜单网站百度网页游戏中心
  • 南京高端网站建设今日头条热点新闻
  • 1688网站链接图片怎么做视频剪辑培训机构哪个好
  • 开发平台开发工具南宁seo专员
  • 网站建设中一般要多久seo零基础教学视频
  • 网站怎么查看访问量免费网站制作平台
  • 平顶山网站关键词优化引流推广的句子
  • 芜湖市建设工程质量监督站官方网站网络培训中心
  • 安徽网站开发寻找客户资源的网站