Unable to set RequestRateLimiter as default filters via configuration properties
See original GitHub issuein Finchley.RC1, I found that I can’t configure defaultFilters with RequestRateLimiter like this
default-filters:
- name: RequestRateLimiter
args:
burstCapacity: 2
replenishRate: 2
keyResolver: '#{@userKeyResolver}'
because AbstractRateLimiter#onApplicationEvent would not put the routeId and routeConfig to the config map.
@Override
public void onApplicationEvent(FilterArgsEvent event) {
Map<String, Object> args = event.getArgs();
// only defaultFilters is ok, others will return
if (args.isEmpty() || !hasRelevantKey(args)) {
return;
}
String routeId = event.getRouteId();
C routeConfig = newConfig();
ConfigurationUtils.bind(routeConfig, args,
configurationPropertyName, configurationPropertyName, validator);
getConfig().put(routeId, routeConfig);
}
private boolean hasRelevantKey(Map<String, Object> args) {
return args.keySet().stream()
.anyMatch(key -> key.startsWith(configurationPropertyName + "."));
}
if only add RequestRateLimiter to defaultFilters, only the defaultFilters contains the key configurationPropertyName
, others don’t have the key so they are not in config map.
but RedisRateLimiter#isAllowed can’t get the routeConfig through the specified routeId so we will get an exception.
public Mono<Response> isAllowed(String routeId, String id) {
if (!this.initialized.get()) {
throw new IllegalStateException("RedisRateLimiter is not initialized");
}
// the routeConfig can only be obtained when the routeId is "defaultFilters"
Config routeConfig = getConfig().get(routeId);
if (routeConfig == null) {
if (defaultConfig == null) {
throw new IllegalArgumentException("No Configuration found for route " + routeId);
}
routeConfig = defaultConfig;
}
// ...
}
exception
java.lang.IllegalArgumentException: No Configuration found for route service_customer
at org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter.isAllowed(RedisRateLimiter.java:93) ~[spring-cloud-gateway-core-2.0.0.RC1.jar:2.0.0.RC1]
is it a bug or the designer deliberately did so?
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Can't enable RequestRateLimiter filter in Spring Cloud Gateway
I think I know what your problem is. Did you configure a key resolver? If not, that filter will return 403 by default...
Read more >5. GatewayFilter Factories - Spring Cloud
Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a...
Read more >Spring Cloud Gateway Custom Api limiter - Medium
To set a new KeyResolver, we need to create a bean of KeyResolver class. Here's the example. ApiLimiterConfiguration.javapackage com.faza.
Read more >Rate Limiting In Spring Cloud Gateway With Redis
When using RequestRateLimiter with Spring Cloud Gateway we may leverage ... .filter.request-rate-limiter.empty-key-status-code properties.
Read more >Spring Cloud Gateway WebFilter Factories - Baeldung
In this sense, it offers a set of interesting functionalities to apply ... This filter can only be configured using the Java DSL...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@spencergibb But it doesn’t work, cannot find configuration for route.
@spencergibb Yes, I have read this configuration. But this configuration is for the specified route, this can work. However, what I said is that the default-filters are not for the specified route.