如何在 krakenD 中对后端配置应用速率限制
How to apply rate limits on backend configuration in krakenD
我有一个 krakend 端点配置如下
{
"endpoint": "/api/login",
"method": "POST",
"output_encoding": "no-op",
"headers_to_pass": [
"*"
],
"backend": [
{
"encoding": "no-op",
"host": [ "http://login-service:80" ],
"url_pattern": "/api/login",
"method": "POST",
"extra_config": {
"github.com/devopsfaith/krakend-ratelimit/juju/router": {
"maxRate": 2,
"capacity": 2,
"clientMaxRate": 1,
"strategy": "header",
"key": "x-real-ip"
}
}
}
]
}
我点击了这些链接来形成配置
https://www.krakend.io/docs/backends/rate-limit/
https://www.krakend.io/docs/endpoints/rate-limit/
但问题是我没有看到配置工作。
对于上述配置,每秒允许的最大请求数为 2,但是,端点每秒接受超过 2 个请求而不显示任何错误消息。根据文档,如果触发限制的人达到 maxRate 限制,它应该显示 503 Service Unavailable。
您的额外配置似乎放错了地方。
路由器速率限制进入端点内部,而不是后端。使用这个:
{
"endpoint": "/api/login",
"method": "POST",
"output_encoding": "no-op",
"headers_to_pass": [
"*"
],
"extra_config": {
"github.com/devopsfaith/krakend-ratelimit/juju/router": {
"maxRate": 2,
"capacity": 2,
"clientMaxRate": 1,
"strategy": "header",
"key": "x-real-ip"
}
},
"backend": [
{
"encoding": "no-op",
"host": [
"http://login-service:80"
],
"url_pattern": "/api/login",
"method": "POST"
}
]
}
我有一个 krakend 端点配置如下
{
"endpoint": "/api/login",
"method": "POST",
"output_encoding": "no-op",
"headers_to_pass": [
"*"
],
"backend": [
{
"encoding": "no-op",
"host": [ "http://login-service:80" ],
"url_pattern": "/api/login",
"method": "POST",
"extra_config": {
"github.com/devopsfaith/krakend-ratelimit/juju/router": {
"maxRate": 2,
"capacity": 2,
"clientMaxRate": 1,
"strategy": "header",
"key": "x-real-ip"
}
}
}
]
}
我点击了这些链接来形成配置 https://www.krakend.io/docs/backends/rate-limit/ https://www.krakend.io/docs/endpoints/rate-limit/
但问题是我没有看到配置工作。 对于上述配置,每秒允许的最大请求数为 2,但是,端点每秒接受超过 2 个请求而不显示任何错误消息。根据文档,如果触发限制的人达到 maxRate 限制,它应该显示 503 Service Unavailable。
您的额外配置似乎放错了地方。 路由器速率限制进入端点内部,而不是后端。使用这个:
{
"endpoint": "/api/login",
"method": "POST",
"output_encoding": "no-op",
"headers_to_pass": [
"*"
],
"extra_config": {
"github.com/devopsfaith/krakend-ratelimit/juju/router": {
"maxRate": 2,
"capacity": 2,
"clientMaxRate": 1,
"strategy": "header",
"key": "x-real-ip"
}
},
"backend": [
{
"encoding": "no-op",
"host": [
"http://login-service:80"
],
"url_pattern": "/api/login",
"method": "POST"
}
]
}