Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set RequestRateLimiter as default filters via configuration properties #310

Closed
zhaoyibo opened this issue May 9, 2018 · 8 comments

Comments

@zhaoyibo
Copy link

zhaoyibo commented May 9, 2018

in 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?

@starslau
Copy link

I also encountered this problem, how did you solve it later?

@SoftwareKing
Copy link

@zhaoyibo 方便加你微信交流一下吗?我微信是Software_King

@zhaoyibo
Copy link
Author

zhaoyibo commented May 17, 2018

@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.

@spencergibb
Copy link
Member

It's the same, just under the default routes

@zhaoyibo
Copy link
Author

@spencergibb But it doesn't work, cannot find configuration for route.

@spencergibb
Copy link
Member

I see.

@keets2012
Copy link

@spencergibb have the problem been solved ?

Johnny850807 pushed a commit to Johnny850807/spring-cloud-gateway that referenced this issue Dec 14, 2020
* Polish + Fixed service-registry endpoint + Added features endpoint docs

Fixes spring-cloud#53 
Fixes spring-cloud#310
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants