您将向Kong添加一个API。为此,您首先需要添加一个Service;这就是Kong用来指定它管理的上游API和微服务的名称。
出于本指南的目的,我们将创建一个指向Mockbin API的服务。Mockbin是一个“echo”类型的公共网站,它将返回请求的请求作为响应返回给请求者。这有助于了解Kong如何代理您的API请求。
在开始向Service发出请求之前,您需要为其添加一个Route。Route指定请求在到达Kong后如何(以及是否)发送到其服务。一个Service可以有多个Route.
在配置完Service和Route以后,就可以通过Kong使用他们发送请求。
Kong在:8001
端口上公开了RESTful Admin API。 Kong的配置,包括添加的Service和Route,是通过对该API发送请求进行的。
1.使用Admin API添加您的服务
- 执行以下cURL请求,将你的第一个Service(指向Mockbin API)添加到Kong:
$ curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=example-service' \
--data 'url=http://mockbin.org'
example-service这是我们的服务名 不是example_service
In our example, the request body contained two strings:
name
: The name of the serviceurl
: An argument that populates thehost
,port
, andpath
attributes of the service
如果请求成功 ,你会收到 http的201
响应,类似如下:
HTTP/1.1 201 Created
Date: Fri, 11 Nov 2022 03:38:45 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: http://localhost:8002
X-Kong-Admin-Request-ID: 3xk6T3V665Z8cwHo5ov0Oqi08dkS58TG
vary: Origin
Access-Control-Allow-Credentials: true
vary: Origin
Content-Length: 376
X-Kong-Admin-Latency: 14
Server: kong/3.0.1.0-enterprise-edition
{
"host": "mockbin.org",
"name": "example-service",
"enabled": true,
"connect_timeout": 60000,
"read_timeout": 60000,
"retries": 5,
"protocol": "http",
"path": null,
"port": 80,
"tags": null,
"client_certificate": null,
"tls_verify": null,
"created_at": 1661346938,
"updated_at": 1661346938,
"tls_verify_depth": null,
"id": "3b2be74e-335b-4f25-9f08-6c41b4720315",
"write_timeout": 60000,
"ca_certificates": null
}
- 查看服务是否添加成功
但你创建了一个服务,kong gateway会给这个服务赋予一个唯一id,如上图“3b2be74e-335b-4f25-9f08-6c41b4720315”,id和name可以被用来identify the service in subsequent requests. This is the service URL and takes the form of /services/{service name or id}
curl -X GET http://localhost:8001/services/example_service
返回如下信息说明添加成功:
{"tls_verify":null,"client_certificate":null,"tls_verify_depth":null,"tags":null,"ca_certificates":null,"path":null,"read_timeout":60000,"protocol":"http","name":"example-service","id":"44a70e67-070e-483d-a824-ff1ed907a049","created_at":1668137925,"retries":5,"updated_at":1668137925,"enabled":true,"write_timeout":60000,"connect_timeout":60000,"host":"mockbin.org","port":80}
- 列出添加的服务
curl -X GET http://localhost:8001/services
返回如下:
{"next":null,"data":[{"tls_verify":null,"client_certificate":null,"tls_verify_depth":null,"tags":null,"ca_certificates":null,"path":null,"read_timeout":60000,"protocol":"http","name":"example-service","id":"44a70e67-070e-483d-a824-ff1ed907a049","created_at":1668137925,"retries":5,"updated_at":1668137925,"enabled":true,"write_timeout":60000,"connect_timeout":60000,"host":"mockbin.org","port":80}]}
2.路由管理
创建路由
路由定义了如何通过kong gateway代理一个请求,你可以通过POST一个专门的服务URL创建一个路由。
我们从创建一个路由使用/mock路径来转发example-service:
curl -i -X POST http://localhost:8001/services/example-service/routes \ --data 'paths[]=/mock' \ --data name=example-route
如果上面的路由被成功创建返回如下:http的201响应码
HTTP/1.1 201 Created Date: Fri, 11 Nov 2022 08:29:23 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Access-Control-Allow-Origin: http://localhost:8002 X-Kong-Admin-Request-ID: 8G90A8IGPUF4vKESaYmAeh5Unsyny4pl vary: Origin Access-Control-Allow-Credentials: true Content-Length: 485 X-Kong-Admin-Latency: 14 Server: kong/3.0.1.0-enterprise-edition {"paths":["/mock"],"methods":null,"regex_priority":0,"destinations":null,"service":{"id":"44a70e67-070e-483d-a824-ff1ed907a049"},"path_handling":"v0","protocols":["http","https"],"request_buffering":true,"response_buffering":true,"id":"6ec8a1dd-f187-416b-8f50-9b9001fbb0a8","created_at":1668155363,"preserve_host":false,"tags":null,"strip_path":true,"sources":null,"snis":null,"updated_at":1668155363,"name":"example-route","hosts":null,"headers":null,"https_redirect_status_code":426}
查看route 路由配置
和上面的service一样,kong赋予了route唯一的id,可以通过如下格式查看配置
/routes/{route name or id}
,通过如下格式查看刚才添加的路由:curl -X GET http://localhost:8001/services/example-service/routes/example-route
返回的信息包含了刚才添加的路由配置:
{"message":"Not found"}root@ly-huawei:~# curl -X GET http://localhost:8001/services/example-service/routes/example-route {"paths":["/mock"],"methods":null,"regex_priority":0,"destinations":null,"service":{"id":"44a70e67-070e-483d-a824-ff1ed907a049"},"path_handling":"v0","protocols":["http","https"],"request_buffering":true,"response_buffering":true,"id":"6ec8a1dd-f187-416b-8f50-9b9001fbb0a8","created_at":1668155363,"preserve_host":false,"tags":null,"strip_path":true,"sources":null,"snis":null,"updated_at":1668155363,"name":"example-route","hosts":null,"headers":null,"https_redirect_status_code":426}root@ly-huawei:~# curl -X GET http://localhost:8001/services/example-service/routes/ {"next":null,"data":[{"paths":["/mock"],"methods":null,"regex_priority":0,"destinations":null,"service":{"id":"44a70e67-070e-483d-a824-ff1ed907a049"},"path_handling":"v0","protocols":["http","https"],"request_buffering":true,"response_buffering":true,"id":"6ec8a1dd-f187-416b-8f50-9b9001fbb0a8","created_at":1668155363,"preserve_host":false,"tags":null,"strip_path":true,"sources":null,"snis":null,"updated_at":1668155363,"name":"example-route","hosts":null,"headers":null,"https_redirect_status_code":426}]}
修改路由配置
和服务一样,route也可以通过发送一个PATCH请求进行动态更新。比如通过如下命令修改配置的tag为“tutorial”,虽然我上面由于懒没有写service的修改,大家自己看官网吧。
curl --request PATCH \ --url localhost:8001/services/example-service/routes/example-route \ --data tags="tutorial"
列出所有的路由配置
curl http://localhost:8001/routes
成功返回http的200状态码
{"next":null,"data":[{"paths":["/mock"],"methods":null,"regex_priority":0,"destinations":null,"service":{"id":"44a70e67-070e-483d-a824-ff1ed907a049"},"path_handling":"v0","protocols":["http","https"],"request_buffering":true,"response_buffering":true,"id":"6ec8a1dd-f187-416b-8f50-9b9001fbb0a8","created_at":1668155363,"preserve_host":false,"tags":["tutorial"],"strip_path":true,"sources":null,"snis":null,"updated_at":1668157098,"name":"example-route","hosts":null,"headers":null,"https_redirect_status_code":426}]}
3.测试上面添加的代理是否成功
Kong 就是一个API Gateway,它将客户端的请求通过路由转发到恰当的上层应用,例如你可以通过http://localhost:8000/mock
访问https://mockbin.org/
- 8001是kong的管理端口,8000是客户端连接端口
Mockbin 提供了
/requests
用来响应客户端的请求。curl -X GET http://localhost:8000/mock/requests
你会得到如下响应:
{ "startedDateTime": "2022-11-11T09:25:39.897Z", "clientIPAddress": "172.19.0.1", "method": "GET", "url": "http://localhost/requests", "httpVersion": "HTTP/1.1", "cookies": {}, "headers": { "host": "mockbin.org", "connection": "close", "accept-encoding": "gzip", "x-forwarded-for": "172.19.0.1,121.36.55.115, 172.70.210.180", "cf-ray": "7685fb5b9efd7b19-LAX", "x-forwarded-proto": "http", "cf-visitor": "{\"scheme\":\"http\"}", "x-forwarded-host": "localhost", "x-forwarded-port": "80", "x-forwarded-path": "/mock/requests", "x-forwarded-prefix": "/mock", "user-agent": "curl/7.68.0", "accept": "*/*", "cf-connecting-ip": "121.36.55.115", "cdn-loop": "cloudflare", "x-request-id": "951f7f44-b1c3-47a6-8d67-57a645fc948f", "via": "1.1 vegur", "connect-time": "0", "x-request-start": "1668158739890", "total-route-time": "0" }, "queryString": {}, "postData": { "mimeType": "application/octet-stream", "text": "", "params": [] }, "headersSize": 591, "bodySize": 0 }
**网上的中文教程都是生搬硬套过来的,根本没有实践过,~~~~,还是看官网吧