spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

How can I configure different RedisRateLimiter to apply to different routes? #3413

Closed ethan810818 closed 3 months ago

ethan810818 commented 4 months ago

Describe the bug: I get an error when I try to manually configure the RedisRateLimiter and include it in setRateLimiter() ERROR: java.lang.IllegalStateException: RedisRateLimiter is not initialized

I used a fire-fighting solution called rateLimiter and it worked. However, that caused another problem:

CODE: https://github.com/bmt810818/spring-cloud-gateway

version: spring-cloud-gateway-server-4.0.7.jar:4.0.7

dependencies: implementation 'org.springframework.boot:spring-boot-starter-data-redis'

ethan810818 commented 4 months ago

rateLimiter easily fetches auth if auth is the only configuration. But when there are many specific configurations (merchant, page), how will rateLimiter handle them?

Any answer is helpful to me. Thank you for viewing this post

pandrez commented 3 months ago

Hi @bmt810818!

You can inject the ApplicationContextBean in your factory methods and call setApplicationContexton the RedisRateLimiter instance. This way the redisRateLimiter will be initialized.

Hope this helps