SpringCloud ------ Zuul filter combined with Google Gauva limiting reality

premise:

Gateway has been configured Zuul

reference:

https://www.cnblogs.com/tianhengblogs/p/12495370.html

 

Limiting way:

1) nginx limiting layer

2) limiting the gateway layer

 

1. Add restrictor filter

import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * Orders limiting Interface
 */
@Component
public  class Order Rate Limiter filter extends ZuulFilter {

    // generates tokens per 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 context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        ///apigateway/order/api/v1/order/test
        System.out.println(request.getRequestURI());
        //http://127.0.0.1:9000/apigateway/order/api/v1/order/test
        System.out.println(request.getRequestURL());

        // Interface limiting the 
        List <String> = NOFILTER new new the ArrayList <> ();
        noFIlter.add("/apigateway/order/**");

        AntPathMatcher matcher = new AntPathMatcher();
        for (String pattern : noFIlter) {//pattern--/user/**
            if (StringUtils.isNotEmpty(pattern)
                    && matcher.match(pattern, request.getRequestURI())) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Object run() throws ZuulException {

        // can be tested using JMeter 
        the RequestContext context = RequestContext.getCurrentContext ();
         // time tryAcquire maximum flow restrictor immediately, the configuration parameters may be 
        IF (! RATE_LIMITER.tryAcquire ()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 429);
            result.put ( "msg", "at present the volume of traffic, limiting the ..." );

            context.setSendZuulResponse ( false );
            context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
            context.setResponseBody(JSON.toJSONString(result));
            // solve the Chinese garbled 
            context.getResponse () setCharacterEncoding ( "UTF-8." );
            context.getResponse().setContentType("text/html;charset=UTF-8");
        }

        return null;
    }
}

 

Guess you like

Origin www.cnblogs.com/tianhengblogs/p/12501235.html