Spring Cloud Zuul RateLimit对Zuul APIGateway进行限流
1、POM依赖
1 2 3 4 5 6 7 8 9 10 11 12
| # Spring Cloud Zuul RateLimit <dependency> <groupId>com.marcosbarbero.cloud</groupId> <artifactId>spring-cloud-zuul-ratelimit</artifactId> <version>2.2.7.RELEASE</version> </dependency>
# Redis <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
2、YAML配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| spring: application: name: springcloud-zuul profiles: active: ${ENVIRONMENT:pro} redis: host: 10.10.253.16 port: 6379 server: port: 8125 zuul: prefix: /api ignored-services: springboot-produce, springboot-consume add-host-header: true routes: springboot-produce: path: /produce/** springboot-consume: path: /consume/** ratelimit: enabled: true repository: REDIS key-prefix: zrl- behind-proxy: true add-response-headers: false deny-request: response-status-code: 404 origins: - boer.xyz default-policy-list: - limit: 20 refresh-interval: 30 type: - origin - url policy-list: springboot-produce: - limit: 5 refresh-interval: 60 type: - url=/hello
|
3、Test验证
1 2 3 4
| curl -is http://10.10.10.56:8125/api/produce/hello
curl -is http://10.10.10.56:8125/api/produce/
|
4、Ref参考
- https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit/tree/2.2.x
- https://www.baeldung.com/spring-cloud-zuul-rate-limit