java 单机接口限流处理方案
436
2022-06-07
限流的需求出现在许多常见的场景中
首先我们编写注解类AccessLimit
,使用注解方式在方法上限流更优雅更方便!三个参数分别代表有效时间、最大访问次数、是否需要登录,可以理解为 seconds 内最多访问 maxCount 次。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AccessLimit {
int seconds();
int maxCount();
boolean needLogin() default true;
}
限流的思路
key
是否存在,是否count
超过了限制的访问次数response
返回msg:请求过于频繁
给前端予以展示import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class AccessLimtInterceptor implements HandlerInterceptor {
@Autowired
private RedisService redisService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
if (null == accessLimit) {
return true;
}
int seconds = accessLimit.seconds();
int maxCount = accessLimit.maxCount();
boolean needLogin = accessLimit.needLogin();
if (needLogin) {
//判断是否登录
}
String key = request.getContextPath() + ":" + request.getServletPath() + ":" + ip ;
Integer count = redisService.get(key);
if (null == count || -1 == count) {
redisService.set(key, 1);
redisService.expire(seconds);
return true;
}
if (count < maxCount) {
redisService.inCr(key);
return true;
}
if (count >= maxCount) {
// response 返回 json 请求过于频繁请稍后再试
return false;
}
}
return true;
}
}
注册拦截器并配置拦截路径和不拦截路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
// extends WebMvcConfigurerAdapter 已经废弃,java 8开始直接继承就可以
@Configuration
public class IntercepterConfig implements WebMvcConfigurer {
@Autowired
private AccessLimtInterceptor accessLimtInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(accessLimtInterceptor)
.addPathPatterns("/拦截路径")
.excludePathPatterns("/不被拦截路径 通常为登录注册或者首页");
}
}
在Controller
层的方法上直接可以使用注解@AccessLimit
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestControler {
@GetMapping("accessLimit")
@AccessLimit(seconds = 3, maxCount = 10)
public String testAccessLimit() {
//xxxx
return "";
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~