/ Afred's Blog / Kong 初体验

Kong 初体验

2017-02-26 posted in [编程之旅]

Kong是什么?

官网是这么定义的:

The open-source API Gateway and Microservices Management Layer, delivering high performance and reliability.

Kong是一个开源的网关管理工具,基于Nginx,利用各种插件提供高性能高可用的服务,架构图(摘自官网)如下:

架构图

安装

安装流程可以参考安装地址

添加接口sample

Kong有两个Http端口,默认是8000和8001,前者是网关访问端口,后者是管理后台的rest端口。

假设有一个本地有一个后端服务,端口是12345,接口地址为/spring/test,通过rest api添加到Kong:

  ~ curl -i -X POST \
> --url http://127.0.0.1:8001/apis/ \
> --data 'name=mockbin' \
> --data 'upstream_url=http://127.0.0.1:12345' \
> --data 'request_host=mockbin.com' \
> --data 'request_path=/spring/test'
HTTP/1.1 201 Created
Date: Sun, 26 Feb 2017 03:54:37 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.9.9

{"upstream_url":"http:\/\/127.0.0.1:12345","strip_request_path":false,"request_path":"\/spring\/test","id":"5d94bcdf-2548-4606-9279-c911b69044ab","created_at":1488081277000,"preserve_host":false,"name":"mockbin","request_host":"mockbin.com"}

添加成功,接下来看看效果。在/etc/hosts文件中添加host:

127.0.0.1 mockbin.com

使用curl --verbose模拟请求,:

  ~ curl --verbose -i -X GET \
  --url http://mockbin.com:8000/spring/test
*   Trying 127.0.0.1...
* Connected to mockbin.com (127.0.0.1) port 8000 (#0)
> GET /spring/test HTTP/1.1
> Host: mockbin.com:8000
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sun, 26 Feb 2017 03:56:58 GMT
Date: Sun, 26 Feb 2017 03:56:58 GMT
< Content-Type: application/octet-stream
Content-Type: application/octet-stream
< Transfer-Encoding: chunked
Transfer-Encoding: chunked
< Connection: keep-alive
Connection: keep-alive
< X-Kong-Upstream-Latency: 4
X-Kong-Upstream-Latency: 4
< X-Kong-Proxy-Latency: 0
X-Kong-Proxy-Latency: 0
< Via: kong/0.9.9
Via: kong/0.9.9

<
* Connection #0 to host mockbin.com left intact
hello world%

可以看到后端服务器返回hello wolrd,说明整个请求链正常。

Kong目前还不支持后端配置多个upstream。

comments powered by Disqus