} catch (Exception e) {
logger.error(“preHandle catch a exception:” + e.getMessage());
result = ResponseEnum.FAIL;
}
if (ResponseEnum.SUCCESS.code.equals(result.code)) {
return true;
}
handlerResponse(result, response);
return false;
}
protected abstract ResponseEnum preFilter(HttpServletRequest request);
private void handlerResponse(ResponseEnum result, HttpServletResponse response) {
ResponseDto responseDto = new ResponseDto();
responseDto.setCode(result.code);
responseDto.setStatus(result.status);
responseDto.setMessage(result.message);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
PrintWriter printWriter = null;
try {
printWriter = response.getWriter();
printWriter.write(JsonUtils.toJson(responseDto));
} catch (Exception e) {
logger.error(“handlerResponse catch a exception:” + e.getMessage());
} finally {
if (printWriter != null) {
printWriter.close();
}
}
}
}
b)定义流量控制拦截器,流量控制拦截器继承自上面的拦截器抽象类,在preFilter方法中进行流量控制。
@Component(“rateLimitInterceptor”)
public class RateLimitInterceptor extends AbstractInterceptor {
private Logger logger = LoggerFactory.getLogger(RateLimitInterceptor.class);
private static final RateLimiter rateLimiter = RateLimiter.create(300);
public static void setRate(double limiterQPS){
rateLimiter.setRate(limiterQPS);
}
@Override
protected ResponseEnum preFilter(HttpServletRequest request) {
if (!rateLimiter.tryAcquire()) {
logger.warn(“限流中…”);
return ResponseEnum.RATE_LIMIT;
}
return ResponseEnum.SUCCESS;
}
}
使用Guava提供的RateLimiter类来实现流量控制,过程很简单:定义了一个QPS为1的全局限流器(便于测试),使用tryAcquire()方法来尝试获取令牌,如果成功则返回ResponseEnum.OK,否则返回ResponseEnum.R
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
ATE_LIMIT。
3.继承WebMvcConfigurerAdapter来添加自定义拦截器
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurationSupport {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new RateLimitInterceptor()).addPathPatterns("
@RequestMapping(value = “getUserList”, method = RequestMethod.GET)
public String getUserList() {
String result = null;
try {
result = “请求成功”;
}catch (Exception e){
logger.error(“请求失败”, e);
return JsonUtils.toJson(ResponseUtils.failInServer(result));
}
return JsonUtils.toJson(ResponseUtils.success(result));
}
上文使用到的ResponseEnum是一个返回Code的枚举:
public enum ResponseEnum {
SUCCESS(“200”,“000000”,“请求成功”),
FAIL(“200”,“100000”,“请求失败”),
FAIL_BY_PARAMS(“200”,“200000”,“请求参数异常”),
FAIL_IN_SERVER(“200”,“300000”,“服务器内部异常”),
RATE_LIMIT(“200”,“400000”,“限流中”);
public String status;
public String code;
public String message;
ResponseEnum(String s, String s1, String s2) {
this.status = s;
this.code = s1;
this.message = s2;
}
}
5.使用Postman来测试接口
快速并且反复的调用接口,可以很容易的看到两种结果。
成功通过限流器的结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)