SpringCloud of Zuul high concurrency limiting Interface (XII)

Interface highly concurrent limiting technology gauva (Google frame) MySql maximum number of connections 3000;

Principle: put the frame per second token bucket 100, the interface request to pick up a token in order to continue to go back later to get the token, otherwise not allowed back; when the interface too often, they would request not even the token, this time to play a limiting role; we did at the gateway layer, a current-limiting:

1  / * 
2  * line flow restrictor
 . 3   * / 
. 4  @Component
 . 5  public  class OrderRateLimiterFilter the extends ZuulFilter {
 . 6  . 7 . 8 // generated token 1000 per second . 9 Private static Final RateLimiter RATE_LIMITER = RateLimiter.create (1000 );
 10 . 11     @Override
 12 is public String filterType () {
 13 is return PRE_TYPE;
 14     }
 15 16     @Override
 . 17      
                               public int filterOrder() {
18         return -4;
19     }
20 21 22 23     @Override
24     public boolean shouldFilter() {
25 26 27         RequestContext requestContext = RequestContext.getCurrentContext();
28         HttpServletRequest request = requestContext.getRequest();
29 30         //只对订单接口限流
31         if ("/apigateway/api/v1/order/save".equalsIgnoreCase (Request.getRequestURI ())) {
 32              return  to true ;
 33 is          }
 34 is  35 return to false ;
 36     }
 37 [ 38 is     @Override
 39 public Object RUN () throws ZuulException {
 40          the RequestContext requestContext = RequestContext.getCurrentContext ();
 41 is // RATE_LIMITER.tryAcquire () indicates get token immediately, as long as you get interception; may be provided over a period of time to get interception 42 is IF (! RATE_LIMITER.tryAcquire ()) {
 43 is              requestContext.setSendZuulResponse (                           
         false);
44             requestContext.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
45         }
46         return null;
47     }
48 49 50 51 }
52

 

 

Guess you like

Origin www.cnblogs.com/dalianpai/p/11710311.html