微服务网关Kong – 流量控制

983次阅读

共计 3536 个字符,预计需要花费 9 分钟才能阅读完成。

Kong系列文章

1、微服务网关Kong – 简介

2、微服务网关Kong – 安装

3、微服务网关Konga – 安装

4、微服务网关Kong – 代理

5、微服务网关Kong – 身份验证

6、微服务网关Kong – 安全

7、微服务网关Kong – 流量控制

1 请求大小限制

阻止输入的请求,其主体大于特定大小的兆字节

1.1 启用大小限制

1、在服务上启用插件

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=request-size-limiting"  \
    --data "config.allowed_payload_size=128"

2、在路由上启用插件

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=request-size-limiting"  \
    --data "config.allowed_payload_size=128"

3、在消费者中启用插件

$ curl -X POST http://kong:8001/plugins \
    --data "name=request-size-limiting" \
    --data "consumer_id={consumer_id}"  \
    --data "config.allowed_payload_size=128"
curl -X POST http://172.16.60.8:8001/routes/b0db420a-d3c3-45ee-8b25-11f3fd8ca283/plugins \
    --data "name=request-size-limiting"  \
    --data "config.allowed_payload_size=-20"
    
{
  "created_at": 1609212809,
  "id": "08112606-e023-4d1b-ba76-853832585feb",
  "tags": null,
  "enabled": true,
  "protocols": ["grpc", "grpcs", "http", "https"],
  "name": "request-size-limiting",
  "consumer": null,
  "service": null,
  "route": {
    "id": "b0db420a-d3c3-45ee-8b25-11f3fd8ca283"
  },
  "config": {
    "size_unit": "megabytes",
    "allowed_payload_size": -20
  }
}

1.2 访问测试

微服务网关Kong - 流量控制

2 请求速率限制

速率限制开发人员在给定的时间、分钟、小时、日、月或年的时间内可以发出多少HTTP请求

2.1 启用速率限制

1、在服务上启用插件

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=rate-limiting"  \
    --data "config.second=5" \
    --data "config.hour=10000"

2、在路由上启用插件

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=rate-limiting"  \
    --data "config.second=5" \
    --data "config.hour=10000"

3、在消费者中启用插件

$ curl -X POST http://kong:8001/plugins \
    --data "name=rate-limiting" \
    --data "consumer_id={consumer_id}"  \
    --data "config.second=5" \
    --data "config.hour=10000"
curl -X POST http://172.16.60.8:8001/routes/b0db420a-d3c3-45ee-8b25-11f3fd8ca283/plugins \
    --data "name=rate-limiting"  \
    --data "config.second=2" \
    --data "config.hour=10000"
​
{
  "created_at": 1609213117,
  "id": "0329fd21-3844-4a84-b325-68282577c78a",
  "tags": null,
  "enabled": true,
  "protocols": ["grpc", "grpcs", "http", "https"],
  "name": "rate-limiting",
  "consumer": null,
  "service": null,
  "route": {
    "id": "b0db420a-d3c3-45ee-8b25-11f3fd8ca283"
  },
  "config": {
    "minute": null,
    "redis_host": null,
    "redis_timeout": 2000,
    "limit_by": "consumer",
    "hour": 10000,
    "policy": "cluster",
    "month": null,
    "redis_password": null,
    "second": 2,
    "day": null,
    "hide_client_headers": false,
    "path": null,
    "redis_database": 0,
    "year": null,
    "redis_port": 6379,
    "header_name": null,
    "fault_tolerant": true
  }
}

2.2 访问测试

微服务网关Kong - 流量控制

3 请求终止

这个插件使用指定的状态码和消息终止传入的请求。这允许(暂时)停止服务或路由(或废弃的API实体)上的通信,甚至阻塞用户

3.1 启用请求终止插件

1、在服务上启用插件

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=request-termination"  \
    --data "config.status_code=403" \
    --data "config.message=So long and thanks for all the fish!"

2、在路由上启用插件

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=request-termination"  \
    --data "config.status_code=403" \
    --data "config.message=So long and thanks for all the fish!"
curl -X POST http://172.16.60.8:8001/routes/b0db420a-d3c3-45ee-8b25-11f3fd8ca283/plugins \
    --data "name=request-termination"  \
    --data "config.status_code=403" \
    --data "config.message=The service is under maintenance"
​
{
  "created_at": 1609213373,
  "id": "77c99041-0a99-4388-b4fa-9378791c3839",
  "tags": null,
  "enabled": true,
  "protocols": ["grpc", "grpcs", "http", "https"],
  "name": "request-termination",
  "consumer": null,
  "service": null,
  "route": {
    "id": "b0db420a-d3c3-45ee-8b25-11f3fd8ca283"
  },
  "config": {
    "status_code": 403,
    "content_type": null,
    "body": null,
    "message": "The service is under maintenance"
  }
}

3.2 访问测试

微服务网关Kong - 流量控制

3.3 备注

  • config.status_code :要发送的响应代码。

  • config.message :如果使用默认响应生成器,则发送的消息。

  • config.body : 要发送的raw响应体,跟config.message相互排斥

  • config.content_type : 为config.body配置raw响应的内容类型,默认:application/json; charset=utf-8

正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2020-12-29发表,共计3536字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索