安装准备
- pgsql 9.4+,或者一个对象数据库,CASSANDRA
我选择的是pqsql,可以见 关于pgsql
这篇
安装
0.11以后的版本可能是docker的了,之前的可以用安装包,下载地址
https://github.com/Mashape/kong/releases/
安装后,命令
kong --help
启动
kong start
还可以
brew install kong
客户端
- 客户端版本的 kongDash
web版本 kong-dashboard
sudo npm install -g kong-dashboard kong-dashboard start 默认端口 8080,或者指定端口 kong-dashboard start -p 9000
现在主要说 web版本的客户端
添加一个api
有一个kong的文档,有个说明
ATTRIBUTE | xxx | DESCRIPTION |
---|---|---|
name | The API name. 名称 | |
hosts | semi-optional | A comma-separated list of domain names that point to your API. For example: example.com. At least one of hosts, uris, or methods should be specified. 指向您的API的域名的逗号分隔列表。例如:example.com。的至少一个hosts,uris或methods应符合规定。 本地服务IP |
uris | semi-optional | A comma-separated list of URIs prefixes that point to your API. For example: /my-path. At least one of hosts, uris, or methods should be specified. 以逗号分隔的URI列表,指向您的API。例如:/my-path。的至少一个hosts,uris或methods应符合规定。 本地后缀 |
methods | semi-optional | A comma-separated list of HTTP methods that point to your API. For example: GET,POST. At least one of hosts, uris, or methods should be specified. 指向您的API的HTTP方法的逗号分隔列表。例如:GET,POST。的至少一个hosts,uris或methods应符合规定。 请求方式 |
upstream_url | The base target URL that points to your API server. This URL will be used for proxying requests. For example: https://example.com. 指向您的API服务器的基本目标网址。此URL将用于代理请求。例如:https://example.com。 api全url | |
strip_uri | optional | When matching an API via one of the uris prefixes, strip that matching prefix from the upstream URI to be requested. Default: true. 通过其中一个uris前缀匹配API时,从上游URI中删除匹配的前缀。默认值:true。 |
preserve_host | optional | When matching an API via one of the hosts domain names, make sure the request Host header is forwarded to the upstream service. By default, this is false, and the upstream Host header will be extracted from the configured upstream_url. 通过其中一个hosts域名匹配API时,请确保将请求Host头转发到上游服务。默认情况下,将从配置中提取false上游标Host头upstream_url。 |
retries | optional | The number of retries to execute upon failure to proxy. The default is 5. 无法代理时执行的重试次数。默认是5。 |
upstream_connect_timeout | optional | The timeout in milliseconds for establishing a connection to your upstream service. Defaults to 60000. 建立与上游服务连接的超时时间(以毫秒为单位)。默认为60000。 |
upstream_send_timeout | optional | The timeout in milliseconds between two successive write operations for transmitting a request to your upstream service Defaults to 60000. 用于将请求发送到上游服务的两个连续写操作之间的超时(以毫秒为单位)默认为60000。 |
upstream_read_timeout | optional | The timeout in milliseconds between two successive read operations for transmitting a request to your upstream service Defaults to 60000. 两个连续读取操作之间以超过毫秒的速度发送请求到您的上游服务默认为60000。 |
https_only | optional | To be enabled if you wish to only serve an API through HTTPS, on the appropriate port (8443 by default). Default: false. 如果您希望通过HTTPS仅在适当的端口(8443默认情况下)通过HTTPS提供API来启用。默认值:false。 |
http_if_terminated | optional | Consider the X-Forwarded-Proto header when enforcing HTTPS only traffic. Default: true. X-Forwarded-Proto在实施仅HTTPS流量时考虑头。默认值:true。 |
实际上就添加5项目
# | optional | e.g. |
---|---|---|
1 | name | xyinfo |
2 | hosts | 127.0.0.1 |
3 | uris | /views/page/demo.html |
4 | methods | GET |
5 | upstream url | http://shiw.crossess.com/api/xyinfo |
kong
相关的端口
端口 | 说明 | 举例 |
---|---|---|
8000 | 客户端调用api端口,网关对外开放端口。 | 新建一个api,访问path= /test,则访问 http://10.110.2.3:8000/test |
8001 | kong admin api管理端口,可以通过该端口对api、consumer、plugin进行管理, | 查看名字叫test 这个api配置信息,访问 http://10.110.2.3:8001/apis/test,查看所有api信息,访问 http://10.110.2.3:8001/apis |
8443 | kong https url https | |
8080 | kong dashboard ui | 一个web客户端 |
新增一个api
$ curl -i -X POST --url http://127.0.0.1:8001/apis/ --data 'name=person2api' --data 'methods=GET' --data 'uris=/testUser' --data 'hosts=127.0.0.1' --data 'upstream_url=http://10.110.2.54:8040/metrics'
- HTTP/1.1 201 Created
- Date: Thu, 27 Jul 2017 11:24:09 GMT
- Content-Type: application/json; charset=utf-8
- Transfer-Encoding: chunked
- Connection: keep-alive
- Access-Control-Allow-Origin: *
- Server: kong/0.10.3
1 | {"methods":["GET"],"http_if_terminated":true,"id":"dffb6cbb-1a0d-4a1f-aff8-72b7a2aad586","retries":5,"preserve_host":false,"created_at":1501154650000,"upstream_connect_timeout":60000,"upstream_url":"http:\/\/10.110.2.54:8040\/metrics","upstream_read_timeout":60000,"upstream_send_timeout":60000,"https_only":false,"strip_uri":true,"uris":["\/testUser"],"name":"person2api","hosts":["127.0.0.1"]} |
插件的问题(内容来源)
Kong默认插件
- 身份认证:Kong提供了Basic Authentication、Key authentication、OAuth2.0 authentication、HMAC authentication、JWT、LDAP authentication认证实现。
- 安全:ACL(访问控制)、CORS(跨域资源共享)、动态SSL、IP限制、爬虫检测实现。
- 流量控制:请求限流(基于请求计数限流)、上游响应限流(根据upstream响应计数限流)、请求大小限制。限流支持本地、Redis和集群限流模式。
- 分析监控:Galileo(记录请求和响应数据,实现API分析)、Datadog(记录API Metric如请求次数、请求大小、响应状态和延迟,可视化API Metric)、Runscope(记录请求和响应数据,实现API性能测试和监控)。
- 转换:请求转换(在转发到upstream之前修改请求)、响应转换(在upstream响应返回给客户端之前修改响应)。
- 日志:TCP、UDP、HTTP、File、Syslog、StatsD、Loggly等。
可能需要扩展的插件: API级别的超时、重试、fallback策略、缓存、API聚合、ABTest等.