Kong plugin
Kong's plugin supports four dimensions, the execution order is from top to bottom, and it should be noted that if the same plugin is configured in different dimensions, it will only be executed once:
- Apply the API plus consumer mix;
- Applied to consumers;
- Applied in the API;
- Applied globally;
Also, not all plugins support defining consumers
Add another API for testing Add a new interface:
{ "created_at": 1515055457572, "strip_uri": true, "id": "a99919d1-0770-40a1-81a9-e3c31fd9d282", "name": "spring-boot-consul-service2", "methods": [ "GET" ], "http_if_terminated": false, "preserve_host": false, "upstream_url": "http://spring-boot-consul-service2.service.dc1.consul:17004", "uris": [ "/user" ], "upstream_send_timeout": 60000, "upstream_connect_timeout": 60000, "upstream_read_timeout": 60000, "retries": 5, "https_only": false
}
Add steams
{ "created_at": 1515055993113, "orderlist": [ 41, 28, 44, 14, 79, 38, 30, 69, 15, 88, 72, 53, 89, 47, 81, 43, 60, 91, 92, 100, 98, 55, 12, 40, 9, 31, 84, 24, 36, 39, 96, 74, 6, 8, 93, 99, 78, 34, 49, 37, 61, 76, 33, 35, 32, 77, 70, 26, 52, 87, 73, 23, 57, 25, 17, 90, 62, 82, 19, 75, 50, 4, 51, 42, 65, 66, 63, 3, 1, 68, 94, 67, 20, 80, 54, 7, 5, 16, 85, 13, 46, 10, 97, 22, 48, 11, 27, 86, 18, 59, 83, 21, 56, 95, 2, 64, 58, 71, 45, 29 ], "id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4", "name": "spring-boot-consul-service2.service.dc1.consul", "slots": 100
}
Add targets
tagets information
{ "total": 3, "data": [ { "created_at": 1515056103599, "id": "994e7886-3e48-45cb-80ad-3f7918ede007", "upstream_id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4", "target": "192.168.0.186:17004", "weight": 15 }, { "created_at": 1515056109570, "id": "03b477ee-a69d-49ee-9892-276f451c373c", "upstream_id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4", "target": "192.168.0.185:17004", "weight": 15 }, { "created_at": 1515056112730, "id": "8ace8d83-4504-4334-ad04-f0b536963df9", "upstream_id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4", "target": "192.168.0.184:17004", "weight": 15 } ]
}
The current list of APIs is as follows:
{ "total": 2, "data": [ { "created_at": 1515055457572, "strip_uri": true, "id": "a99919d1-0770-40a1-81a9-e3c31fd9d282", "name": "spring-boot-consul-service2", "methods": [ "GET" ], "http_if_terminated": false, "https_only": false, "upstream_url": "http://spring-boot-consul-service2.service.dc1.consul:17004", "uris": [ "/user" ], "preserve_host": false, "upstream_connect_timeout": 60000, "upstream_read_timeout": 60000, "upstream_send_timeout": 60000, "retries": 5 }, { "created_at": 1514995615974, "strip_uri": true, "id": "b8431831-6a77-434e-ac4a-7e379550714f", "name": "spring-boot-consul-service", "methods": [ "GET" ], "http_if_terminated": false, "https_only": false, "upstream_url": "http://spring-boot-consul-service.service.dc1.consul:17003", "uris": [ "/service" ], "preserve_host": false, "upstream_connect_timeout": 60000, "upstream_read_timeout": 60000, "upstream_send_timeout": 60000, "retries": 5 } ]
}
The access interfaces return the following:
{ "msg": "ok", "date": { "score": 42, "name": "tree", "id": 1, "age": 12, "nowTime": 1515060166929, "info": "/checkUser, host:192.168.0.186, service_id:application-1700417004" }, "code": 200
} { "msg": "ok", "date": { "score": 505, "name": "tree", "id": 1, "age": 12, "nowTime": 1515060402392, "info": "/checkUser, host:192.168.0.184, service_id:application-1700317003" }, "code": 200
} Add the Key-Auth plugin
Open the key-auth plugin
{ "created_at": 1515066239000, "config": { "key_in_body": false, "run_on_preflight": true, "anonymous": "", "hide_credentials": false, "key_names": [ "apikey" ] }, "id": "78c519d3-11f7-4cdf-b038-9f17f1a83427", "enabled": true, "api_id": "a99919d1-0770-40a1-81a9-e3c31fd9d282", "name": "key-auth"
} Then access the interface with an error like this:
Remove the key-auth plugin
Check out the API binding plugin
Add the rate limiting plugin rate-limiting
output
{ "created_at": 1515068825000, "config": { "minute": 10, "policy": "cluster", "redis_timeout": 2000, "hide_client_headers": false, "second": 2, "limit_by": "consumer", "redis_port": 6379, "redis_database": 0, "fault_tolerant": true }, "id": "482c627f-4041-4320-9b2f-0cc7722b92ea", "name": "rate-limiting", "api_id": "a99919d1-0770-40a1-81a9-e3c31fd9d282", "enabled": true
} When the access interface exceeds the limit, the following returns will be received:
Return
{ "message": "API rate limit exceeded"
} However, there are still some problems, because the message format is fixed, the output format is not consistent with the backend service, and custom message formats need to be supported.
Kong modifies the exception return value For example, the service unified message format is as follows:
{ "code": 200, "msg": "ok", "data": {xxx}
} We also want the exception return of kong to remain in the same format as above. Then we can directly modify the source code of Kong, which is actually very simple.
Start by downloading the source code
#下载kong插件源码
git clonegit@github.com:Kong/kong.git
Locate the output code to modify
The output code is located in kong/kong/tools/responses.lua, open it for editing.
Save, then compile
[root@tree81 kong]# luarocks make kong 0.11.2-0 is now installed in /usr/local (license: MIT) Launch, try again
Quickly refresh the address several times in a row:The hyperlink login is visible.Get an error message:
{ "code": 429, "msg": "API rate limit exceeded"
} Done, perfect :) |