如何在无数据库和声明式配置中访问Kong服务

时间:2019-12-30 14:29:04

标签: microservices api-gateway kong

我已经用声明性的方式配置了kong。我的服务和路线已配置为。

services:
 - name: mock-service
   url: http://mockbin.org/request
   path: /
   tags:
   - example
   routes:
    - name: mock-route
      methods:
      - GET
      hosts: ["mockbin.org"]
      paths: ["/mock"] 

我的服务和路线已注册,我可以看到响应为

http://localhost:8001/services  :-  Response 


{ 
   "next":null,
   "data":[ 
      { 
         "host":"mockbin.org",
         "created_at":1577708204,
         "connect_timeout":60000,
         "id":"9ff0ee4e-7c2b-51e3-9490-f93f946547e8",
         "protocol":"http",
         "name":"mock-service",
         "read_timeout":60000,
         "port":80,
         "path":"/request",
         "updated_at":1577708204,
         "client_certificate":null,
         "tags":[ 
            "example"
         ],
         "write_timeout":60000,
         "retries":5
      }
   ]
}


http://localhost:8001/routes   Response

{ 
   "next":null,
   "data":[ 
      { 
         "id":"21113e89-d366-5f75-b44a-cef07644d282",
         "tags":null,
         "updated_at":1577708204,
         "destinations":null,
         "headers":null,
         "protocols":[ 
            "http",
            "https"
         ],
         "created_at":1577708204,
         "snis":null,
         "hosts":[ 
            "mockbin.org"
         ],
         "name":"mock-route",
         "preserve_host":false,
         "regex_priority":0,
         "strip_path":true,
         "sources":null,
         "methods":[ 
            "GET"
         ],
         "https_redirect_status_code":426,
         "paths":[ 
            "/mock"
         ],
         "service":{ 
            "id":"9ff0ee4e-7c2b-51e3-9490-f93f946547e8"
         }
      }
   ]
}

When i am trying to acess the my mockbin service though kong . i not getting the response .

http://localhost:8000/mock    if host is not passed in header

{
"message": "no Route matched with those values"
}


http://localhost:8000/mock   if host is passed in header

-header 

 Host   mockbin.org

 response status :- 500 

 An unexpected error occurred

kong日志文件中的错误:

2019/12/30 17:46:44 [notice] 60919#0: *2 [kong] init.lua:298 declarative config loaded from /var/kong.yml, context: init_worker_by_lua*
2019/12/30 17:46:44 [warn] 60917#0: *1 [lua] globalpatches.lua:52: sleep(): executing a blocking 'sleep' (0.001 seconds), context: init_worker_by_lua*
2019/12/30 17:49:56 [error] 60920#0: *1586 upstream timed out (110: Connection timed out) while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://3.228.254.201:80/request", host: "mockbin.org"
2019/12/30 17:49:56 [error] 60920#0: *1586 [lua] balancer.lua:917: balancer_execute(): DNS resolution failed: dns server error: 100 cache only lookup failed. Tried: ["(short)mockbin.org:(na) - cache-miss","mockbin.org:1 - cache only lookup failed/dns server error: 100 cache only lookup failed","mockbin.org:33 - cache only lookup failed/dns server error: 100 cache only lookup failed","mockbin.org:5 - cache only lookup failed/dns server error: 100 cache only lookup failed"] while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://3.228.254.201:80/request", host: "mockbin.org"
2019/12/30 17:49:56 [error] 60920#0: *1586 [lua] init.lua:800: balancer(): failed to retry the dns/balancer resolver for mockbin.org' with: dns server error: 100 cache only lookup failed while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://3.228.254.201:80/request", host: "mockbin.org"
2019/12/30 17:57:02 [error] 60918#0: *6694 upstream timed out (110: Connection timed out) while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://3.228.254.201:80/request", host: "mockbin.org"
2019/12/30 17:58:02 [error] 60918#0: *6694 upstream timed out (110: Connection timed out) while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://52.21.131.234:80/request", host: "mockbin.org"
2019/12/30 17:58:02 [error] 60918#0: *6694 [lua] balancer.lua:917: balancer_execute(): DNS resolution failed: dns server error: 100 cache only lookup failed. Tried: ["(short)mockbin.org:(na) - cache-miss","mockbin.org:1 - cache only lookup failed/dns server error: 100 cache only lookup failed","mockbin.org:33 - cache only lookup failed/dns server error: 100 cache only lookup failed","mockbin.org:5 - cache only lookup failed/dns server error: 100 cache only lookup failed"] while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://52.21.131.234:80/request", host: "mockbin.org"
2019/12/30 17:58:02 [error] 60918#0: *6694 [lua] init.lua:800: balancer(): failed to retry the dns/balancer resolver for mockbin.org' with: dns server error: 100 cache only lookup failed while connecting to upstream, client: 172.21.200.35, server: kong, request: "GET /mock HTTP/1.1", upstream: "http://52.21.131.234:80/request", host: "mockbin.org"

我已经按照文章进行了解决。但没有成功。

https://docs.konghq.com/0.13.x/getting-started/configuring-a-service/

https://www.jerney.io/secure-apis-kong-keycloak-1/

https://medium.com/@vasista/explaining-kong-plugins-key-authentication-and-rate-limiting-on-flask-api-9f922c4f051

2 个答案:

答案 0 :(得分:0)

finally i am able to resolve it. 

sol :-

    enter code here


#------------------------------------------------------------------------------
# DNS RESOLVER
#------------------------------------------------------------------------------

# By default the DNS resolver will use the standard configuration files
# `/etc/hosts` and `/etc/resolv.conf`. The settings in the latter file will be
# overridden by the environment variables `LOCALDOMAIN` and `RES_OPTIONS` if
# they have been set.
#
# Kong will resolve hostnames as either `SRV` or `A` records (in that order, and
# `CNAME` records will be dereferenced in the process).
# In case a name was resolved as an `SRV` record it will also override any given
# port number by the `port` field contents received from the DNS server.
#
# The DNS options `SEARCH` and `NDOTS` (from the `/etc/resolv.conf` file) will
# be used to expand short names to fully qualified ones. So it will first try
# the entire `SEARCH` list for the `SRV` type, if that fails it will try the
# `SEARCH` list for `A`, etc.
#
# For the duration of the `ttl`, the internal DNS resolver will load balance each
# request it gets over the entries in the DNS record. For `SRV` records the
# `weight` fields will be honored, but it will only use the lowest `priority`
# field entries in the record.

答案 1 :(得分:0)

我遇到了同样的问题,并通过以下方式解决: 我正在使用以下配置: kong.yml

services:
  - name: userapi-service
    url: http://localhost:8082/userapi
    tags:
    - userapi
  routes:
  - name: userapi-route
    paths:
    - /userapi

访问http://localhost:8001/services

时进入下方
{
"next": null,
"data": [
    {
        "host": "localhost",
        "created_at": 1584710150,
        "connect_timeout": 60000,
        "id": "973c6d1e-b31b-5453-8424-20ad70a0c6c2",
        "protocol": "http",
        "name": "userapi-service",
        "read_timeout": 60000,
        "port": 8082,
        "path": "/userapi",
        "updated_at": 1584710150,
        "client_certificate": null,
        "tags": [
            "userapi"
        ],
        "write_timeout": 60000,
        "retries": 5
    }
  ]
}

以及访问路由时的以下内容:http://localhost:8001/services/userapi-service/routes

{
"next": null,
"data": [
    {
        "id": "3ba059ab-673e-5c4e-9eb6-eb5aa3437476",
        "path_handling": "v0",
        "paths": [
            "/"
        ],
        "destinations": null,
        "headers": null,
        "protocols": [
            "http",
            "https"
        ],
        "created_at": 1584710150,
        "snis": null,
        "service": {
            "id": "973c6d1e-b31b-5453-8424-20ad70a0c6c2"
        },
        "name": "userapi-route",
        "tags": null,
        "preserve_host": false,
        "regex_priority": 0,
        "strip_path": true,
        "sources": null,
        "updated_at": 1584710150,
        "https_redirect_status_code": 426,
        "hosts": [
            "example.com"
        ],
        "methods": null
    }
  ]
}

并使用http://localhost:8000/-标头主机:example.com,并使用我的api调用的相对路径:http://localhost:8000/userapi/xxxxx/xxxx