当前位置: 首页 > 知识库问答 >
问题:

如何在无db和声明性配置中访问Kong服务

郎祯
2023-03-14

我已经用声明式方法配置了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个答案

夏侯承恩
2023-03-14
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.
鄢承运
2023-03-14

我遇到了同样的问题,并以这种方式解决:我使用了以下配置: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-服务/路线

{
"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/-头主机:示例。com,并使用api调用的相对路径:http://localhost:8000/userapi/xxxxx/xxxx

 类似资料:
  • 主要问题--这看起来像是一个基本的flyway问题,我可能(不知为什么)在我的研究中错过了这个问题,但是--当尝试使用flyway迁移数据时,是否可以访问applications services(spring配置的)?下面的一些细节- 其他详细信息- 我知道我们不能注入spring数据服务等(从这个SO问题中了解到)。我从数据访问的角度理解了这一点。 但是在使用flyway时,我们是否可以不使用

  • 问题内容: 我对Eclipse体系结构中的扩展/服务方法有些困惑。开发人员有两种选择: Eclipse插件扩展的使用-http: //www.eclipse.org/articles/Article-Plug-in-architecture/plugin_architecture.html 声明式服务的使用-http: //www.eclipse.org/equinox/bundles/ 什么时候

  • 正如在 声明性映射 ,声明式样式包括生成映射 Table 对象,或容纳 Table 或其他 FromClause 直接对象。 以下示例假定声明性基类为: from sqlalchemy.orm import declarative_base Base = declarative_base() 下面的所有示例都说明了从上面继承的类 Base . 装饰风格 使用修饰符的声明性映射(无声明基) 下面的

  • 我有一个Spring Boot应用程序,我使用Kong作为API网关。 我想用SpringDoc OpenAPI记录我的应用程序的RESTAPI。 当我独立运行Spring Boot应用程序时,一切都在本地运行得很好,但当访问Kong后面的Swagger/OpenAPI UI时,我面临一个问题。 这是我的kong.yml: 假设kong位于8000端口,而我的Spring应用程序位于9000端口。

  • 问题内容: 如何在Python中声明静态属性? 这里写了我如何声明一个方法: Python中的静态方法? 问题答案: 在Python的类级别定义的所有变量均被视为静态 在类中,您可以使用相同的名称拥有两个不同的变量(一个静态变量和一个普通变量)。不要困惑。

  • 我正在检查使用JDT的方法的调用,并对其参数执行检查。我正在使用一个 AST 访问者类,该类访问方法调用节点并执行此操作。我在访客类中使用以下方法。 但是作为变量声明一部分的调用没有被访问。 例如:如果我正在寻找方法“createQuery”的调用,将访问以下调用。 但是这个没有被访问。 我怎样才能访问这样的语句并以正确的方式获取参数? 请帮忙。