Kong Gateway - 19 基于网关服务的请求终止(Request Termination)

这个插件通过指定带状态码的消息来终止传入的请求。
这样做可以允许(暂时)停止服务或路由的通讯,
甚至阻止某个/某些消费者继续访问暴露的网关服务。

URL Format  http://localhost:8001/services/{service}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/plugins \
--data "name=request-termination"  \
--data "config.status_code=403" \

--data "config.message=So long and thanks for all the fish."

HTTP/1.1 201 Created
Date: Sun, 20 May 2018 11:01:11 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1526814071000, 
    "config": {
        "status_code": 403, 
        "message": "So long and thanks for all the fish."
    }, 
    "id": "cb855af3-b193-4a11-9bc4-a4db85cc2161", 
    "enabled": true, 
    "service_id": "e281052c-6672-4eb4-858b-db736f86e1f3", 
    "name": "request-termination"
}
[root@contoso ~]# curl -i -X GET \
--url http://contoso.org:8000/v1/countries \
--header "Authorization: Basic amFja0Bob3RtYWlsLmNvbToxMjM0NTY="
HTTP/1.1 403 Forbidden
Date: Sun, 20 May 2018 11:22:45 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
Vary: Origin
Access-Control-Allow-Origin: http://contoso.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Auth-Token
X-RateLimit-Limit-countries-minute: 10
X-RateLimit-Remaining-countries-minute: 10

{"message":"So long and thanks for all the fish."}
URL Format  http://localhost:8001/routes/{route_id}/plugins
[root@contoso ~]# curl -X POST \
--url http://localhost:8001/routes/29eff1c5-376c-4519-b3a6-33f26954aeb7/plugins \
--data "name=request-termination"  \
--data "config.status_code=403" \
--data "config.message=So long and thanks for all the fish."
{
    "created_at": 1526815548000, 
    "config": {
        "status_code": 403, 
        "message": "So long and thanks for all the fish."
    }, 
    "id": "30900fb1-eefb-4d69-9572-f8fe971bc19d", 
    "enabled": true, 
    "route_id": "29eff1c5-376c-4519-b3a6-33f26954aeb7", 
    "name": "request-termination"
}
URL Format  http://localhost:8001/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/plugins \
--data "name=request-termination" \
--data "consumer_id=2a15a391-a19e-47b2-9e49-34e81c3bc210"  \
--data "config.status_code=403" \
--data "config.message=So long and thanks for all the fish."
HTTP/1.1 201 Created
Date: Sun, 20 May 2018 11:29:58 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1526815798000, 
    "config": {
        "status_code": 403, 
        "message": "So long and thanks for all the fish."
    }, 
    "id": "fcc9916c-0ad6-4db1-81fb-5256e5574984", 
    "name": "request-termination", 
    "enabled": true, 
    "consumer_id": "2a15a391-a19e-47b2-9e49-34e81c3bc210"
}
[root@contoso ~]# curl -i -X GET \
--url http://localhost:8000/v1/countries \
--header "Authorization: Basic amFja0Bob3RtYWlsLmNvbToxMjM0NTY="
HTTP/1.1 403 Forbidden
Date: Sun, 20 May 2018 11:31:47 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
Vary: Origin
Access-Control-Allow-Origin: http://contoso.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Auth-Token
X-RateLimit-Limit-countries-minute: 10
X-RateLimit-Remaining-countries-minute: 10

{"message":"So long and thanks for all the fish."}

如何逆向操作恢复被终止的服务请求?

[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/fcc9916c-0ad6-4db1-81fb-5256e5574984

HTTP/1.1 204 No Content
Date: Sun, 20 May 2018 11:55:17 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/30900fb1-eefb-4d69-9572-f8fe971bc19d
HTTP/1.1 204 No Content
Date: Sun, 20 May 2018 11:55:44 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/cb855af3-b193-4a11-9bc4-a4db85cc2161
HTTP/1.1 204 No Content
Date: Sun, 20 May 2018 11:56:10 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
[root@contoso ~]# curl -i -X GET \
--url http://localhost:8000/v1/countries/2 \
--header "Authorization: Basic amFja0Bob3RtYWlsLmNvbToxMjM0NTY="
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 119
Connection: keep-alive
Date: Sun, 20 May 2018 11:56:26 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
Vary: Origin
Access-Control-Allow-Origin: http://contoso.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Auth-Token
X-RateLimit-Limit-countries-minute: 10
X-RateLimit-Remaining-countries-minute: 7
X-Kong-Upstream-Latency: 63
X-Kong-Proxy-Latency: 3
Via: kong/0.13.1

[
    {
        "id": 2, 
        "sip": "1.0.1.0", 
        "eip": "1.0.3.255", 
        "sip2long": 16777472, 
        "eip2long": 16778239, 
        "code": "CN", 
        "country_name": "China"
    }
]
相关文章
相关标签/搜索