Zuul 限流

时间:2021-02-27 20:04:39
/**
* 限流
*/
@Component
public class RateLimiterFilter extends ZuulFilter { //1秒钟100个令牌
private static final RateLimiter RATE_LIMITER = RateLimiter.create(100);
@Override
public String filterType() {
return PRE_TYPE;
} @Override
public int filterOrder() {
return SERVLET_DETECTION_FILTER_ORDER -1;
} @Override
public boolean shouldFilter() {
return true;
} @Override
public Object run() throws ZuulException {
if(!RATE_LIMITER.tryAcquire()){
throw new RateLimitException();
}
return null;
}
}