spring-cloud-zuul-ratelimit 配置
1、 添加spring-cloud-zuul-ratelimit依赖。
<dependency>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>1.3.2.RELEASE</version>
2、 全局限流配置
#全局限流:
zuul.ratelimit.enabled=true
zuul.ratelimit.behind-proxy=true
zuul.ratelimit.repository=REDIS
zuul.ratelimit.add-response-headers=true
zuul.ratelimit.default-policy.limit=7
zuul.ratelimit.default-policy.quota=1000
zuul.ratelimit.default-policy.refresh-interval=60
zuul.ratelimit.default-policy.type=origin
3、 特定服务限流配置
#局部限流:针对某个服务进行限流
#60s 内请求超过 3 次,服务端就抛出异常,60s 后可以恢复正常请求
zuul.ratelimit.policies.api-b.limit=3
zuul.ratelimit.policies.api-b.quota = 1000
zuul.ratelimit.policies.api-b.refresh-interval=60
#针对某个 IP 进行限流,不影响其他 IP
zuul.ratelimit.policies.api-b.type=origin
4,完整示例:
server.port=9527
spring.application.name=gateway
eureka.client.serviceUrl.defaultZone=http://localhost:6660/eureka/
eureka.instance.instance-id = gateway-9527.com
eureka.instance.prefer-ip-address=true
zuul.routes.api-a.path =/aaaa/**
zuul.routes.api-a.service-id =saas
zuul.routes.api-b.path =/bbbb/**
zuul.routes.api-b.service-id =lula
#全局限流:
zuul.ratelimit.enabled=true
zuul.ratelimit.behind-proxy=true
zuul.ratelimit.repository=REDIS
zuul.ratelimit.add-response-headers=true
zuul.ratelimit.default-policy.limit=7
zuul.ratelimit.default-policy.quota=1000
zuul.ratelimit.default-policy.refresh-interval=60
zuul.ratelimit.default-policy.type=origin
#局部限流:针对某个服务进行限流
#60s 内请求超过 3 次,服务端就抛出异常,60s 后可以恢复正常请求
zuul.ratelimit.policies.api-b.limit=3
zuul.ratelimit.policies.api-b.quota = 1000
zuul.ratelimit.policies.api-b.refresh-interval=60
#针对某个 IP 进行限流,不影响其他 IP
zuul.ratelimit.policies.api-b.type=origin
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=