KONGAPI服务和路由接口配置

2021-07-21  本文已影响0人  集韵增广
Kong管理接口

参考:https://docs.konghq.com/gateway-oss/2.5.x/admin-api/#service-object

Admin API

Kong Gateway comes with an internal RESTful Admin API for administration purposes. Requests to the Admin API can be sent to any node in the cluster, and Kong will keep the configuration consistent across all nodes.

8001:is the default port on which the Admin API listens.

8444:is the default port for HTTPS traffic to the Admin API.

This API is designed for internal use and provides full control over Kong, so care should be taken when setting up Kong environments to avoid undue public exposure of this API. See this document for a discussion of methods to secure the Admin API.

Service Object

Service entities, as the name implies, are abstractions of each of your own upstream services. Examples of Services would be a data transformation microservice, a billing API, etc.

The main attribute of a Service is its URL (where Kong should proxy traffic to), which can be set as a single string or by specifying its protocol,host,port and path individually.

Services are associated to Routes (a Service can have many Routes associated with it). Routes are entry-points in Kong and define rules to match client requests. Once a Route is matched, Kong proxies the request to its associated Service. See the Proxy Reference for a detailed explanation of how Kong proxies traffic.

添加服务是指,如果我们增加了一个微服务程序并增加一些服务,那么就需要增加一个服务,增加服务比较简单,主要增加的字段为:

host:服务的地址,最好是域名,当然单机的话,指向ip也可以;

protocol:也就是服务支持的协议,远程最好是https,本机http就行;

port:是protocol对应的端口,本地的话各种端口都好;

path:是访问路由,一般是/, 如果有前缀什么的也一样;

查看service:

curl -i 10.10.30.70:8001/services

{"next":null,"data":[{"host":"jsonplaceholder.typicode.com","id":"0ee63b33-870a-41a3-92f0-8b51f33c6650","protocol":"https","read_timeout":60000,"tls_verify_depth":null,"port":443,"updated_at":1625033953,"ca_certificates":null,"created_at":1625032986,"connect_timeout":60000,"write_timeout":60000,"name":"testapi","retries":5,"path":"/","tls_verify":null,"tags":[],"client_certificate":null},{"host":"10.10.30.60","id":"6605a206-4160-44ec-8716-fd552b7f9002","protocol":"http","read_timeout":60000,"tls_verify_depth":null,"port":8283,"updated_at":1625034657,"ca_certificates":null,"created_at":1625034483,"connect_timeout":60000,"write_timeout":60000,"name":"60","retries":5,"path":"/","tls_verify":null,"tags":[],"client_certificate":null}]}

添加service:

curl -x POST 10.10.30.70:8001/services --data name=testadminapi --data host=jsonplaceholder.typicode.com --data protocol=https --data port=443 --data path=/

{"host":"jsonplaceholder.typicode.com","id":"f62130a4-bd3d-460e-9d09-7f378db7e4b6", "protocol":"https", "read_timeout":60000, "tls_verify_depth":null,"port":443,"updated_at":1626848074,"ca_certificates":null,"created_at":1626848074,"connect_timeout":60000,"write_timeout":60000,"name":"testadminapi","retries":5,"path":"/","tls_verify":null,"tags":null,"client_certificate":null}

Route Object

Route entities define rules to match client requests. Each Route is associated with a Service, and a Service may have multiple Routes associated to it. Every request matching a given Route will be proxied to its associated Service.The combination of Routes and Services (and the separation of concerns between them) offers a powerful routing mechanism with which it is possible to define fine-grained entry-points in Kong leading to different upstream services of your infrastructure.

路由是添加服务后kong的路由规则,比如访问kong地址:8000的时候,路径是什么,对应的路由规则是什么,举例:当访问http:kongaddr:8000/users,然后因为有路由配置的protocol是http,method是get,path是/users,所以就可以运用此规则走到相对应的服务,然后访问到服务的地址和路径去;添加路由时必填字段是:

name:路由名字

protocols:支持的协议

methods:访问的方法

paths:路由匹配的路径

strip_path:是否过滤路径前缀,true就是把路径当前缀过滤掉,false就是不过滤

查看route:

curl -i 10.10.30.70:8001/routes

{"next":null,"data":[{"strip_path":false,"tags":null,"updated_at":1625034123,"destinations":null,"headers":null,"protocols":["http","https"],"methods":["GET","POST","PUT","DELETE"],"service":{"id":"0ee63b33-870a-41a3-92f0-8b51f33c6650"}, "snis":null, "hosts":null, "name":"example", "path_handling":"v1", "paths":["/users","/posts","/comments","/albums","/photos","/todos"], "preserve_host":false, "regex_priority":0, "response_buffering":true, "sources":null, "id":"fd3140c5-6e39-4bed-a258-85b9b3439444","https_redirect_status_code":426,"request_buffering":true,"created_at":1625033101},{"strip_path":false, "tags":null,"updated_at":1625034858,"destinations":null,"headers":null,"protocols":["http","https"],"methods":["GET","POST"],"service":{"id":"6605a206-4160-44ec-8716-fd552b7f9002"}, "snis":null,"hosts":null,"name":"60","path_handling":"v1","paths":["/web","/v1"], "preserve_host":false, "regex_priority":0,"response_buffering":true,"sources":null,"id":"fd829e76-3992-41b9-a59d-8cbb41c47717", "https_redirect_status_code":426, "request_buffering":true,"created_at":1625034504}]}

curl -i 10.10.30.70:8001/services/testapi/routes

{"next":null,"data":[{"strip_path":false,"tags":null,"updated_at":1625034123,"destinations":null,"headers":null,"protocols":["http","https"],"methods":["GET","POST","PUT","DELETE"],"service":{"id":"0ee63b33-870a-41a3-92f0-8b51f33c6650"}, "snis":null,"hosts":null,"name":"example","path_handling":"v1", "paths":["/users","/posts","/comments","/albums","/photos","/todos"],"preserve_host":false,"regex_priority":0,"response_buffering":true, "sources":null,"id":"fd3140c5-6e39-4bed-a258-85b9b3439444","https_redirect_status_code":426,"request_buffering":true,"created_at":1625033101}]}

curl -i 10.10.30.70:8001/services/f62130a4-bd3d-460e-9d09-7f378db7e4b6/routes

{"next":null,"data":[]}

添加route:

curl -H "Content-Type: application/json" -X POST 10.10.30.70:8001/services/f62130a4-bd3d-460e-9d09-7f378db7e4b6/routes --data '{"name":"testadminroute","protocols":["http","https"],"methods":["POST","GET","PUT"],"paths":["/users","/posts","/comments","/albums","/photos","/todos"],"strip_path":false}'

{"strip_path":false,"tags":null,"updated_at":1626852736,"destinations":null,"headers":null,"protocols":["http","https"],"methods":["POST","GET","PUT"],"service":{"id":"f62130a4-bd3d-460e-9d09-7f378db7e4b6"},"snis":null,"hosts":null,"name":"testadminroute","path_handling":"v0", "paths":["/users","/posts","/comments","/albums","/photos","/todos"],"preserve_host":false,"regex_priority":0,"response_buffering":true, "sources":null,"id":"36f30105-7578-47e6-837d-dd5bd80aa4ec","https_redirect_status_code":426,"request_buffering":true,"created_at":1626852736}

更新route:

curl -H "Content-Type: application/json" --request PATCH 10.10.30.70:8001/services/f62130a4-bd3d-460e-9d09-7f378db7e4b6/routes --data '{"name":"testadminroute","protocols":["http","https"],"methods":["POST","GET","PUT","DELETE"],"paths":["/users","/posts","/comments","/albums","/photos","/todos"],"strip_path":false}'

{"strip_path":false,"tags":null,"updated_at":1626853336,"destinations":null,"headers":null,"protocols":["http","https"],"methods":["POST","GET","PUT","DELETE"],"service":{"id":"f62130a4-bd3d-460e-9d09-7f378db7e4b6"},"snis":null,"hosts":null,"name":"testadminroute", "path_handling":"v0","paths":["/users","/posts","/comments","/albums","/photos","/todos"],"preserve_host":false,"regex_priority":0,"response_buffering":true, "sources":null,"id":"36f30105-7578-47e6-837d-dd5bd80aa4ec","https_redirect_status_code":426,"request_buffering":true,"created_at":1626852736}

上一篇下一篇

猜你喜欢

热点阅读