• 使用google的guova开发高并发下的接口限流


    使用google的guova开发高并发下的接口限流

    使用google的guova进行限流

    1、guova的限流方式,在定时产生定量的令牌,令牌的数量限制了流量

    2、增加一个订单接口限流类OrderRateFilter,继承ZuulFilter,并重载方法;filterType、filterOrder、shouldFilter、run

        filterType中return PRE_TYPE;

        fileterOrder中应该优先级最高,设为-4

        shouldFilter中设置限流的方法(类似于鉴权)

         RequestContext requestContext= RequestContext.getCurrentContext();

    HttpServletRequest httpServletRequest=requestContext.getRequest();

    //System.out.println(httpServletRequest.getRequestURI());///apigateway/order/api/v1/orderfeignhystrix/save

    //System.out.println(httpServletRequest.getRequestURL());//http://192.168.136.128:9000/apigateway/order/api/v1/orderfeignhystrix/save

     

    if ("/apigateway/order/api/v1/orderfeignhystrix/save".equalsIgnoreCase(httpServletRequest.getRequestURI()))

    {

    return true;

    }

        run中设置 ,获取令牌

        guava中需要增加令牌设置

        //每秒1000个令牌

        private static final RateLimiter RATE_LIMITER=RateLimiter.create(1000);

        

        在run中增加RATELIMITER.tryActquire();

            如果未取到这返回过多的访问

              

              

    if (!RATE_LIMITER.tryAcquire())

    {

    RequestContext requestContext=RequestContext.getCurrentContext();

    requestContext.setSendZuulResponse(false);

    requestContext.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());

    }

  • 相关阅读:
    开发者最好的推广平台
    [ERR] 2006
    PS通道
    PS图层样式
    PS 图层 蒙版
    科研狗的基本绘图技巧 | PS | AI
    memcached的常规操作:增删改查【转】
    mysql:pt-online-schema-change 在线修改表、删除表数据【转】
    HAProxy的四层与七层的区别及透传IP实战案例【转】
    【springBoot】SpringBoot修改启动logo图案
  • 原文地址:https://www.cnblogs.com/programer-xinmu78/p/10551551.html
Copyright © 2020-2023  润新知