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

深圳营销型网站建设设计公司献县制作网站

深圳营销型网站建设设计公司,献县制作网站,wordpress手机版论坛,网站做前端文章目录 依赖注解aophelperTest 免责声明#xff1a;本人无意侵权#xff0c;奈何找不到原文作者#xff0c;也找不到网址#xff0c;于是自己记录一下#xff0c;如果有侵权之嫌#xff0c;请联系我删除文章 依赖 !-- https://mvnrepository.com/artifact/com.goo… 文章目录 依赖注解aophelperTest 免责声明本人无意侵权奈何找不到原文作者也找不到网址于是自己记录一下如果有侵权之嫌请联系我删除文章 依赖 !-- https://mvnrepository.com/artifact/com.google.guava/guava --dependencygroupIdcom.google.guava/groupIdartifactIdguava/artifactIdversion32.1.3-jre/version/dependency注解 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;Target(value ElementType.METHOD) Retention(value RetentionPolicy.RUNTIME) public interface RateConfigAnno {String limitType();double limitCount() default 5d; }aop import cn.hutool.core.thread.ThreadUtil; import com.alibaba.fastjson2.JSONObject; import com.google.common.util.concurrent.RateLimiter; import com.tjbchtyw.tjflowcontrol.annocation.RateConfigAnno; import com.tjbchtyw.tjflowcontrol.helper.RateLimitHelper; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletResponse; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;import java.io.IOException; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.Objects;Aspect Component public class GuavaLimitAop {private static final Logger logger LoggerFactory.getLogger(GuavaLimitAop.class);Before(annotation(com.tjbchtyw.tjflowcontrol.annocation.RateConfigAnno))public void limit(JoinPoint joinPoint) {//1、获取当前的调用方法Method currentMethod getCurrentMethod(joinPoint);if (Objects.isNull(currentMethod)) {return;}//2、从方法注解定义上获取限流的类型String limitType currentMethod.getAnnotation(RateConfigAnno.class).limitType();double limitCount currentMethod.getAnnotation(RateConfigAnno.class).limitCount();//使用guava的令牌桶算法获取一个令牌获取不到先等待RateLimiter rateLimiter RateLimitHelper.getRateLimiter(limitType, limitCount);// boolean b true;boolean pass rateLimiter.tryAcquire();if (pass) {System.out.println(获取到令牌);}else {//重试 仅测试用 有优化方案可以放在评论区for (int i 0; i 5; i) {ThreadUtil.safeSleep(1000);System.out.println(第 (i 1) 次获取令牌);if(rateLimiter.tryAcquire()) break;if(i 4){System.out.println(在第 (i 1) 次后未获取到令牌 开始限流);HttpServletResponse resp ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();JSONObject jsonObjectnew JSONObject();jsonObject.put(success,false);jsonObject.put(msg,限流中);try {output(resp, jsonObject.toJSONString());}catch (Exception e){logger.error(error,e:{},e);}}}}}private Method getCurrentMethod(JoinPoint joinPoint) {Method[] methods joinPoint.getTarget().getClass().getMethods();Method target null;for (Method method : methods) {if (method.getName().equals(joinPoint.getSignature().getName())) {target method;break;}}return target;}public void output(HttpServletResponse response, String msg) throws IOException {response.setContentType(application/json;charsetUTF-8);ServletOutputStream outputStream null;try {outputStream response.getOutputStream();outputStream.write(msg.getBytes(StandardCharsets.UTF_8));} catch (IOException e) {e.printStackTrace();} finally {outputStream.flush();outputStream.close();}} }helper import com.google.common.util.concurrent.RateLimiter;import java.util.HashMap; import java.util.Map;public class RateLimitHelper {private RateLimitHelper(){}private static final MapString,RateLimiter rateMap new HashMap();public static RateLimiter getRateLimiter(String limitType, double limitCount ){RateLimiter rateLimiter rateMap.get(limitType);if(rateLimiter null){rateLimiter RateLimiter.create(limitCount);rateMap.put(limitType,rateLimiter);}return rateLimiter;}}Test RestControllerTag(name 测试Controller, description 这是描述)RequestMapping(/pred-api)public class FlowController {AutowiredFlowContext flowContext;PostMapping(/pdf/test)//Count 限制次数RateConfigAnno(limitType makePdf,limitCount 15)Operation(summary 限流接口)public String flowCont(Parameter(name pdfParam, description 参数对象,type标识执行不同的策略) RequestBody Param Param) {return test;}}
http://www.hkea.cn/news/14321187/

相关文章:

  • 重庆江北营销型网站建设公司哪家好wordpress程序在ftp中上传到空间 网页怎么打不开
  • 电影网站建设的程序wordpress编辑器格式
  • 企业网站模板免费简述什么是网络营销
  • 网站seo优化wordpress 数字格式
  • 织梦网站建设考试织梦网站添加视频教程
  • 南宁手机做网站设计淘宝上开做网站的店铺
  • wordpress网站更换空间怎么创网站
  • 商标可以做网站吗友情链接交换网
  • 做网站卖别人的软件可以吗wordpress排行榜模板
  • 2022年没封网站直接进入合肥seo
  • 网站不备案大连模板建站代理
  • 做网站教材建设旅游网站的意义
  • 做网站程序看什么书wordpress缺少临时文件夹
  • 如何通过建设网站赚钱新网站seo方法
  • 滨州网站定制ui设计哪里培训机构
  • 公司可以做多个网站吗中国十大网站建设
  • 阿里云大学 网站建设刷赞网站空间免费
  • 网站模板目录网络软文
  • 荆门网站制作公司智能设计平台
  • 爱站网 关键词挖掘附近的装修公司地点
  • 网站建设好还需投资吗网站结构有哪几种
  • wordpress重装密码两个域名同一个网站做优化
  • 高频网站开发网站开发字体
  • 免费商城网站模板想做外贸做哪些网站好
  • 图片版小说网站源码扁平化的网站结构图
  • 哈尔滨行业网站建设策划网站制作叫什么
  • 网站建设加空间视频制作软件哪个好 前十名
  • 郑州网站建设hndream湖南省住房和建设厅网站
  • 合肥市建设工程市场信息价网站成都网站建设_创新互联
  • 预约网站制作ui设计做app网站要学什么