使用Guava实现限流器

  • @author linzhiqiang

  • @date 2019/4/17

*/

public abstract class AbstractInterceptor extends HandlerInterceptorAdapter {

private Logger logger = LoggerFactory.getLogger(AbstractInterceptor.class);

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

ResponseEnum result;

try {

result = preFilter(request);

} 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;

}

/**

  • 自定义pre处理

  • @param request

  • @return

*/

protected abstract ResponseEnum preFilter(HttpServletRequest request);

/**

  • 错误处理事件

  • @param result

  • @param response

*/

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方法中进行流量控制。

/**

  • @author linzhiqiang

  • @date 2019/4/17

*/

@Component(“rateLimitInterceptor”)

public class RateLimitInterceptor extends AbstractInterceptor {

private Logger logger = LoggerFactory.getLogger(RateLimitInterceptor.class);

/**

  • 单机全局限流器(限制QPS为250)

*/

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.RATE_LIMIT。

3.继承WebMvcConfigurerAdapter来添加自定义拦截器

@Configuration

public class MyWebAppConfigurer extends WebMvcConfigurationSupport {

@Override

public void addInterceptors(InterceptorRegistry registry) {

// 多个拦截器组成一个拦截器链

// addPathPatterns 用于添加拦截规则

// excludePathPatterns 用户排除拦截

registry.addInterceptor(new RateLimitInterceptor()).addPathPatterns(“/**”);

super.addInterceptors(registry);

}

}

4.写一个Controller来提供一个简 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 单的访问接口

/**

  • request测试专用

  • @return

*/

@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的枚举:

你可能感兴趣的:(Java,经验分享,架构,java)