Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在redis中使用spring cloud api网关RateLimit时,响应头中的X-RateLimit-Remaining-1?_Spring_Gateway_Ratelimit - Fatal编程技术网

为什么在redis中使用spring cloud api网关RateLimit时,响应头中的X-RateLimit-Remaining-1?

为什么在redis中使用spring cloud api网关RateLimit时,响应头中的X-RateLimit-Remaining-1?,spring,gateway,ratelimit,Spring,Gateway,Ratelimit,我在SpringCloudAPI网关中使用redis实现了ratelimit。这是应用程序的一部分。yml: spring: cloud: gateway: httpclient: ssl: useInsecureTrustManager: true discovery: locator: enabled: true routes: - id: test-r

我在SpringCloudAPI网关中使用redis实现了ratelimit。这是
应用程序的一部分。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

利率限制不起作用。为什么
X-RateLimit-Remaining
的值为负值?这是什么意思?如何修复它?

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

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

docker run --name redis -d redis
2) 您可以在测试嵌入式Redis服务器时使用,如中所述,添加maven依赖项:

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

我最近也遇到了同样的问题。在我的例子中,安装了一个旧版本的Redis,导致X-RateLimit-Remaining不断地设置为-1

redis-cli shutdown
@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();
    }
}
redis-cli shutdown