2

我正在开发一个带有微服务的小项目,我想将 Kong 配置为我的结构的 API 网关,但我无法将来自 Kong 的传入请求转发到我的服务。

例如,我有一个在 address172.18.0.15和 port本地运行的 docker 容器8006。我可以很好地访问 api 172.18.0.15:8006/compute,我想配置 kong 以便能够在localhost:8001/compute. 我错过了什么吗?应该api出现在我的 url 中以便 Kong 发现我的 api?这是我添加到 Kong 的配置,我基本上只是添加了所有 REST 方法并将 url:port 设置为 upstream-url 字段。

{
    "data": [
        {
            "created_at": 1514479841682,
            "http_if_terminated": false,
            "https_only": false,
            "id": "9cf925a5-2c27-4dbc-ae2a-e70fba94ef53",
            "methods": [
                "GET",
                "POST",
                "PUT",
                "DELETE"
            ],
            "name": "compute",
            "preserve_host": false,
            "retries": 5,
            "strip_uri": true,
            "upstream_connect_timeout": 60000,
            "upstream_read_timeout": 60000,
            "upstream_send_timeout": 60000,
            "upstream_url": "http://172.18.0.15:8006"
        }
    ],
    "total": 1
}

这对我不起作用:当我尝试通过 localhost 访问 API 时,请求超时并出现499错误。非常感谢任何帮助。

编辑:我尝试通过设置 URI 字段来添加我的 API。我已经检查过使用docker logs kong,看起来 kong 确实在访问正确的地址,但由于某些未知原因而超时:

2017/12/28 17:55:25 [error] 47#0: *103 upstream timed out (110: Connection timed out) while connecting to upstream, client: 172.17.0.1, server: kong, request: "GET /compute?origin=20,20&destination=20,20 HTTP/1.1", upstream: "http://172.18.0.15:8006/compute?origin=20,20&destination=20,20", host: "localhost:8000"

但是,如果我使用 curl 访问相同的地址,它就可以正常工作:

# curl http://172.18.0.15:8006/compute?origin=20,20\&destination=20,20
[]

我非常怀疑其他人会遇到同样的问题,但问题出在我的 Docker 配置中,我忘记将 api 网关与服务放在同一子网中。解决这个问题解决了我的问题。

4

0 回答 0