1、引入maven依赖
<dependency>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<!--<version>2.1.0.RELEASE</version>-->
<version>1.3.2.RELEASE</version>
</dependency>
<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--连接池common-pool,因为不管是Jedis也好,还是Lettuce,底层都会基于commons-pool来实现连接池效果-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2、引入配置
spring:
redis:
host: your ip
port: 6379
database: 0
password: your password
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
zuul:
ratelimit:
enabled: true
repository: REDIS # 使用 Redis 作为限流存储
behind-proxy: false # 如果 Zuul 跑在代理后面,如 Nginx,则需要设置为 true
default-policy: #可选 - 针对所有的路由配置的策略,除非特别配置了policies
limit: 50 #可选 - 每个刷新时间窗口对应的请求数量限制
quota: -1 #可选- 每个刷新时间窗口对应的请求时间限制(秒)
refresh-interval: 60 # 刷新时间窗口的时间,默认值 (秒)
type: #可选 限流方式
- origin
- url