janiussyafiq commented on issue #13263: URL: https://github.com/apache/apisix/issues/13263#issuecomment-4290258731
Hi @liuxiaobo007 , this can be achieved by setting the `limit-req` plugin within each consumer. Here is an example for your use-case: 1. Create two consumers with `key-auth` and `limit-req` with different config. ```bash curl -s http://127.0.0.1:9180/apisix/admin/consumers \ -H "X-API-KEY: $admin_key" \ -X PUT -d '{ "username": "consumer-1", "plugins": { "key-auth": { "key": "key-1" }, "limit-req": { "rate": 1, "burst": 0, "key": "consumer_name", "key_type": "var", "rejected_code": 429 } } }' curl -s http://127.0.0.1:9180/apisix/admin/consumers \ -H "X-API-KEY: $admin_key" \ -X PUT -d '{ "username": "consumer-2", "plugins": { "key-auth": { "key": "key-2" }, "limit-req": { "rate": 2, "burst": 0, "key": "consumer_name", "key_type": "var", "rejected_code": 429 } } }' ``` 2. Create a route ```bash curl -s http://127.0.0.1:9180/apisix/admin/routes/1 \ -H "X-API-KEY: $admin_key" \ -X PUT -d '{ "uri": "/anything", "plugins": { "key-auth": {} }, "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:8080": 1 } } }' ``` 3. Run an upstream container within local machine for low latency request (optional, can use other as well) ```bash docker run --network host -P ghcr.io/mccutchen/go-httpbin ``` 4. Hit the route as `consumer-1`. ```bash for i in $(seq 1 3); do echo -n "req $i: " curl -s -o /dev/null -w "%{http_code}" \ http://127.0.0.1:9080/anything \ -H 'apikey: key-1' echo done # result req 1: 200 req 2: 429 req 3: 429 ``` 5. Hit the route as `consumer-2`. ```bash for i in 1 2 3; do echo -n "Req $i at $(date +%S.%3N)s -> Status: " curl -s -o /dev/null -w "%{http_code}\n" \ http://127.0.0.1:9080/anything \ -H 'apikey: key-2' # After the first request, wait to clear the bucket # After the second, fire immediately to trigger the limit if [ $i -eq 1 ]; then sleep 0.51 fi done # result Req 1 at 37.567s -> Status: 200 Req 2 at 38.097s -> Status: 200 Req 3 at 38.109s -> Status: 429 ``` From here we can see each consumer has different limit-req quota. Let me know if u have any further clarifying question :) Reference: https://apisix.apache.org/docs/apisix/terminology/consumer/#example -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
