2021-02-26 21:40:08 +08:00
|
|
|
|
---
|
|
|
|
|
title: limit-req
|
|
|
|
|
---
|
|
|
|
|
|
2019-10-31 09:27:28 +08:00
|
|
|
|
<!--
|
|
|
|
|
#
|
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
-->
|
|
|
|
|
|
2021-02-26 21:40:08 +08:00
|
|
|
|
## 目录
|
2021-01-03 11:26:40 +08:00
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
- [简介](#简介)
|
|
|
|
|
- [属性](#属性)
|
|
|
|
|
- [示例](#示例)
|
|
|
|
|
- [如何在 `route` 或 `service` 上使用](#如何在`route`或`service`上使用)
|
|
|
|
|
- [如何在 `consumer` 上使用](#如何在`consumer`上使用)
|
|
|
|
|
- [移除插件](#移除插件)
|
|
|
|
|
|
|
|
|
|
## 简介
|
2019-06-06 10:57:40 +08:00
|
|
|
|
|
|
|
|
|
限制请求速度的插件,使用的是漏桶算法。
|
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
## 属性
|
2020-03-20 23:03:59 +08:00
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
|
|
|
|
|
| ------------- | ------- | ------ | ------ | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- |
|
2021-01-29 22:31:01 +08:00
|
|
|
|
| rate | integer | 必须 | | rate > 0 | 指定的请求速率(以秒为单位),请求速率超过 `rate` 但没有超过 (`rate` + `brust`)的请求会被加上延时。 |
|
|
|
|
|
| burst | integer | 必须 | | burst >= 0 | t请求速率超过 (`rate` + `brust`)的请求会被直接拒绝。 |
|
2020-10-06 18:18:22 +08:00
|
|
|
|
| key | string | 必须 | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for", "consumer_name"] | 用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端IP地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP","consumer_name"(consumer 的 username)。 |
|
2021-01-29 22:31:01 +08:00
|
|
|
|
| rejected_code | integer | 可选 | 503 | [200,...,599] | 当请求超过阈值被拒绝时,返回的 HTTP 状态码。 |
|
2019-06-06 10:57:40 +08:00
|
|
|
|
|
2020-06-16 13:36:16 +08:00
|
|
|
|
**key 是可以被用户自定义的,只需要修改插件的一行代码即可完成。并没有在插件中放开是处于安全的考虑。**
|
|
|
|
|
|
2020-03-20 23:03:59 +08:00
|
|
|
|
## 示例
|
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
### 如何在`route`或`service`上使用
|
2019-06-06 10:57:40 +08:00
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
这里以`route`为例(`service`的使用是同样的方法),在指定的 `route` 上启用 `limit-req` 插件。
|
2019-06-06 10:57:40 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-06-06 10:57:40 +08:00
|
|
|
|
{
|
2019-09-03 13:53:41 +08:00
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/index.html",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"limit-req": {
|
|
|
|
|
"rate": 1,
|
|
|
|
|
"burst": 2,
|
|
|
|
|
"rejected_code": 503,
|
|
|
|
|
"key": "remote_addr"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"39.97.63.215:80": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-06 10:57:40 +08:00
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-19 01:17:05 +08:00
|
|
|
|
你可以使用浏览器打开 dashboard:`http://127.0.0.1:9080/apisix/dashboard/`,通过 web 界面来完成上面的操作,先增加一个 route:
|
|
|
|
|
|
2021-02-25 16:32:21 +08:00
|
|
|
|
![添加路由](../../../assets/images/plugin/limit-req-1.png)
|
2019-09-19 01:17:05 +08:00
|
|
|
|
|
|
|
|
|
然后在 route 页面中添加 limit-req 插件:
|
|
|
|
|
|
2021-02-25 16:32:21 +08:00
|
|
|
|
![添加插件](../../../assets/images/plugin/limit-req-2.png)
|
2020-03-20 23:03:59 +08:00
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
**测试插件**
|
2019-09-19 01:17:05 +08:00
|
|
|
|
|
2019-06-06 10:57:40 +08:00
|
|
|
|
上述配置限制了每秒请求速率为 1,大于 1 小于 3 的会被加上延时,速率超过 3 就会被拒绝:
|
2020-03-20 23:03:59 +08:00
|
|
|
|
|
2019-06-06 10:57:40 +08:00
|
|
|
|
```shell
|
|
|
|
|
curl -i http://127.0.0.1:9080/index.html
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
当你超过,就会收到包含 503 返回码的响应头:
|
2020-03-20 23:03:59 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-06-06 10:57:40 +08:00
|
|
|
|
HTTP/1.1 503 Service Temporarily Unavailable
|
|
|
|
|
Content-Type: text/html
|
|
|
|
|
Content-Length: 194
|
|
|
|
|
Connection: keep-alive
|
|
|
|
|
Server: APISIX web server
|
|
|
|
|
|
|
|
|
|
<html>
|
|
|
|
|
<head><title>503 Service Temporarily Unavailable</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
<center><h1>503 Service Temporarily Unavailable</h1></center>
|
|
|
|
|
<hr><center>openresty</center>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这就表示 limit req 插件生效了。
|
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
### 如何在`consumer`上使用
|
|
|
|
|
|
|
|
|
|
consumer上开启`limit-req`插件,需要与授权插件一起配合使用,这里以key-auth授权插件为例。
|
|
|
|
|
|
|
|
|
|
1、将`limit-req`插件绑定到consumer上
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2020-10-06 18:18:22 +08:00
|
|
|
|
{
|
|
|
|
|
"username": "consumer_jack",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"key-auth": {
|
|
|
|
|
"key": "auth-jack"
|
|
|
|
|
},
|
|
|
|
|
"limit-req": {
|
|
|
|
|
"rate": 1,
|
|
|
|
|
"burst": 1,
|
|
|
|
|
"rejected_code": 403,
|
|
|
|
|
"key": "consumer_name"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2、创建`route`并开启`key-auth`插件
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2020-10-06 18:18:22 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/index.html",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"key-auth": {
|
|
|
|
|
"key": "auth-jack"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**测试插件**
|
|
|
|
|
|
|
|
|
|
未超过`rate + burst` 的值
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
curl -i http://127.0.0.1:9080/index.html -H 'apikey: auth-jack'
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
|
......
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
当超过`rate + burst` 的值
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
curl -i http://127.0.0.1:9080/index.html -H 'apikey: auth-jack'
|
|
|
|
|
HTTP/1.1 403 Forbidden
|
|
|
|
|
.....
|
|
|
|
|
<html>
|
|
|
|
|
<head><title>403 Forbidden</title></head>
|
|
|
|
|
<body>
|
|
|
|
|
<center><h1>403 Forbidden</h1></center>
|
|
|
|
|
<hr><center>openresty</center>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
说明绑在`consumer`上的 `limit-req`插件生效了
|
|
|
|
|
|
|
|
|
|
## 移除插件
|
2020-03-20 23:03:59 +08:00
|
|
|
|
|
2019-06-06 10:57:40 +08:00
|
|
|
|
当你想去掉 limit req 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-06-06 10:57:40 +08:00
|
|
|
|
{
|
2019-09-03 13:53:41 +08:00
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/index.html",
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"39.97.63.215:80": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-06 10:57:40 +08:00
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2020-10-06 18:18:22 +08:00
|
|
|
|
移除`consumer`上的 `limit-req` 插件
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2020-10-06 18:18:22 +08:00
|
|
|
|
{
|
|
|
|
|
"username": "consumer_jack",
|
|
|
|
|
"plugins": {
|
|
|
|
|
"key-auth": {
|
|
|
|
|
"key": "auth-jack"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2019-06-06 10:57:40 +08:00
|
|
|
|
现在就已经移除了 limit req 插件了。其他插件的开启和移除也是同样的方法。
|