标签:status mamicode filter public set creat bool val override
mysql最大连接是有限制的
Google限流框架 Guava
/** * 订单限流 */ public class OrderRateLimiterFilter extends ZuulFilter { //每秒产生1000个令牌 private static final RateLimiter RATE_LIMITER = RateLimiter.create(1000) @Override public String filterType() { //前置限流 return PRE_TYPE; } @Override public int filterOrder() { return -4; } @Override public boolean shouldFilter() { RequestContext requestContext = RequestContext.getCurrentContext(); HttpServletRequest request = requestContext.getRequest(); //只对订单接口限流 if ("/apizuul/order/api/v1/order/save".equalsIgnoreCase(request.getRequestURI())){ //拦截 return true; } return false; } @Override public Object run() throws ZuulException { RequestContext requestContext = RequestContext.getCurrentContext(); if (!RATE_LIMITER.tryAcquire()){ //没有拿到令牌 requestContext.setSendZuulResponse(false); //不会往下执行 requestContext.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value()); } return null; }
标签:status mamicode filter public set creat bool val override
原文地址:https://www.cnblogs.com/zhuomuniao/p/12453041.html