定义服务

  1. curl -i -X POST http://localhost:8001/services --data name=example_service --data url='http://mockbin.org'
  2. HTTP/1.1 201 Created
  3. Date: Fri, 31 Jul 2020 08:05:54 GMT
  4. Content-Type: application/json; charset=utf-8
  5. Connection: keep-alive
  6. Access-Control-Allow-Origin: *
  7. Server: kong/2.1.0
  8. Content-Length: 361
  9. X-Kong-Admin-Latency: 10
  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

  1. $http POST :8001/services name=example_service url='http://mockbin.org'
  2. HTTP/1.1 201 Created
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Length: 361
  6. Content-Type: application/json; charset=utf-8
  7. Date: Mon, 09 Nov 2020 02:45:46 GMT
  8. Server: kong/2.1.0
  9. X-Kong-Admin-Latency: 6
  10. {
  11. "ca_certificates": null,
  12. "client_certificate": null,
  13. "connect_timeout": 60000,
  14. "created_at": 1604889946,
  15. "host": "mockbin.org",
  16. "id": "53f7da78-d608-4f73-9062-fd8ca11c9627",
  17. "name": "example_service",
  18. "path": null,
  19. "port": 80,
  20. "protocol": "http",
  21. "read_timeout": 60000,
  22. "retries": 5,
  23. "tags": null,
  24. "tls_verify": null,
  25. "tls_verify_depth": null,
  26. "updated_at": 1604889946,
  27. "write_timeout": 60000
  28. }

确定服务节点

  1. curl -i http://localhost:8001/services/example_service
  2. HTTP/1.1 200 OK
  3. Date: Fri, 31 Jul 2020 08:07:31 GMT
  4. Content-Type: application/json; charset=utf-8
  5. Connection: keep-alive
  6. Access-Control-Allow-Origin: *
  7. Server: kong/2.1.0
  8. Content-Length: 361
  9. X-Kong-Admin-Latency: 2
  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

  1. http :8001/services/example_service
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Length: 361
  6. Content-Type: application/json; charset=utf-8
  7. Date: Mon, 09 Nov 2020 02:47:40 GMT
  8. Server: kong/2.1.0
  9. X-Kong-Admin-Latency: 1
  10. {
  11. "ca_certificates": null,
  12. "client_certificate": null,
  13. "connect_timeout": 60000,
  14. "created_at": 1604889946,
  15. "host": "mockbin.org",
  16. "id": "53f7da78-d608-4f73-9062-fd8ca11c9627",
  17. "name": "example_service",
  18. "path": null,
  19. "port": 80,
  20. "protocol": "http",
  21. "read_timeout": 60000,
  22. "retries": 5,
  23. "tags": null,
  24. "tls_verify": null,
  25. "tls_verify_depth": null,
  26. "updated_at": 1604889946,
  27. "write_timeout": 60000
  28. }

konga
图片.png

路由/Route

  1. curl -i -X POST http://localhost:8001/services/example_service/routes --data 'paths[]=/mock' --data 'name=mocking'
  2. HTTP/1.1 201 Created
  3. Date: Fri, 31 Jul 2020 08:10:19 GMT
  4. Content-Type: application/json; charset=utf-8
  5. Connection: keep-alive
  6. Access-Control-Allow-Origin: *
  7. Server: kong/2.1.0
  8. Content-Length: 429
  9. X-Kong-Admin-Latency: 10
  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

  1. http :8001/services/example_service/routes paths:='["/mock"]' name=mocking
  2. HTTP/1.1 201 Created
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Length: 429
  6. Content-Type: application/json; charset=utf-8
  7. Date: Mon, 09 Nov 2020 02:53:14 GMT
  8. Server: kong/2.1.0
  9. X-Kong-Admin-Latency: 7
  10. {
  11. "created_at": 1604890394,
  12. "destinations": null,
  13. "headers": null,
  14. "hosts": null,
  15. "https_redirect_status_code": 426,
  16. "id": "43151ca7-10a9-4f42-805e-9af215eaefc1",
  17. "methods": null,
  18. "name": "mocking",
  19. "path_handling": "v0",
  20. "paths": [
  21. "/mock"
  22. ],
  23. "preserve_host": false,
  24. "protocols": [
  25. "http",
  26. "https"
  27. ],
  28. "regex_priority": 0,
  29. "service": {
  30. "id": "53f7da78-d608-4f73-9062-fd8ca11c9627"
  31. },
  32. "snis": null,
  33. "sources": null,
  34. "strip_path": true,
  35. "tags": null,
  36. "updated_at": 1604890394
  37. }

图片.png
验证服务

  1. curl -i -X GET http://localhost:8000/mock
  • Added a Service named example_service with 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.

负载均衡

图片.png
创建上游服务

  1. curl -X POST http://localhost:8001/upstreams --data name=example_upstream
  2. {"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}

关联服务

  1. curl -X PATCH http://localhost:8001/services/example_service --data host='example_upstream'
  2. {"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

  1. curl -X POST http://localhost:8001/upstreams/example_upstream/targets --data target='mockbin.org:80'
  2. {"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

  1. curl -X POST http://localhost:8001/upstreams/example_upstream/targets --data target='httpbin.org:80'
  2. {"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

  1. http -f post :8001/plugins name=rate-limiting config.minute=5 config.policy=local
  2. HTTP/1.1 201 Created
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Length: 497
  6. Content-Type: application/json; charset=utf-8
  7. Date: Mon, 09 Nov 2020 02:59:28 GMT
  8. Server: kong/2.1.0
  9. X-Kong-Admin-Latency: 6
  10. {
  11. "config": {
  12. "day": null,
  13. "fault_tolerant": true,
  14. "header_name": null,
  15. "hide_client_headers": false,
  16. "hour": null,
  17. "limit_by": "consumer",
  18. "minute": 5,
  19. "month": null,
  20. "policy": "local",
  21. "redis_database": 0,
  22. "redis_host": null,
  23. "redis_password": null,
  24. "redis_port": 6379,
  25. "redis_timeout": 2000,
  26. "second": null,
  27. "year": null
  28. },
  29. "consumer": null,
  30. "created_at": 1604890768,
  31. "enabled": true,
  32. "id": "9b64fce7-7364-4202-bc4c-8a93c298ed9c",
  33. "name": "rate-limiting",
  34. "protocols": [
  35. "grpc",
  36. "grpcs",
  37. "http",
  38. "https"
  39. ],
  40. "route": null,
  41. "service": null,
  42. "tags": null
  43. }

图片.png设置信息
图片.png
当请求第6次的时候

  1. http :8000/mock/request
  2. HTTP/1.1 429 Too Many Requests
  3. Connection: keep-alive
  4. Content-Length: 41
  5. Content-Type: application/json; charset=utf-8
  6. Date: Mon, 09 Nov 2020 03:01:17 GMT
  7. RateLimit-Limit: 5
  8. RateLimit-Remaining: 0
  9. RateLimit-Reset: 43
  10. Retry-After: 43
  11. Server: kong/2.1.0
  12. X-Kong-Response-Latency: 1
  13. X-RateLimit-Limit-Minute: 5
  14. X-RateLimit-Remaining-Minute: 0
  15. {
  16. "message": "API rate limit exceeded"
  17. }

路由

  1. http :8001/routes/mocking/plugins name=key-auth
  2. HTTP/1.1 201 Created
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Length: 363
  6. Content-Type: application/json; charset=utf-8
  7. Date: Mon, 09 Nov 2020 03:13:53 GMT
  8. Server: kong/2.1.0
  9. X-Kong-Admin-Latency: 7
  10. {
  11. "config": {
  12. "anonymous": null,
  13. "hide_credentials": false,
  14. "key_in_body": false,
  15. "key_names": [
  16. "apikey"
  17. ],
  18. "run_on_preflight": true
  19. },
  20. "consumer": null,
  21. "created_at": 1604891633,
  22. "enabled": true,
  23. "id": "c7e0cc7e-f48a-4a3d-93d8-df78edd91c42",
  24. "name": "key-auth",
  25. "protocols": [
  26. "grpc",
  27. "grpcs",
  28. "http",
  29. "https"
  30. ],
  31. "route": {
  32. "id": "43151ca7-10a9-4f42-805e-9af215eaefc1"
  33. },
  34. "service": null,
  35. "tags": null
  36. }

测试

  1. http :8000/mock
  2. HTTP/1.1 401 Unauthorized
  3. Connection: keep-alive
  4. Content-Length: 45
  5. Content-Type: application/json; charset=utf-8
  6. Date: Mon, 09 Nov 2020 03:15:25 GMT
  7. Server: kong/2.1.0
  8. WWW-Authenticate: Key realm="kong"
  9. X-Kong-Response-Latency: 30
  10. {
  11. "message": "No API key found in request"
  12. }

服务

增加插件

  1. http :8001/services/example_service/plugins name=key-auth
  2. HTTP/1.1 201 Created
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Length: 363
  6. Content-Type: application/json; charset=utf-8
  7. Date: Mon, 09 Nov 2020 03:17:03 GMT
  8. Server: kong/2.1.0
  9. X-Kong-Admin-Latency: 7
  10. {
  11. "config": {
  12. "anonymous": null,
  13. "hide_credentials": false,
  14. "key_in_body": false,
  15. "key_names": [
  16. "apikey"
  17. ],
  18. "run_on_preflight": true
  19. },
  20. "consumer": null,
  21. "created_at": 1604891823,
  22. "enabled": true,
  23. "id": "4f6ee17a-b308-4761-b512-f52eed31e3e2",
  24. "name": "key-auth",
  25. "protocols": [
  26. "grpc",
  27. "grpcs",
  28. "http",
  29. "https"
  30. ],
  31. "route": null,
  32. "service": {
  33. "id": "53f7da78-d608-4f73-9062-fd8ca11c9627"
  34. },
  35. "tags": null
  36. }

图片.png

关闭插件

查看插件信息

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"
}

图片.png

创建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
}

参考

https://www.jianshu.com/p/f3b1699777d6