🤔 Introducing APISIX AI Gateway – Built for LLMs and AI workloads. Learn More

limit-conn

描述

limit-conn 插件通过并发连接数来限制请求速率。超过阈值的请求将根据配置被延迟或拒绝,从而确保可控的资源使用并防止过载。

属性

名称类型必选项默认值有效值描述
conninteger 或 string整数时 > 0;或 lua-resty-expr允许的最大并发请求数。超过配置的限制且低于 conn + burst 的请求将被延迟。如果未配置 rules,则为必填项。
burstinteger 或 string整数时 >= 0;或 lua-resty-expr允许延迟的过多并发请求数。超过 conn + burst 的请求将被立即拒绝。如果未配置 rules,则为必填项。
default_conn_delaynumber> 0允许超过 conn 且不超过 conn + burst 的并发请求的处理延迟(秒),可根据 only_use_default_delay 设置动态调整。
only_use_default_delaybooleanfalse如果为 false,则根据请求超出 conn 限制的程度按比例延迟请求。拥塞越严重,延迟就越大。例如,当 conn5burst3default_conn_delay1 时,6 个并发请求将导致 1 秒的延迟,7 个请求将导致 2 秒的延迟,8 个请求将导致 3 秒的延迟,依此类推,直到达到 conn + burst 的总限制,超过此限制的请求将被拒绝。如果为 true,则使用 default_conn_delay 延迟 burst 范围内的所有超额请求。超出 conn + burst 的请求将被立即拒绝。例如,当 conn5burst3default_conn_delay1 时,6、7 或 8 个并发请求都将延迟 1 秒。
key_typestringvar[var, var_combination]key 的类型。如果 key_typevar,则 key 将被解释为变量。如果 key_typevar_combination,则 key 将被解释为变量的组合。
keystringremote_addr用于计数请求的 key。如果 key_typevar,则 key 将被解释为变量。变量不需要以美元符号($)为前缀。如果 key_typevar_combination,则 key 会被解释为变量的组合。所有变量都应该以美元符号($)为前缀。例如,要配置 key 使用两个请求头 custom-acustom-b 的组合,则 key 应该配置为 $http_custom_a $http_custom_b。如果未配置 rules,则为必填项。
rejected_codeinteger503[200, …, 599]请求因超出阈值而被拒绝时返回的 HTTP 状态码。
rejected_msgstring非空请求因超出阈值而被拒绝时返回的响应主体。
allow_degradationbooleanfalse如果为 true,则允许 APISIX 在插件或其依赖项不可用时继续处理没有插件的请求。
policystringlocal[local, redis, redis-cluster]速率限制计数器的策略。如果是 local,则计数器存储在本地内存中。如果是 redis,则计数器存储在 Redis 实例上。如果是 redis-cluster,则计数器存储在 Redis 集群中。
redis_hoststringRedis 节点的地址。当 policyredis 时必填。
redis_portinteger6379>= 1policyredis 时,Redis 节点的端口。
redis_usernamestring如果使用 Redis ACL,则为 Redis 的用户名。如果使用旧式身份验证方法 requirepass,则仅配置 redis_password。当 policyredis 时使用。
redis_passwordstringpolicyredisredis-cluster 时,Redis 节点的密码。
redis_sslbooleanfalse如果为 true,则在 policyredis 时使用 SSL 连接到 Redis。
redis_ssl_verifybooleanfalse如果为 true,则在 policyredis 时验证服务器 SSL 证书。
redis_databaseinteger0>= 0policyredis 时,Redis 中的数据库编号。
redis_timeoutinteger1000>= 1policyredisredis-cluster 时,Redis 超时值(以毫秒为单位)。
redis_keepalive_timeoutinteger10000>= 1000policyredisredis-cluster 时,Redis 的空闲连接超时时间(以毫秒为单位)。
redis_keepalive_poolinteger100>= 1policyredisredis-cluster 时,Redis 的连接池最大连接数。
key_ttlinteger3600Redis 键的 TTL(以秒为单位)。当 policyredisredis-cluster 时使用。
redis_cluster_nodesarray[string]至少包含一个地址的 Redis 集群节点列表。当 policyredis-cluster 时必填。
redis_cluster_namestringRedis 集群的名称。当 policyredis-cluster 时必填。
redis_cluster_sslbooleanfalse如果为 true,则在 policyredis-cluster 时使用 SSL 连接到 Redis 集群。
redis_cluster_ssl_verifybooleanfalse如果为 true,则在 policyredis-cluster 时验证服务器 SSL 证书。
rulesarray[object]按顺序应用的速率限制规则数组。从 APISIX 3.16.0 起可用。你应配置以下参数集之一,但不能同时配置两者:connburstdefault_conn_delaykeyrulesdefault_conn_delay
rules.conninteger 或 string> 0 或 lua-resty-expr允许的最大并发请求数。超过配置的限制且低于 conn + burst 的请求将被延迟。该参数也支持 string 数据类型,并允许使用以美元符号($)为前缀的内置变量。
rules.burstinteger 或 string>= 0 或 lua-resty-expr允许延迟的过多并发请求数。超过 conn + burst 的请求将被立即拒绝。该参数也支持 string 数据类型,并允许使用以美元符号($)为前缀的内置变量。
rules.keystring用于计数请求的键。如果配置的键不存在,则不会执行该规则。key 被解释为变量的组合。所有变量都应以美元符号($)为前缀。

示例

以下示例演示了如何在不同场景中配置 limit-conn

NOTE

你可以这样从 config.yaml 中获取 admin_key 并存入环境变量:

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

通过远程地址应用速率限制

以下示例演示如何使用 limit-conn 通过 remote_addr 限制请求速率,并附带示例连接和突发阈值。

创建一个带有 limit-conn 插件的路由:

Admin API

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "limit-conn-route",
    "uri": "/get",
    "plugins": {
      "limit-conn": {
        "conn": 2,
        "burst": 1,
        "default_conn_delay": 0.1,
        "key_type": "var",
        "key": "remote_addr",
        "policy": "local",
        "rejected_code": 429
      }
    },
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "httpbin.org:80": 1
      }
    }
  }'

ADC

adc.yaml
services:
  - name: httpbin
    routes:
      - uris:
          - /get
        name: limit-conn-route
        plugins:
          limit-conn:
            conn: 2
            burst: 1
            default_conn_delay: 0.1
            key_type: var
            key: remote_addr
            policy: local
            rejected_code: 429
    upstream:
      type: roundrobin
      nodes:
        - host: httpbin.org
          port: 80
          weight: 1

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

Gateway API

limit-conn-ic.yaml
apiVersion: v1
kind: Service
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  type: ExternalName
  externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: limit-conn-plugin-config
spec:
  plugins:
    - name: limit-conn
      config:
        conn: 2
        burst: 1
        default_conn_delay: 0.1
        key_type: var
        key: remote_addr
        policy: local
        rejected_code: 429
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /get
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: limit-conn-plugin-config
      backendRefs:
        - name: httpbin-external-domain
          port: 80

APISIX Ingress Controller

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  ingressClassName: apisix
  externalNodes:
  - type: Domain
    name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  ingressClassName: apisix
  http:
    - name: limit-conn-route
      match:
        paths:
          - /get
        methods:
          - GET
      upstreams:
        - name: httpbin-external-domain
      plugins:
        - name: limit-conn
          config:
            conn: 2
            burst: 1
            default_conn_delay: 0.1
            key_type: var
            key: remote_addr
            policy: local
            rejected_code: 429

应用配置:

kubectl apply -f limit-conn-ic.yaml

conn:允许 2 个并发请求。

burst:允许 1 个过多的并发请求。

default_conn_delay:允许超过 connconn + burst 之间的并发请求有 0.1 秒的处理延迟。

key_type:设置为 var,将 key 解释为变量。

key:根据请求的 remote_addr 计算速率限制计数。

policy:使用内存中的本地计数器。

rejected_code:将拒绝状态码设置为 429

向路由发送五个并发请求:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get"'

你应该会看到类似以下内容的响应,其中超过阈值的请求被拒绝:

Response: 200
Response: 200
Response: 200
Response: 429
Response: 429

通过远程地址和消费者名称应用速率限制

以下示例演示如何使用 limit-conn 通过变量组合 remote_addrconsumer_name 对请求进行速率限制。

Admin API

创建消费者 john

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "username": "john"
  }'

为消费者创建 key-auth 凭证:

curl "http://127.0.0.1:9180/apisix/admin/consumers/john/credentials" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "cred-john-key-auth",
    "plugins": {
      "key-auth": {
        "key": "john-key"
      }
    }
  }'

创建第二个消费者 jane

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "username": "jane"
  }'

为消费者创建 key-auth 凭证:

curl "http://127.0.0.1:9180/apisix/admin/consumers/jane/credentials" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "cred-jane-key-auth",
    "plugins": {
      "key-auth": {
        "key": "jane-key"
      }
    }
  }'

创建一个带有 key-authlimit-conn 插件的路由:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "limit-conn-route",
    "uri": "/get",
    "plugins": {
      "key-auth": {},
      "limit-conn": {
        "conn": 2,
        "burst": 1,
        "default_conn_delay": 0.1,
        "rejected_code": 429,
        "policy": "local",
        "key_type": "var_combination",
        "key": "$remote_addr $consumer_name"
      }
    },
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "httpbin.org:80": 1
      }
    }
  }'

ADC

创建两个消费者和一个按消费者进行速率限制的路由:

adc.yaml
consumers:
  - username: john
    credentials:
      - name: key-auth
        type: key-auth
        config:
          key: john-key
  - username: jane
    credentials:
      - name: key-auth
        type: key-auth
        config:
          key: jane-key
services:
  - name: limit-conn-service
    routes:
      - name: limit-conn-route
        uris:
          - /get
        plugins:
          key-auth: {}
          limit-conn:
            conn: 2
            burst: 1
            default_conn_delay: 0.1
            rejected_code: 429
            policy: local
            key_type: var_combination
            key: "$remote_addr $consumer_name"
    upstream:
      type: roundrobin
      nodes:
        - host: httpbin.org
          port: 80
          weight: 1

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

创建两个消费者和一个按消费者进行速率限制的路由:

Gateway API

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
  namespace: aic
  name: john
spec:
  gatewayRef:
    name: apisix
  credentials:
    - type: key-auth
      name: primary-key
      config:
        key: john-key
---
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
  namespace: aic
  name: jane
spec:
  gatewayRef:
    name: apisix
  credentials:
    - type: key-auth
      name: primary-key
      config:
        key: jane-key
---
apiVersion: v1
kind: Service
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  type: ExternalName
  externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: limit-conn-plugin-config
spec:
  plugins:
    - name: key-auth
      config:
        _meta:
          disable: false
    - name: limit-conn
      config:
        conn: 2
        burst: 1
        default_conn_delay: 0.1
        rejected_code: 429
        policy: local
        key_type: var_combination
        key: "$remote_addr $consumer_name"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /get
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: limit-conn-plugin-config
      backendRefs:
        - name: httpbin-external-domain
          port: 80

APISIX Ingress Controller

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
  namespace: aic
  name: john
spec:
  ingressClassName: apisix
  authParameter:
    keyAuth:
      value:
        key: john-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
  namespace: aic
  name: jane
spec:
  ingressClassName: apisix
  authParameter:
    keyAuth:
      value:
        key: jane-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  ingressClassName: apisix
  externalNodes:
  - type: Domain
    name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  ingressClassName: apisix
  http:
    - name: limit-conn-route
      match:
        paths:
          - /get
        methods:
          - GET
      upstreams:
        - name: httpbin-external-domain
      plugins:
        - name: key-auth
          config:
            _meta:
              disable: false
        - name: limit-conn
          config:
            conn: 2
            burst: 1
            default_conn_delay: 0.1
            rejected_code: 429
            policy: local
            key_type: var_combination
            key: "$remote_addr $consumer_name"

应用配置:

kubectl apply -f limit-conn-ic.yaml

key-auth:在路由上启用 key 认证。

key_type:设置为 var_combination,将 key 解释为变量的组合。

key:设置为 $remote_addr $consumer_name,按远程地址和消费者应用速率限制配额。

以消费者 john 的身份发送五个并发请求:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get" -H "apikey: john-key"'

你应该会看到类似以下内容的响应,其中超过阈值的请求被拒绝:

Response: 200
Response: 200
Response: 200
Response: 429
Response: 429

接下来立刻以消费者 jane 的身份发送五个并发请求:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get" -H "apikey: jane-key"'

你还应该看到类似以下内容的响应,其中过多的请求被拒绝:

Response: 200
Response: 200
Response: 200
Response: 429
Response: 429

在此示例中,该插件按变量组合 remote_addrconsumer_name 进行速率限制,这意味着每个消费者的配额是独立的。

限制 WebSocket 连接速率

以下示例演示了如何使用 limit-conn 插件来限制并发 WebSocket 连接的数量。

启动一个示例上游 WebSocket 服务器

docker run -d \
  -p 8080:8080 \
  --name websocket-server \
  --network=apisix-quickstart-net \
  jmalloc/echo-server

该服务器在 /.ws 路径上有一个 WebSocket 端点,会回显收到的任何消息。

创建到服务器 WebSocket 端点的路由,并为路由启用 WebSocket:

Admin API

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "ws-route",
    "uri": "/.ws",
    "plugins": {
      "limit-conn": {
        "conn": 2,
        "burst": 1,
        "default_conn_delay": 0.1,
        "key_type": "var",
        "key": "remote_addr",
        "rejected_code": 429,
        "policy": "local"
      }
    },
    "enable_websocket": true,
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "websocket-server:8080": 1
      }
    }
  }'

❶ 为路由启用 WebSocket。

❷ 替换为你的 WebSocket 服务器地址。

ADC

adc.yaml
services:
  - name: websocket-service
    routes:
      - name: ws-route
        uris:
          - /.ws
        enable_websocket: true
        plugins:
          limit-conn:
            conn: 2
            burst: 1
            default_conn_delay: 0.1
            key_type: var
            key: remote_addr
            rejected_code: 429
            policy: local
    upstream:
      type: roundrobin
      nodes:
        - host: websocket-server
          port: 8080
          weight: 1

❶ 为路由启用 WebSocket。

❷ 替换为你的 WebSocket 服务器地址。

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

Gateway API

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: limit-conn-plugin-config
spec:
  plugins:
    - name: limit-conn
      config:
        conn: 2
        burst: 1
        default_conn_delay: 0.1
        key_type: var
        key: remote_addr
        rejected_code: 429
        policy: local
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: ws-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /.ws
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: limit-conn-plugin-config
      backendRefs:
        - name: websocket-server
          port: 8080

APISIX Ingress Controller

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: ws-route
spec:
  ingressClassName: apisix
  http:
    - name: ws-route
      match:
        paths:
          - /.ws
        methods:
          - GET
      websocket: true
      backends:
        - serviceName: websocket-server
          servicePort: 8080
      plugins:
        - name: limit-conn
          config:
            conn: 2
            burst: 1
            default_conn_delay: 0.1
            key_type: var
            key: remote_addr
            rejected_code: 429
            policy: local

应用配置:

kubectl apply -f limit-conn-ic.yaml

安装 WebSocket 客户端,例如 websocat(如果尚未安装)。通过路由与 WebSocket 服务器建立连接:

websocat "ws://127.0.0.1:9080/.ws"

在终端中发送 “hello” 消息,你应该会看到 WebSocket 服务器回显相同的消息:

Request served by 1cd244052136
hello
hello

再打开三个终端会话并运行:

websocat "ws://127.0.0.1:9080/.ws"

由于速率限制的影响,当你尝试与服务器建立 WebSocket 连接时,你应该会看到最后一个终端会话打印 429 Too Many Requests

使用 Redis 服务器在 APISIX 节点之间共享配额

以下示例演示了使用 Redis 服务器对多个 APISIX 节点之间的请求进行速率限制,以便不同的 APISIX 节点共享相同的速率限制配额。

在每个 APISIX 实例上,使用以下配置创建路由。请相应地调整配置详情。

Admin API

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "limit-conn-route",
    "uri": "/get",
    "plugins": {
      "limit-conn": {
        "conn": 1,
        "burst": 1,
        "default_conn_delay": 0.1,
        "rejected_code": 429,
        "key_type": "var",
        "key": "remote_addr",
        "policy": "redis",
        "redis_host": "192.168.xxx.xxx",
        "redis_port": 6379,
        "redis_password": "p@ssw0rd",
        "redis_database": 1
      }
    },
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "httpbin.org:80": 1
      }
    }
  }'

ADC

adc.yaml
services:
  - name: httpbin
    routes:
      - uris:
          - /get
        name: limit-conn-route
        plugins:
          limit-conn:
            conn: 1
            burst: 1
            default_conn_delay: 0.1
            rejected_code: 429
            key_type: var
            key: remote_addr
            policy: redis
            redis_host: "192.168.xxx.xxx"
            redis_port: 6379
            redis_password: "p@ssw0rd"
            redis_database: 1
    upstream:
      type: roundrobin
      nodes:
        - host: httpbin.org
          port: 80
          weight: 1

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

Gateway API

limit-conn-ic.yaml
apiVersion: v1
kind: Service
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  type: ExternalName
  externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: limit-conn-plugin-config
spec:
  plugins:
    - name: limit-conn
      config:
        conn: 1
        burst: 1
        default_conn_delay: 0.1
        rejected_code: 429
        key_type: var
        key: remote_addr
        policy: redis
        redis_host: "redis-service.aic.svc"
        redis_port: 6379
        redis_password: "p@ssw0rd"
        redis_database: 1
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /get
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: limit-conn-plugin-config
      backendRefs:
        - name: httpbin-external-domain
          port: 80

APISIX Ingress Controller

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  ingressClassName: apisix
  externalNodes:
  - type: Domain
    name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  ingressClassName: apisix
  http:
    - name: limit-conn-route
      match:
        paths:
          - /get
        methods:
          - GET
      upstreams:
        - name: httpbin-external-domain
      plugins:
        - name: limit-conn
          config:
            conn: 1
            burst: 1
            default_conn_delay: 0.1
            rejected_code: 429
            key_type: var
            key: remote_addr
            policy: redis
            redis_host: "redis-service.aic.svc"
            redis_port: 6379
            redis_password: "p@ssw0rd"
            redis_database: 1

应用配置:

kubectl apply -f limit-conn-ic.yaml

policy:设置为 redis,使用 Redis 实例进行速率限制。

redis_host:设置为 Redis 实例的 IP 地址。

redis_port:设置为 Redis 实例的监听端口。

redis_password:设置为 Redis 实例的密码(如有)。

redis_database:设置为 Redis 实例中的数据库编号。

向路由发送五个并发请求:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get"'

你应该会看到类似以下内容的响应,其中超过阈值的请求被拒绝:

Response: 200
Response: 200
Response: 429
Response: 429
Response: 429

这表明在不同 APISIX 实例中配置的两个路由共享相同的配额。

使用 Redis 集群在 APISIX 节点之间共享配额

你还可以使用 Redis 集群在多个 APISIX 节点之间应用相同的配额,以便不同的 APISIX 节点共享相同的速率限制配额。

确保你的 Redis 实例在集群模式下运行。为 limit-conn 插件配置 redis_cluster_nameredis_cluster_nodes 中的一个或多个节点地址。

在每个 APISIX 实例上,使用以下配置创建路由。请相应地调整配置详情。

Admin API

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "limit-conn-route",
    "uri": "/get",
    "plugins": {
      "limit-conn": {
        "conn": 1,
        "burst": 1,
        "default_conn_delay": 0.1,
        "rejected_code": 429,
        "key_type": "var",
        "key": "remote_addr",
        "policy": "redis-cluster",
        "redis_cluster_nodes": [
          "192.168.xxx.xxx:6379",
          "192.168.xxx.xxx:16379"
        ],
        "redis_password": "p@ssw0rd",
        "redis_cluster_name": "redis-cluster",
        "redis_cluster_ssl": true
      }
    },
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "httpbin.org:80": 1
      }
    }
  }'

ADC

adc.yaml
services:
  - name: httpbin
    routes:
      - uris:
          - /get
        name: limit-conn-route
        plugins:
          limit-conn:
            conn: 1
            burst: 1
            default_conn_delay: 0.1
            rejected_code: 429
            key_type: var
            key: remote_addr
            policy: redis-cluster
            redis_cluster_nodes:
              - "192.168.xxx.xxx:6379"
              - "192.168.xxx.xxx:16379"
            redis_password: "p@ssw0rd"
            redis_cluster_name: "redis-cluster"
            redis_cluster_ssl: true
    upstream:
      type: roundrobin
      nodes:
        - host: httpbin.org
          port: 80
          weight: 1

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

Gateway API

limit-conn-ic.yaml
apiVersion: v1
kind: Service
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  type: ExternalName
  externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: limit-conn-plugin-config
spec:
  plugins:
    - name: limit-conn
      config:
        conn: 1
        burst: 1
        default_conn_delay: 0.1
        rejected_code: 429
        key_type: var
        key: remote_addr
        policy: redis-cluster
        redis_cluster_nodes:
          - "redis-cluster-0.redis-cluster.aic.svc:6379"
          - "redis-cluster-1.redis-cluster.aic.svc:6379"
        redis_password: "p@ssw0rd"
        redis_cluster_name: "redis-cluster"
        redis_cluster_ssl: true
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /get
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: limit-conn-plugin-config
      backendRefs:
        - name: httpbin-external-domain
          port: 80

APISIX Ingress Controller

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  ingressClassName: apisix
  externalNodes:
  - type: Domain
    name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: limit-conn-route
spec:
  ingressClassName: apisix
  http:
    - name: limit-conn-route
      match:
        paths:
          - /get
        methods:
          - GET
      upstreams:
        - name: httpbin-external-domain
      plugins:
        - name: limit-conn
          config:
            conn: 1
            burst: 1
            default_conn_delay: 0.1
            rejected_code: 429
            key_type: var
            key: remote_addr
            policy: redis-cluster
            redis_cluster_nodes:
              - "redis-cluster-0.redis-cluster.aic.svc:6379"
              - "redis-cluster-1.redis-cluster.aic.svc:6379"
            redis_password: "p@ssw0rd"
            redis_cluster_name: "redis-cluster"
            redis_cluster_ssl: true

应用配置:

kubectl apply -f limit-conn-ic.yaml

policy:设置为 redis-cluster,使用 Redis 集群进行速率限制。

redis_cluster_nodes:设置为 Redis 集群中的 Redis 节点地址。

redis_password:设置为 Redis 集群的密码(如有)。

redis_cluster_name:设置为 Redis 集群名称。

redis_cluster_ssl:启用与 Redis 集群的 SSL/TLS 通信。

向路由发送五个并发请求:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get"'

你应该会看到类似以下内容的响应,其中超过阈值的请求被拒绝:

Response: 200
Response: 200
Response: 429
Response: 429
Response: 429

这表明在不同 APISIX 实例中配置的两个路由共享相同的配额。

按规则进行速率限制

以下示例演示了如何配置 limit-conn,根据请求属性应用不同的速率限制规则。此功能从 APISIX 3.16.0 起可用。在此示例中,根据代表调用者访问层级的 HTTP 标头值应用速率限制。

请注意,所有规则按顺序应用。如果配置的键不存在,则对应的规则将被跳过。

除了 HTTP 标头外,你还可以基于其他内置变量或 NGINX 变量来实现更灵活和细粒度的速率限制策略。

创建一个带有 limit-conn 插件的路由,根据请求标头应用不同的速率限制,允许按订阅(X-Subscription-ID)进行速率限制,并对试用用户(X-Trial-ID)实施更严格的限制:

Admin API

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "limit-conn-rules-route",
    "uri": "/get",
    "plugins": {
      "limit-conn": {
        "rejected_code": 429,
        "default_conn_delay": 0.1,
        "policy": "local",
        "rules": [
          {
            "key": "${http_x_subscription_id}",
            "conn": "${http_x_custom_conn ?? 5}",
            "burst": 1
          },
          {
            "key": "${http_x_trial_id}",
            "conn": 1,
            "burst": 1
          }
        ]
      }
    },
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "httpbin.org:80": 1
      }
    }
  }'

ADC

adc.yaml
services:
  - name: httpbin
    routes:
      - uris:
          - /get
        name: limit-conn-rules-route
        plugins:
          limit-conn:
            rejected_code: 429
            default_conn_delay: 0.1
            policy: local
            rules:
              - key: "${http_x_subscription_id}"
                conn: "${http_x_custom_conn ?? 5}"
                burst: 1
              - key: "${http_x_trial_id}"
                conn: 1
                burst: 1
    upstream:
      type: roundrobin
      nodes:
        - host: httpbin.org
          port: 80
          weight: 1

将配置同步到网关:

adc sync -f adc.yaml

Ingress Controller

Gateway API

limit-conn-ic.yaml
apiVersion: v1
kind: Service
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  type: ExternalName
  externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: limit-conn-plugin-config
spec:
  plugins:
    - name: limit-conn
      config:
        rejected_code: 429
        default_conn_delay: 0.1
        policy: local
        rules:
          - key: "${http_x_subscription_id}"
            conn: "${http_x_custom_conn ?? 5}"
            burst: 1
          - key: "${http_x_trial_id}"
            conn: 1
            burst: 1
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: limit-conn-rules-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /get
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: limit-conn-plugin-config
      backendRefs:
        - name: httpbin-external-domain
          port: 80

APISIX Ingress Controller

limit-conn-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  ingressClassName: apisix
  externalNodes:
  - type: Domain
    name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: limit-conn-rules-route
spec:
  ingressClassName: apisix
  http:
    - name: limit-conn-rules-route
      match:
        paths:
          - /get
        methods:
          - GET
      upstreams:
        - name: httpbin-external-domain
      plugins:
        - name: limit-conn
          config:
            rejected_code: 429
            default_conn_delay: 0.1
            policy: local
            rules:
              - key: "${http_x_subscription_id}"
                conn: "${http_x_custom_conn ?? 5}"
                burst: 1
              - key: "${http_x_trial_id}"
                conn: 1
                burst: 1

应用配置:

kubectl apply -f limit-conn-ic.yaml

❶ 使用 X-Subscription-ID 请求标头的值作为速率限制键。

❷ 根据 X-Custom-Conn 标头动态设置请求连接数。如果未提供该标头,则应用默认的并发连接数 5。

❸ 使用 X-Trial-ID 请求标头的值作为速率限制键。

要验证速率限制,向路由发送 7 个并发请求,使用相同的订阅 ID:

seq 1 7 | xargs -n1 -P7 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get" -H "X-Subscription-ID: sub-123456789"'

你应该会看到以下响应,表明在未提供 X-Custom-Conn 标头时,应用了默认的并发连接限制 5 和突发值 1:

Response: 429
Response: 200
Response: 200
Response: 200
Response: 200
Response: 200
Response: 200

向路由发送 5 个并发请求,使用相同的订阅 ID 并将 X-Custom-Conn 标头设置为 1:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get" -H "X-Subscription-ID: sub-123456789" -H "X-Custom-Conn: 1"'

你应该会看到以下响应,表明应用了并发连接限制 1 和突发值 1:

Response: 429
Response: 429
Response: 429
Response: 200
Response: 200

最后,向路由发送 5 个请求,附带试用 ID 标头:

seq 1 5 | xargs -n1 -P5 bash -c 'curl -s -o /dev/null -w "Response: %{http_code}\n" "http://127.0.0.1:9080/get" -H "X-Trial-ID: trial-123456789"'

你应该会看到以下响应,表明应用了并发连接限制 1 和突发值 1:

Response: 429
Response: 429
Response: 429
Response: 200
Response: 200