kong configuration service agent and route simple API


API interface proxy connection kong achieve by konga

Preface :

Before the API Kong already done a study and understand, from the beginning of this article, we will learn how to use KONG implement API interface proxy. To do this, you first need to add services; that is the name used to refer to the upstream API Kong and micro-management of their services on behalf of.

In this paper, we will create a link to Mockbin API service learning test.

1. ADD NEW SERVICE

[The SERVICE] :
the abstract level of service, he can be mapped directly to a physical service (host pointing ip + port), you can also point to an upstream load balancing to do. Popular said that the service is backstage access interface configuration.

Navigate to the SERVICESpage and addADD NEW SERVICE

Field Description :

UrlParameter is a simplified parameter disposable adding protocol, host, port, and path. Also, do not put SERVICEas back-end specific API, we take it as a great service that the following have multiple API (endpoint or route). So when creating the service fill in the domain name service on the line. Of course, also be a band pathof Url, associated with each of the API such ( ROUTE) will be routed to the pathupper.

Attributes description
name (required) service name.
tags (optional) Optionally add a tag to the service
url (Optional) The protocol, host, port, and immediately set to brief the path attribute. This property is write-only (Management API never "return" url)
protocol (Required) This protocol is used to communicate with the upstream. It can be http (default) or https.
host(必填) upstream服务器的主机。
port(必填) upstream服务器端口。默认为80
path(可选) 在向upstream服务器请求中使用的路径。默认为空。
retries(可选) 在代理失败的情况下执行的重试次数。默认值是5。
connect_timeout(可选) 建立到upstream服务器的连接的超时时间。默认为60000。
write_timeout(可选) 将请求发送到upstream服务器的两个连续写操作之间的超时时间。默认为60000。
read_timeout(可选) 将请求发送到upstream服务器的两个连续读取操作之间的超时时间。默认为60000。

添加成功返回SERVICES界面,查看添加的Services信息,两种查看方式:

2. ADD ROUTE

[ROUTE]:
路由的抽象,他负责将实际的 request 映射到 service

返回SERVICES界面,选择example-service服务

选择Routes,然后ADD ROUTE

字段说明:

属性 描述
name(可选) 定义名称
tags(可选) 向路由添加标记
protocols(可选) 这条路线应该允许的协议列表。默认情况下,它是“http”、“https”,这意味着路由接受这两种方式。当设置为“HTTPS”时,HTTP请求会被请求升级到HTTPS。通过表单编码,符号是协议=http&协议=https。使用JSON,使用数组。
methods(半可选) 与此路由相匹配的HTTP方法列表。例如: ["GET", "POST"].至少有一个主机、路径或方法必须被设置。用表单编码参数是methods[]=GET&methods[]=OPTIONS。使用JSON,使用数组。
hosts(半可选) 与此路径匹配的域名列表。例如:example.com. 至少有一个主机、路径或方法必须被设置。用表单编码参数是 hosts[]=foo.com&hosts[]=bar.com。使用JSON,使用数组。
paths(半可选) 与此路径相匹配的路径列表。例如: /my-path.至少有一个主机、路径或方法必须被设置。用表单编码参数是 paths[]=/foo&paths[]=/bar. 使用JSON,使用数组。
Regex priority(可选) 当多个路由同时使用正则表达式匹配某个给定请求时,用来选择哪个路由解析该请求的数字。当两个路由匹配路径并且具有相同的regex_优先级时,将使用较旧的路由(最低创建位置)。注意,非regex路由的优先级不同(较长的非regex路由在较短的路由之前匹配)。默认为0。
strip_path(可选) 当通过其中一条路径匹配路由时,从上游upstream请求URL中去掉匹配前缀。默认值为true。
preserve_host(可选) 当通过一个主机域名匹配一条路由时,在upstream请求头中使用请求主机头。默认设置为false,upstream主机头将是服务主机的主机头。

添加成功返回SERVICES界面,点击Routes,查看路由。
又或者,可以从左侧ROUTES导航进入查看路由。

3. 验证API 代理

浏览器验证

Kong API需要通过Routes规则中配置的hosts or path对请求进行API调度控制。

由于我们Routes规则中配置的hosts是虚拟域名,因此,我们需要在本地及服务器hosts列表中添加dns解析,这样在浏览器才可以携带host请求信息去正常访问Kong API。

“WIN +R 输入drivers”,调出hosts`

10.122.45.97 test.example.com

请求kong api

$ curl -i -X GET \
  --url http://localhost:8000/request \
  --header 'Host: test.example.com'

kong使用Admin API实现接口代理

请参阅官方文档

 
[sleepy↓]





 

Guess you like

Origin www.cnblogs.com/sunhongleibibi/p/12024386.html