您的位置:首页 > 健康 > 养生 > 卓创源码网_济南智能网站建设电话_semantic scholar_比较火的推广软件

卓创源码网_济南智能网站建设电话_semantic scholar_比较火的推广软件

2024/10/31 3:00:32 来源:https://blog.csdn.net/meetbetterhc/article/details/142877564  浏览:    关键词:卓创源码网_济南智能网站建设电话_semantic scholar_比较火的推广软件
卓创源码网_济南智能网站建设电话_semantic scholar_比较火的推广软件

创建一个限流注解

package com.hhc.analysis.config.annotation;import com.hhc.analysis.common.constant.CacheConstants;import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;/*** 限流注解* * @author ruoyi*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter
{/*** 限流key*/public String key() default CacheConstants.RATE_LIMIT_KEY;/*** 限流时间*/public int time() default 1;/*** 限流次数(最大访问次数)*/public int count() default 5;/***  时间单位,默认秒*/TimeUnit timeUnit() default TimeUnit.SECONDS;
}

创建一个切面类

package com.hhc.analysis.config.aspectj;import com.hhc.analysis.common.constant.ErrorConstants;
import com.hhc.analysis.common.exception.AIDocException;
import com.hhc.analysis.config.annotation.RateLimiter;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;/*** 限流处理** @author ruoyi*/
@Aspect
@Component
public class RateLimiterAspect
{private static final Logger log = LoggerFactory.getLogger(RateLimiterAspect.class);@Autowiredprivate RedisTemplate redisTemplate;/*** 定义切点为注解*/@Pointcut("@annotation(com.hhc.analysis.config.annotation.RateLimiter)")public void AccessLimitPointcut(){}@Before(value = "AccessLimitPointcut()")public void handleAccessLimit(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();RateLimiter annotation = AnnotationUtils.findAnnotation(method, RateLimiter.class);int accessMaxTimes = annotation.count();long timeOut = annotation.time();TimeUnit timeUnit = annotation.timeUnit();String key = annotation.key();// 如果redis不存在或已经过期Long expire = redisTemplate.opsForValue().getOperations().getExpire(key);if (!redisTemplate.hasKey(key) || Objects.requireNonNull(expire).intValue() < 0) {redisTemplate.opsForValue().set(key, 1, timeOut, timeUnit);} else {long increment = redisTemplate.opsForValue().increment(key).intValue();if (increment > accessMaxTimes) {throw new AIDocException(ErrorConstants.INVOKE_LIMIE, "1分钟内只能请求 " + accessMaxTimes+"次,请稍后尝试!");}}}
}

在调用的接口上加上@RateLimiter 就实现啦

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com