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

网站开发要学什么网站策划报告

网站开发要学什么,网站策划报告,四川省人民政府关于调整全省最低工资标准的通知,网站建设小文章目录 依赖注解aophelperTest 免责声明&#xff1a;本人无意侵权&#xff0c;奈何找不到原文作者&#xff0c;也找不到网址&#xff0c;于是自己记录一下&#xff0c;如果有侵权之嫌&#xff0c;请联系我删除文章 依赖 <!-- https://mvnrepository.com/artifact/com.goo…

文章目录

  • 依赖
  • 注解
  • aop
  • helper
  • Test

免责声明:本人无意侵权,奈何找不到原文作者,也找不到网址,于是自己记录一下,如果有侵权之嫌,请联系我删除文章

依赖

  <!-- https://mvnrepository.com/artifact/com.google.guava/guava --><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>32.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 jsonObject=new 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;charset=UTF-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 Map<String,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

@RestController@Tag(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.tj-hxxt.cn/news/114426.html

相关文章:

  • 营销型网站标准网页源码搜索引擎排名查询
  • 创建网站免费注册seo还有未来吗
  • 做PPT参考图片网站 知乎企业官网首页设计
  • 中山网站建设方案网络营销的重要性
  • 甘肃网站制作公司广告推广方式有哪几种
  • 黄石企业网站设计手机优化助手
  • 如何在自己网站做直播竞价托管一般要多少钱
  • 做一个网站系统多少钱app推广拉新一手渠道代理
  • 建筑考试培训网丈哥seo博客工具
  • 自动生成作文网站各大搜索引擎收录入口
  • 镇平做网站百度竞价入门教程
  • 苏州公司网站建设方案友情链接的获取途径有哪些
  • 关于医院要求建设网站的请示南京 seo 价格
  • 网站的栏目网络工程师培训一般多少钱
  • wordpress中常用插件安装包seo按照搜索引擎的什么对网站
  • 网站建设报价明细模板网站建设方案书模板
  • 西瓜网络深圳网站建设 东莞网站建设强化防疫指导
  • 国家出台建设工程政策的网站百度投诉中心人工电话号码
  • 佛山建站网站模板优化公司治理结构
  • 内部网络网站怎么做百度云盘官网
  • 网站开发形成收入怎么做帐搜索引擎优化的重要性
  • 大学生网站设计作品成品代码软件怎么推广
  • 网站建设定制公司推荐网站关键词seo排名
  • 生鲜网站建设规划书范文com域名注册
  • 宁夏网站设计联系电话友情链接只有链接
  • 武汉b2b网站建设公司网站统计工具有哪些
  • 哪家做网站数据分析系统
  • 做电影网站不放国内主机尚硅谷培训机构官网
  • 考研网站做刷词广东又出现新病毒
  • 1元云购网站建设百度seo培训课程