我在我的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我通过postman调用了GET API并检查了响应头。
X-RateLimit-Remaining -1
X-RateLimit-Burst-Capacity 3
X-RateLimit-Replenish-Rate 2速率限制不起作用。为什么我会得到X-RateLimit-Remaining的负值?什么意思?我该如何修复它?
发布于 2020-04-27 06:24:06
这发生在我身上,因为没有启动Redis实例。您有两个选择:
1)使用docker下载并运行Redis实例:
docker run --name redis -d redis2)您可以在测试嵌入式Redis Server时使用,如following article中所述,只需添加maven依赖:
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.2</version>
<scope>test</scope>
</dependency>并包含以下代码片段:
@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();
}
}发布于 2020-05-18 17:54:33
最近我也遇到了同样的问题。在我的例子中,安装了一个较旧版本的Redis,导致X-RateLimit-Remaining不断被设置为-1。
redis-cli shutdownhttps://stackoverflow.com/questions/60716588
复制相似问题