KONGAPI服务和路由接口配置

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}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容

  • Kong 是 Mashape 开源的高性能高可用 API 网关和 API 管理服务层。它基于 OpenResty ...
    meng_philip123阅读 9,051评论 0 6
  • KONG有很多插件,权限认证的,安全保护的,流量控制的,还有一些功能型的。权限配置主要讲的就是Authentica...
    集韵增广阅读 1,602评论 0 0
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 124,880评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,046评论 0 4