Consumer Group

Description

Consumer Groups are used to extract commonly used Plugin configurations and can be bound directly to a Consumer.

With consumer groups, you can define any number of plugins, e.g. rate limiting and apply them to a set of consumers, instead of managing each consumer individually.

Example

The example below illustrates how to create a Consumer Group and bind it to a Consumer:

  1. curl http://127.0.0.1:9180/apisix/admin/consumer_groups/company_a \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "limit-count": {
  6. "count": 200,
  7. "time_window": 60,
  8. "rejected_code": 503,
  9. "group": "$consumer_group_id"
  10. }
  11. }
  12. }'
  1. curl http://127.0.0.1:9180/apisix/admin/consumers \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "username": "jack",
  5. "plugins": {
  6. "key-auth": {
  7. "key": "auth-one"
  8. }
  9. },
  10. "group_id": "company_a"
  11. }'

When APISIX can’t find the Consumer Group with the group_id, the Admin API is terminated with a status code of 400.

Consumer Group - 图1tip
  1. When the same plugin is configured in consumer, routing, plugin config and service, only one configuration is in effect, and the consumer has the highest priority. Please refer to Plugin.
  2. If a Consumer already has the plugins field configured, the plugins in the Consumer Group will effectively be merged into it. The same plugin in the Consumer Group will not override the one configured directly in the Consumer.

For example, if we configure a Consumer Group as shown below:

  1. {
  2. "id": "bar",
  3. "plugins": {
  4. "response-rewrite": {
  5. "body": "hello"
  6. }
  7. }
  8. }

To a Consumer as shown below.

  1. {
  2. "username": "foo",
  3. "group_id": "bar",
  4. "plugins": {
  5. "basic-auth": {
  6. "username": "foo",
  7. "password": "bar"
  8. },
  9. "response-rewrite": {
  10. "body": "world"
  11. }
  12. }
  13. }

Then the body in response-rewrite keeps world.