zoukankan      html  css  js  c++  java
  • 【转载】Spring Cloud Gateway限流详解

    https://www.imooc.com/article/290828/

    Spring Cloud Gateway限流详解

    2019.08.11 12:56 7257浏览
     

    Spring Cloud Gatway内置的 RequestRateLimiterGatewayFilterFactory 提供限流的能力,基于令牌桶算法实现。目前,它内置的 RedisRateLimiter ,依赖Redis存储限流配置,以及统计数据。当然你也可以实现自己的RateLimiter,只需实现 org.springframework.cloud.gateway.filter.ratelimit.RateLimiter 接口,或者继承 org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter 。

    漏桶算法:

    想象有一个水桶,水桶以一定的速度出水(以一定速率消费请求),当水流速度过大水会溢出(访问速率超过响应速率,就直接拒绝)。

    漏桶算法的两个变量:

    • 水桶漏洞的大小:rate
    • 最多可以存多少的水:burst

    令牌桶算法:

    系统按照恒定间隔向水桶里加入令牌(Token),如果桶满了的话,就不加了。每个请求来的时候,会拿走1个令牌,如果没有令牌可拿,那么就拒绝服务。

    TIPS

    • Redis Rate Limiter的实现基于这篇文章: Stripe
    • Spring官方引用的令牌桶算法文章: Token Bucket Algorithm ,有兴趣可以看看。

    写代码

    • 加依赖:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
      </dependency>
      
    • 写配置:

      spring:
        cloud:
          gateway:
            routes:
              - id: after_route
                uri: lb://user-center
                predicates:
                  - TimeBetween=上午0:00,下午11:59
                filters:
                  - AddRequestHeader=X-Request-Foo, Bar
                  - name: RequestRateLimiter
                    args:
                      # 令牌桶每秒填充平均速率
                      redis-rate-limiter.replenishRate: 1
                      # 令牌桶的上限
                      redis-rate-limiter.burstCapacity: 2
                      # 使用SpEL表达式从Spring容器中获取Bean对象
                      key-resolver: "#{@pathKeyResolver}"
        redis:
          host: 127.0.0.1
          port: 6379
      
    • 写代码:按照X限流,就写一个针对X的KeyResolver。

      @Configuration
      public class Raonfiguration {
          /**
           * 按照Path限流
           *
           * @return key
           */
          @Bean
          public KeyResolver pathKeyResolver() {
              return exchange -> Mono.just(
                  exchange.getRequest()
                      .getPath()
                      .toString()
              );
          }
      }
      
    • 这样,限流规则即可作用在路径上。

      例如:
      # 访问:http://${GATEWAY_URL}/users/1,对于这个路径,它的redis-rate-limiter.replenishRate = 1,redis-rate-limiter.burstCapacity = 2;
      # 访问:http://${GATEWAY_URL}/shares/1,对这个路径,它的redis-rate-limiter.replenishRate = 1,redis-rate-limiter.burstCapacity = 2;
      

    测试

    持续高速访问某个路径,速度过快时,返回 HTTP ERROR 429 。

    拓展

    你也可以实现针对用户的限流:

    @Bean
    public KeyResolver userKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }
    

    针对来源IP的限流:

    @Bean
    public KeyResolver ipKeyResolver() {
      return exchange -> Mono.just(
        exchange.getRequest()
        .getHeaders()
        .getFirst("X-Forwarded-For")
      );
    }
    

    相关文章

    本文首发

  • 相关阅读:
    JetBrains注册码计算(IntelliJ IDEA 15.0注册码激活)
    java分页数据导出excel
    linux系统关机与重新启动命令
    无向图的连通性分析
    流域水文模拟
    深信服笔试题(网络project师售后)
    CSS这些代码你都不会,你还有什么好说的!!!
    springMVC3学习(四)--訪问静态文件如js,jpg,css
    POJ 3311 Hie with the Pie(状压DP + Floyd)
    NSDictionary所有API的学习。
  • 原文地址:https://www.cnblogs.com/exmyth/p/13577298.html
Copyright © 2011-2022 走看看