ZUUL网关API接口防刷限流

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 # 设置为true重定向是会添加host请求头
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
# 对/hello接口每分钟限制5次访问
curl -is http://10.10.10.56:8125/api/produce/hello
# 对其他接口无限制
curl -is http://10.10.10.56:8125/api/produce/

4、Ref参考

  1. https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit/tree/2.2.x
  2. https://www.baeldung.com/spring-cloud-zuul-rate-limit

ZUUL网关API接口防刷限流
https://www.boer.xyz/2021/05/21/zuul-ratelimit/
作者
boer
发布于
2021年5月21日
许可协议