业务场景介绍
Mockbin 是一个请求响应的测试网站。
我们为KONG添加路由,并将请求转发至Mockbin。
- 单个服务可以有多个路由。
- Kong的配置,包括添加服务和路由,是通过该API的请求进行的,默认http端口为8001。
- Kong默认处理路由转发的请求端口为8000。
使用ADMIN API添加服务
$ curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=example-service' \
--data 'url=http://mockbin.org'
应答信息:
HTTP/1.1 201 Created
Date: Mon, 20 Apr 2020 02:21:26 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.3
Content-Length: 296
X-Kong-Admin-Latency: 16
{
"host":"mockbin.org",
"created_at":1587349286,
"connect_timeout":60000,
"id":"7bed6af7-c6d0-4228-a2b0-e0103d17aa0a",
"protocol":"http",
"name":"example-service",
"read_timeout":60000,
"port":80,
"path":null,
"updated_at":1587349286,
"retries":5,
"write_timeout":60000,
"tags":null,
"client_certificate":null
}
使用ADMIN API添加路由
$ curl -i -X POST \
--url http://localhost:8001/services/example-service/routes \
--data 'hosts[]=example.com'
应答信息:
HTTP/1.1 201 Created
Date: Mon, 20 Apr 2020 02:28:52 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.3
Content-Length: 429
X-Kong-Admin-Latency: 171
{
"id":"d31de681-7c91-4bb5-8d68-e6bccc10a95f",
"path_handling":"v0",
"paths":null,"destinations":null,
"headers":null,
"protocols":["http","https"],
"methods":null,
"snis":null,
"service":{"id":"7bed6af7-c6d0-4228-a2b0-e0103d17aa0a"},
"name":null,
"strip_path":true,
"preserve_host":false,
"regex_priority":0,
"updated_at":1587349732,
"sources":null,
"hosts":["example.com"],
"https_redirect_status_code":426,
"tags":null,
"created_at":1587349732
}
此时,我的第一个路由已经创建成功。
测试KONG路由
使用curl测试Kong的路由转发
$ curl -i -X GET \
--url http://localhost:8000/ \
--header 'Host: example.com'
成功响应表示我们的Kong路由配置成功。