You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ docker-compose up --no-recreate -d
Starting kong-database ... done Creating kong ... done
$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
kong /docker-entrypoint.sh kong ... Up 0.0.0.0:7946->7946/tcp, 0.0.0.0:7946->7946/udp, 0.0.0.0:8000->8000/tcp, 0.0.0.0:8001->8001/tcp, 0.0.0.0:8443->8443/tcp, 8444/tcp
kong-database docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
$ curl -i -X GET --url http://127.0.0.1:8000/
HTTP/1.1 404 Not Found
Date: Mon, 09 Jul 2018 06:33:31 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Server: kong/0.14.0
Content-Length: 58
{"message":"no route and no API found with those values"}
发现返回的是404 Not Found,因为我们在 route 中定义了 host,所以需要在 header 中指定 host。 根据 Kong Admin Api 要求,添加 Route 时,methods、hosts、path三者至少选择一个。修改请求如下:
Kong 是 Mashape 开源的高性能高可用 API 网关和 API 管理服务层。它基于 OpenResty 进行 API 管理,并提供了插件实现 API 的 AOP。最近因工作的需要研究了 Kong 的相关应用实现。
准备
kong 的官方文档提供了基于不同平台的安装方式,为了方便,这里使用 docker-compose 进行。
上述创建名为 kong-gateway 的目录并且进入,然后在 kong-gateway 目录中创建 docker-compose.yml 文件,并且创建了 config、 plugins、 postgresql 三个文件夹。其中, config 文件夹用于存放 kong 网关配置, plugins 文件夹用于存放自定义插件, postgresql 文件夹用于存放 postgresql 数据文件,这三个文件夹将被挂载到 docker 容器中。 docker-compose.yml 文件定义如下:
config 文件夹下放置 kong 的配置文件 kong.conf,简单定义如下:
上述准备好后的 kong-gateway文件夹目录如下:
启动
首先准备数据库,执行下面命令:
执行过程中将拉取 docker-compose.yml 中定义的 postgres:9.6 镜像和 kong:latest镜像,并执行数据库迁移操作。
迁移完成后,执行下面命令,启动 kong 网关:
这时候控制台执行 curl http://127.0.0.1:8001/status可以看到管理端口输出的状态json。
运行
Servie Object
Kong 中的 Service 对象代表了上游服务的一个抽象,主要由协议、主机名、端口、路径等组成。Service 和路由关联(一个Service可能会被关联至多个路由)。更多参见Kong Admin Api Service Object。
我们首先执行命令创建一个 Service, 这个 Service 名称为 baidu-service,对应的 url 地址为 http://www.baidu.com:
可以通过以下命令查看所有已创建的 Service:
也可以根据 serviceId 查看某个具体的 Service:
Route Object
路由定义了匹配客户端请求的规则,每一个路由关联一个 Service,每一个 Service 有可能被多个路由关联,每一个匹配到指定的路由请求将被代理到它关联的 Service 上。 更多参见Kong Admin Api Route Object。
首先创建一个Route:
可以通过以下命令查看所有已创建的 Routes:
也可以根据 routeId 查看某个具体的 Route:
运行
先执行如下命令执行:
发现返回的是404 Not Found,因为我们在 route 中定义了 host,所以需要在 header 中指定 host。 根据 Kong Admin Api 要求,添加 Route 时,methods、hosts、path三者至少选择一个。修改请求如下:
原文链接: http://www.yuxiumin.com/2018/07/08/kong-api-gateway-install/
The text was updated successfully, but these errors were encountered: