【问题标题】:Kong (0.10) API setup and usageKong (0.10) API 设置和使用
【发布时间】:2017-04-19 23:53:15
【问题描述】:

我是 Kong 的新手,我正在尝试弄脏 Kong。 我有一个 kong (0.10) 和 Cassandra (latest) 在单独的 docker 容器上运行的环境。我的操作系统是 macOS -Sierra 10.12.4。

Docker 中的 Cassandra

docker run -d --name kong-database -p 9042:9042 cassandra:latest

docker中的Kong

 docker run -d --name kong \
   --link kong-database:kong-database \
   -e "KONG_DATABASE=cassandra" \
   -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
   -e "KONG_PG_HOST=kong-database" \
   -p 8000:8000 \
   -p 8443:8443 \
   -p 8001:8001 \
   -p 7946:7946 \
   -p 7946:7946/udp \
   kong

根据Kong documentation,我通过 Kong Admin API 添加了我的第一个 API(用于获取 IP)。

curl -i -X POST \
  --url http://localhost:8001/apis/ \
  --data 'name=example-api' \
  --data 'hosts=example.com' \
  --data 'upstream_url=http://httpbin.org/ip'
HTTP/1.1 201 Created
Date: Wed, 19 Apr 2017 23:39:13 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: false
Server: kong/0.10.1

{"http_if_terminated":true,"id":"9bab7ba9-f1f2-4b4d-aa9d-9966da17f94b","retries":5,"preserve_host":false,"created_at":1492645153409,"upstream_connect_timeout":60000,"upstream_url":"http:\/\/httpbin.org\/ip","upstream_send_timeout":60000,"https_only":false,"upstream_read_timeout":60000,"strip_uri":true,"name":"example-api","hosts":["example.com"]}

当我执行以下命令进行测试时,

curl -i -X GET --url http://localhost:8000/ --header 'Host: example.com'

我明白了,

    HTTP/1.1 404 NOT FOUND
Date: Wed, 19 Apr 2017 23:40:48 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 233
Connection: keep-alive
Server: gunicorn/19.7.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: kong/0.10.1
X-Kong-Upstream-Latency: 590
X-Kong-Proxy-Latency: 93

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

但是,当我直接curl http://httpbin.org/ip 时,我得到了我的 IP。

如何配置 Kong 以获得相同的结果?

【问题讨论】:

    标签: kong


    【解决方案1】:

    尝试将upstream_url改为http://httpbin.org,然后如下curl请求:

    curl -i -X GET --url http://localhost:8000/ip --header 'Host: example.com'
    

    我今天收到了同样的消息,发现它不起作用,当你最后有一个斜线时。当您使用此 upstream_url 添加 API 时,可能有一些东西会产生这种情况。

    编辑: 使用您的配置,请求应该如下所示:

    curl -i -X GET --url http://localhost:8000 --header 'Host: example.com'
    

    请注意网址末尾删除的斜线。也许这会导致此错误,您可以修复它,而无需编辑您的 API 配置 :)

    【讨论】:

      【解决方案2】:

      您添加的 api 有一个您未在测试 url 中指定的名称 (example-api)

      所以应该是这样的:

      curl -i -X GET --url http://localhost:8000/example-api --header 'Host: example.com'

      【讨论】:

      • 我不记得在请求 URL 中明确指定 API 名称的必要性
      • Matthias S,那么 kong 代理如何知道将您的呼叫重定向到哪个 API? :)
      • 我认为主机头是为此目的。还是在 v10.x 中有一些重大变化?我可以通过 Kong 发送请求,而无需在请求 URL 中指定确切的 api。好吧,至少是 0.9.x。此外,快速入门指南也没有引用此行为
      • 是的,你也可以使用主机头
      • 好吧,我不知道,我可以在请求 URL 中指定 api,现在我们都学到了一些东西 :-D
      猜你喜欢
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多