【问题标题】:Why X-RateLimit-Remaining -1 in response header while using spring cloud api gateway ratelimit with redis?为什么在使用带有redis的spring cloud api gateway ratelimit时响应标头中的X-RateLimit-Remaining -1?
【发布时间】:2020-06-28 04:36:47
【问题描述】:

我在我的 spring cloud api 网关中使用 redis 实现了 ratelimit。这是application.yml的一部分:

spring:  
  cloud:
    gateway:
      httpclient:
        ssl:
          useInsecureTrustManager: true
      discovery:
        locator:
          enabled: true
      routes:
        - id: test-rest-service
          uri: lb://test-rest-service
          predicates:
            - Path=/test/**
          filters:
            - RewritePath=/test/(?<path>.*), /$\{path}
            - name: RequestRateLimiter
              args:
                key-resolver: "#{@userRemoteAddressResolver}"
                redis-rate-limiter.replenishRate: 2
                redis-rate-limiter.burstCapacity: 3

我通过邮递员调用了一个 GET API 并检查了响应标头。

X-RateLimit-Remaining -1
X-RateLimit-Burst-Capacity 3
X-RateLimit-Replenish-Rate 2

速率限制不起作用。为什么我的 X-RateLimit-Remaining 为负值?这是什么意思?我该如何解决?

【问题讨论】:

    标签: spring gateway ratelimit


    【解决方案1】:

    这发生在我身上,因为没有启动 Redis 实例。你有两个选择:

    1) 使用 docker 下载并运行 Redis 实例:

    docker run --name redis -d redis
    

    2) 您可以通过添加 maven 依赖项来测试 Embedded Redis 服务器,正如 following article 中所解释的那样:

    <dependency>
      <groupId>it.ozimov</groupId>
      <artifactId>embedded-redis</artifactId>
      <version>0.7.2</version>
      <scope>test</scope>
    </dependency>
    

    并包括以下sn-p:

    @TestConfiguration
    public class TestRedisConfiguration {
    
        private RedisServer redisServer;
    
        public TestRedisConfiguration() {
            this.redisServer = new RedisServer(6379);
        }
    
        @PostConstruct
        public void postConstruct() {
            redisServer.start();
        }
    
        @PreDestroy
        public void preDestroy() {
            redisServer.stop();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我最近遇到了同样的问题。就我而言,安装了旧版本的 Redis,导致 X-RateLimit-Remaining 不断设置为 -1。

      redis-cli shutdown
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多