limit-count

以下示例展示了如何在指定路由上启用 limit-count 插件,并设置 key_type"var"

你也可以设置 key_type"var_combination"

  1. curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/index.html",
  5. "plugins": {
  6. "limit-count": {
  7. "count": 2,
  8. "time_window": 60,
  9. "rejected_code": 503,
  10. "key_type": "var_combination",
  11. "key": "$consumer_name $remote_addr"
  12. }
  13. },
  14. "upstream": {
  15. "type": "roundrobin",
  16. "nodes": {
  17. "127.0.0.1:9001": 1
  18. }
  19. }
  20. }'

支持在多个路由间共享同一个限流计数器。首先通过以下命令创建一个服务:

  1. curl -i http://127.0.0.1:9180/apisix/admin/services/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "count": 1,
  6. "time_window": 60,
  7. "rejected_code": 503,
  8. "key": "remote_addr",
  9. "group": "services_1#1640140620"
  10. }
  11. },
  12. "upstream": {
  13. "type": "roundrobin",
  14. "nodes": {
  15. "127.0.0.1:1980": 1
  16. }
  17. }
  18. }'

然后为路由配置 service_id1 ,不同路由将共享同一个计数器:

  1. curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/hello"
  5. }'
  1. curl -i http://127.0.0.1:9180/apisix/admin/services/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "limit-count": {
  6. "count": 1,
  7. "time_window": 60,
  8. "rejected_code": 503,
  9. "key": "remote_addr",
  10. "key_type": "constant",
  11. "group": "services_1#1640140621"
  12. }
  13. },
  14. "upstream": {
  15. "type": "roundrobin",
  16. "nodes": {
  17. "127.0.0.1:1980": 1
  18. }
  19. }
  20. }'

以上配置表示:当多个路由中 limit-count 插件的 group 属性均配置为 services_1#1640140620 时,访问这些路由的请求将会共享同一个计数器,即使这些请求来自于不同的 IP 地址。

注意

同一个 group 里面的 limit-count 的配置必须保持一致。如果修改配置,需要同时更新对应的 group 的值。

如果你需要一个集群级别的流量控制,我们可以借助 Redis 服务器来完成。不同的 APISIX 节点之间将共享流量限速结果,实现集群流量限速。

以下示例展示了如何在指定路由上启用 redis 策略:

  1. curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/index.html",
  5. "plugins": {
  6. "limit-count": {
  7. "count": 2,
  8. "time_window": 60,
  9. "rejected_code": 503,
  10. "key": "remote_addr",
  11. "policy": "redis",
  12. "redis_host": "127.0.0.1",
  13. "redis_port": 6379,
  14. "redis_password": "password",
  15. "redis_database": 1,
  16. "redis_timeout": 1001
  17. }
  18. "upstream": {
  19. "type": "roundrobin",
  20. "nodes": {
  21. "127.0.0.1:1980": 1
  22. }
  23. }
  24. }'
  1. curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. {
  3. "uri": "/index.html",
  4. "plugins": {
  5. "limit-count": {
  6. "count": 2,
  7. "time_window": 60,
  8. "rejected_code": 503,
  9. "key": "remote_addr",
  10. "policy": "redis-cluster",
  11. "redis_cluster_nodes": [
  12. "127.0.0.1:5000",
  13. "127.0.0.1:5001"
  14. ],
  15. "redis_password": "password",
  16. "redis_cluster_name": "redis-cluster-1"
  17. }
  18. },
  19. "upstream": {
  20. "type": "roundrobin",
  21. "nodes": {
  22. "127.0.0.1:1980": 1
  23. }
  24. }
  25. }'

在上文提到的配置中,其限制了 60 秒内请求只能访问 2 次,可通过如下 curl 命令测试请求访问:

在执行测试命令的前两次都会正常访问。其中响应头中包含了 X-RateLimit-LimitX-RateLimit-RemainingX-RateLimit-Reset 字段,分别代表限制的总请求数和剩余还可以发送的请求数以及计数器剩余重置的秒数:

  1. HTTP/1.1 200 OK
  2. Content-Type: text/html
  3. Content-Length: 13175
  4. Connection: keep-alive
  5. X-RateLimit-Limit: 2
  6. X-RateLimit-Remaining: 0
  7. X-RateLimit-Reset: 58
  8. Server: APISIX web server

当第三次进行测试访问时,会收到包含 503 HTTP 状态码的响应头,目前在拒绝的情况下,也会返回相关的头,表示插件生效:

  1. HTTP/1.1 503 Service Temporarily Unavailable
  2. Content-Type: text/html
  3. Content-Length: 194
  4. Connection: keep-alive
  5. X-RateLimit-Limit: 2
  6. X-RateLimit-Remaining: 0
  7. X-RateLimit-Reset: 58
  8. Server: APISIX web server

如果你设置了属性 rejected_msg 的值为 "Requests are too frequent, please try again later.",当第三次访问时,就会收到如下带有 error_msg 返回信息的响应体:

  1. HTTP/1.1 503 Service Temporarily Unavailable
  2. Content-Type: text/html
  3. Content-Length: 194
  4. Connection: keep-alive
  5. X-RateLimit-Limit: 2
  6. X-RateLimit-Remaining: 0
  7. X-RateLimit-Reset: 58
  8. Server: APISIX web server
  9. {"error_msg":"Requests are too frequent, please try again later."}