定义服务
curl -i -X POST http://localhost:8001/services --data name=example_service --data url='http://mockbin.org'HTTP/1.1 201 CreatedDate: Fri, 31 Jul 2020 08:05:54 GMTContent-Type: application/json; charset=utf-8Connection: keep-aliveAccess-Control-Allow-Origin: *Server: kong/2.1.0Content-Length: 361X-Kong-Admin-Latency: 10{"host":"mockbin.org","id":"d7b800e6-d425-4108-9ab4-d6d39a3b33ab","protocol":"http","read_timeout":60000,"tls_verify_depth":null,"port":80,"updated_at":1596182754,"ca_certificates":null,"created_at":1596182754,"connect_timeout":60000,"write_timeout":60000,"name":"example_service","retries":5,"path":null,"tls_verify":null,"tags":null,"client_certificate":null}%
httpie
$http POST :8001/services name=example_service url='http://mockbin.org'HTTP/1.1 201 CreatedAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 361Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 02:45:46 GMTServer: kong/2.1.0X-Kong-Admin-Latency: 6{"ca_certificates": null,"client_certificate": null,"connect_timeout": 60000,"created_at": 1604889946,"host": "mockbin.org","id": "53f7da78-d608-4f73-9062-fd8ca11c9627","name": "example_service","path": null,"port": 80,"protocol": "http","read_timeout": 60000,"retries": 5,"tags": null,"tls_verify": null,"tls_verify_depth": null,"updated_at": 1604889946,"write_timeout": 60000}
确定服务节点
curl -i http://localhost:8001/services/example_serviceHTTP/1.1 200 OKDate: Fri, 31 Jul 2020 08:07:31 GMTContent-Type: application/json; charset=utf-8Connection: keep-aliveAccess-Control-Allow-Origin: *Server: kong/2.1.0Content-Length: 361X-Kong-Admin-Latency: 2{"host":"mockbin.org","id":"d7b800e6-d425-4108-9ab4-d6d39a3b33ab","protocol":"http","read_timeout":60000,"tls_verify_depth":null,"port":80,"updated_at":1596182754,"ca_certificates":null,"created_at":1596182754,"connect_timeout":60000,"write_timeout":60000,"name":"example_service","retries":5,"path":null,"tls_verify":null,"tags":null,"client_certificate":null}
httpie
http :8001/services/example_serviceHTTP/1.1 200 OKAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 361Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 02:47:40 GMTServer: kong/2.1.0X-Kong-Admin-Latency: 1{"ca_certificates": null,"client_certificate": null,"connect_timeout": 60000,"created_at": 1604889946,"host": "mockbin.org","id": "53f7da78-d608-4f73-9062-fd8ca11c9627","name": "example_service","path": null,"port": 80,"protocol": "http","read_timeout": 60000,"retries": 5,"tags": null,"tls_verify": null,"tls_verify_depth": null,"updated_at": 1604889946,"write_timeout": 60000}
konga
路由/Route
curl -i -X POST http://localhost:8001/services/example_service/routes --data 'paths[]=/mock' --data 'name=mocking'HTTP/1.1 201 CreatedDate: Fri, 31 Jul 2020 08:10:19 GMTContent-Type: application/json; charset=utf-8Connection: keep-aliveAccess-Control-Allow-Origin: *Server: kong/2.1.0Content-Length: 429X-Kong-Admin-Latency: 10{"id":"1ceac973-bc70-4ec2-bddb-015001589599","path_handling":"v0","paths":["\/mock"],"destinations":null,"headers":null,"protocols":["http","https"],"created_at":1596183019,"snis":null,"service":{"id":"d7b800e6-d425-4108-9ab4-d6d39a3b33ab"},"name":"mocking","strip_path":true,"preserve_host":false,"regex_priority":0,"updated_at":1596183019,"sources":null,"methods":null,"https_redirect_status_code":426,"hosts":null,"tags":null}
httpie
http :8001/services/example_service/routes paths:='["/mock"]' name=mockingHTTP/1.1 201 CreatedAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 429Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 02:53:14 GMTServer: kong/2.1.0X-Kong-Admin-Latency: 7{"created_at": 1604890394,"destinations": null,"headers": null,"hosts": null,"https_redirect_status_code": 426,"id": "43151ca7-10a9-4f42-805e-9af215eaefc1","methods": null,"name": "mocking","path_handling": "v0","paths": ["/mock"],"preserve_host": false,"protocols": ["http","https"],"regex_priority": 0,"service": {"id": "53f7da78-d608-4f73-9062-fd8ca11c9627"},"snis": null,"sources": null,"strip_path": true,"tags": null,"updated_at": 1604890394}

验证服务
curl -i -X GET http://localhost:8000/mock
- Added a Service named
example_servicewith a URL of[http://mockbin.org](http://mockbin.org). - Added a Route named
/mock. - This means if an HTTP request is sent to the Kong Gateway node on port
8000(the proxy port) and it matches route/mock, then that request is sent to[http://mockbin.org](http://mockbin.org). - Abstracted a backend/upstream service and put a route of your choice on the front end, which you can now give to clients to make requests.
负载均衡

创建上游服务
curl -X POST http://localhost:8001/upstreams --data name=example_upstream{"client_certificate":null,"created_at":1596185397,"id":"028d5d41-2250-4884-a480-57a407afbbcb","tags":null,"name":"example_upstream","algorithm":"round-robin","hash_on_header":null,"hash_fallback_header":null,"host_header":null,"hash_on_cookie":null,"healthchecks":{"threshold":0,"active":{"unhealthy":{"http_statuses":[429,404,500,501,502,503,504,505],"tcp_failures":0,"timeouts":0,"http_failures":0,"interval":0},"type":"http","http_path":"\/","timeout":1,"healthy":{"successes":0,"interval":0,"http_statuses":[200,302]},"https_sni":null,"https_verify_certificate":true,"concurrency":10},"passive":{"unhealthy":{"http_failures":0,"http_statuses":[429,500,503],"tcp_failures":0,"timeouts":0},"healthy":{"http_statuses":[200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,306,307,308],"successes":0},"type":"http"}},"hash_on_cookie_path":"\/","hash_on":"none","hash_fallback":"none","slots":10000}
关联服务
curl -X PATCH http://localhost:8001/services/example_service --data host='example_upstream'{"host":"example_upstream","id":"d7b800e6-d425-4108-9ab4-d6d39a3b33ab","protocol":"http","read_timeout":60000,"tls_verify_depth":null,"port":80,"updated_at":1596186113,"ca_certificates":null,"created_at":1596182754,"connect_timeout":60000,"write_timeout":60000,"name":"example_service","retries":5,"path":null,"tls_verify":null,"tags":null,"client_certificate":null}
创建tagart mockbin.org
curl -X POST http://localhost:8001/upstreams/example_upstream/targets --data target='mockbin.org:80'{"created_at":1596186292.506,"id":"c6bbaf88-8a0c-4f19-aee3-4add52616600","tags":null,"weight":100,"target":"mockbin.org:80","upstream":{"id":"028d5d41-2250-4884-a480-57a407afbbcb"}}
创建tagart httpbin.org:80
curl -X POST http://localhost:8001/upstreams/example_upstream/targets --data target='httpbin.org:80'{"created_at":1596186322.459,"id":"915b93f4-9fea-4e21-8509-35120651e9bd","tags":null,"weight":100,"target":"httpbin.org:80","upstream":{"id":"028d5d41-2250-4884-a480-57a407afbbcb"}}
在浏览器请求下面的地址http://localhost:8000/mock
会在mockbin.org 和httpbin.org 切换
插件
全局
httpie
http -f post :8001/plugins name=rate-limiting config.minute=5 config.policy=localHTTP/1.1 201 CreatedAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 497Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 02:59:28 GMTServer: kong/2.1.0X-Kong-Admin-Latency: 6{"config": {"day": null,"fault_tolerant": true,"header_name": null,"hide_client_headers": false,"hour": null,"limit_by": "consumer","minute": 5,"month": null,"policy": "local","redis_database": 0,"redis_host": null,"redis_password": null,"redis_port": 6379,"redis_timeout": 2000,"second": null,"year": null},"consumer": null,"created_at": 1604890768,"enabled": true,"id": "9b64fce7-7364-4202-bc4c-8a93c298ed9c","name": "rate-limiting","protocols": ["grpc","grpcs","http","https"],"route": null,"service": null,"tags": null}
设置信息
当请求第6次的时候
http :8000/mock/requestHTTP/1.1 429 Too Many RequestsConnection: keep-aliveContent-Length: 41Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 03:01:17 GMTRateLimit-Limit: 5RateLimit-Remaining: 0RateLimit-Reset: 43Retry-After: 43Server: kong/2.1.0X-Kong-Response-Latency: 1X-RateLimit-Limit-Minute: 5X-RateLimit-Remaining-Minute: 0{"message": "API rate limit exceeded"}
路由
http :8001/routes/mocking/plugins name=key-authHTTP/1.1 201 CreatedAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 363Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 03:13:53 GMTServer: kong/2.1.0X-Kong-Admin-Latency: 7{"config": {"anonymous": null,"hide_credentials": false,"key_in_body": false,"key_names": ["apikey"],"run_on_preflight": true},"consumer": null,"created_at": 1604891633,"enabled": true,"id": "c7e0cc7e-f48a-4a3d-93d8-df78edd91c42","name": "key-auth","protocols": ["grpc","grpcs","http","https"],"route": {"id": "43151ca7-10a9-4f42-805e-9af215eaefc1"},"service": null,"tags": null}
测试
http :8000/mockHTTP/1.1 401 UnauthorizedConnection: keep-aliveContent-Length: 45Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 03:15:25 GMTServer: kong/2.1.0WWW-Authenticate: Key realm="kong"X-Kong-Response-Latency: 30{"message": "No API key found in request"}
服务
增加插件
http :8001/services/example_service/plugins name=key-authHTTP/1.1 201 CreatedAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 363Content-Type: application/json; charset=utf-8Date: Mon, 09 Nov 2020 03:17:03 GMTServer: kong/2.1.0X-Kong-Admin-Latency: 7{"config": {"anonymous": null,"hide_credentials": false,"key_in_body": false,"key_names": ["apikey"],"run_on_preflight": true},"consumer": null,"created_at": 1604891823,"enabled": true,"id": "4f6ee17a-b308-4761-b512-f52eed31e3e2","name": "key-auth","protocols": ["grpc","grpcs","http","https"],"route": null,"service": {"id": "53f7da78-d608-4f73-9062-fd8ca11c9627"},"tags": null}
关闭插件
查看插件信息
http :8001/routes/mocking/plugins
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 386
Content-Type: application/json; charset=utf-8
Date: Mon, 09 Nov 2020 03:24:47 GMT
Server: kong/2.1.0
X-Kong-Admin-Latency: 2
{
"data": [
{
"config": {
"anonymous": null,
"hide_credentials": false,
"key_in_body": false,
"key_names": [
"apikey"
],
"run_on_preflight": true
},
"consumer": null,
"created_at": 1604891633,
"enabled": true,
"id": "c7e0cc7e-f48a-4a3d-93d8-df78edd91c42",
"name": "key-auth",
"protocols": [
"grpc",
"grpcs",
"http",
"https"
],
"route": {
"id": "43151ca7-10a9-4f42-805e-9af215eaefc1"
},
"service": null,
"tags": null
}
],
"next": null
}
curl -X PATCH http://127.0.0.1:8001/routes/mocking/plugins/c7e0cc7e-f48a-4a3d-93d8-df78edd91c42 \
--data enabled=false
{"created_at":1604891633,"id":"c7e0cc7e-f48a-4a3d-93d8-df78edd91c42","tags":null,"enabled":false,"protocols":["grpc","grpcs","http","https"],"name":"key-auth","consumer":null,"service":null,"route":{"id":"43151ca7-10a9-4f42-805e-9af215eaefc1"},"config":{"key_names":["apikey"],"run_on_preflight":true,"anonymous":null,"hide_credentials":false,"key_in_body":false}}%
httpie
http PATCH :8001/routes/mocking/plugins/c7e0cc7e-f48a-4a3d-93d8-df78edd91c42 enabled:=false
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 364
Content-Type: application/json; charset=utf-8
Date: Mon, 09 Nov 2020 03:42:19 GMT
Server: kong/2.1.0
X-Kong-Admin-Latency: 8
{
"config": {
"anonymous": null,
"hide_credentials": false,
"key_in_body": false,
"key_names": [
"apikey"
],
"run_on_preflight": true
},
"consumer": null,
"created_at": 1604891633,
"enabled": false,
"id": "c7e0cc7e-f48a-4a3d-93d8-df78edd91c42",
"name": "key-auth",
"protocols": [
"grpc",
"grpcs",
"http",
"https"
],
"route": {
"id": "43151ca7-10a9-4f42-805e-9af215eaefc1"
},
"service": null,
"tags": null
}
缓存
http -f :8001/plugins name=proxy-cache config.strategy=memory config.content_type="application/json; charset=utf-8"
HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 497
Content-Type: application/json; charset=utf-8
Date: Mon, 09 Nov 2020 03:05:36 GMT
Server: kong/2.1.0
X-Kong-Admin-Latency: 9
{
"config": {
"cache_control": false,
"cache_ttl": 300,
"content_type": [
"application/json; charset=utf-8"
],
"memory": {
"dictionary_name": "kong_db_cache"
},
"request_method": [
"GET",
"HEAD"
],
"response_code": [
200,
301,
404
],
"storage_ttl": null,
"strategy": "memory",
"vary_headers": null,
"vary_query_params": null
},
"consumer": null,
"created_at": 1604891136,
"enabled": true,
"id": "3732e473-31af-450b-89c4-1669f375a037",
"name": "proxy-cache",
"protocols": [
"grpc",
"grpcs",
"http",
"https"
],
"route": null,
"service": null,
"tags": null
}
用户
http :8001/consumers username=consumer custom_id=consumer
HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 126
Content-Type: application/json; charset=utf-8
Date: Mon, 09 Nov 2020 03:20:18 GMT
Server: kong/2.1.0
X-Kong-Admin-Latency: 4
{
"created_at": 1604892018,
"custom_id": "consumer",
"id": "b71390e0-daa2-485d-b36b-23a45295c3b4",
"tags": null,
"username": "consumer"
}
创建keyauth
http :8001/consumers/consumer/key-auth key=apikey
HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 164
Content-Type: application/json; charset=utf-8
Date: Mon, 09 Nov 2020 03:22:25 GMT
Server: kong/2.1.0
X-Kong-Admin-Latency: 6
{
"consumer": {
"id": "b71390e0-daa2-485d-b36b-23a45295c3b4"
},
"created_at": 1604892145,
"id": "80732bbd-efb2-4645-9cb0-0512f71406f0",
"key": "apikey",
"tags": null,
"ttl": null
}


