【问题标题】:Kong Configuring Multiple ServicesKong 配置多个服务
【发布时间】:2021-05-14 01:54:08
【问题描述】:

我在同一个kong.yaml 中配置多个服务。例如

services:
  - host: service1.com
    name: service1
    port: 8000
    route: ...

  - host: service2.com
    name: service2
    port: 9000
    route: ...

当我向客户提出请求时,例如

curl -X GET -k https://localhost:8443/v1/service2/api -H "apiKey: service2-api-key"

默认情况下一直代理到 service1 并收到以下错误:

2021/05/14 01:46:40 [error] 26#0: *37325 [lua] balancer.lua:1064: execute(): DNS resolution failed: dns server error: 3 name error. Tried: ["(short)service1.com:(na) - cache-miss","service1.com:33 - cache-miss/scheduled/querying/dns server error: 3 name error","service1.com:1 - cache-miss/scheduled/querying/dns server error: 3 name error","service1.com:5 - cache-miss/scheduled/querying/dns server error: 3 name error"], client: 172.18.0.5, server: kong, request: "GET /v1/service2/api HTTP/2.0", host: "localhost:8443"

docs 中提到,您可以将hosts 属性添加到路由对象,并让客户端向标头中的主机发出请求(这有效)。例如

curl -X GET -k https://localhost:8443/v1/service2/api -H "apiKey: service2-api-key" -H "Host: service2.com"

但是,我无法更改客户端发出请求的方式,因为这已经在生产中。有没有一种方法可以代理请求,而无需更改客户端的请求以在标头中包含主机 (Host: <given host>)?

另外,需要注意的是,如果我完全删除 service1,那么它可以工作,它默认为 service2 路由,而无需在请求的标头中包含额外的 Host。

【问题讨论】:

    标签: kong kong-plugin


    【解决方案1】:

    如果我正确理解您希望将 /v1/service1/api 路由到您的 service1 并将 /v1/service2/api 路由到您的 service2。

    并且您需要将 Host 标头设置为 service1 或 service2。 Kong 默认是"preserve_host": true,,所以你应该得到你想要的

    您需要为每个服务设置路由的path 服务1:

      routes:
      - name: my-route1
        paths:
        - /v1/service1/
    

    服务2:

      routes:
      - name: my-route2
        paths:
        - /v1/service2/
    

    然后你可以测试

    curl -i -X GET --header 'kong-debug: 1' http://my-gateway-url.com:8000/v1/service2/api
    
    HTTP/1.1 200 OK
    Content-Type: application/octet-stream
    Content-Length: 4
    Connection: keep-alive
    Kong-Route-Id: df6f59df-a244-4b74-b782-02138d2d474a
    Kong-Route-Name: route2
    Kong-Service-Id: 0f32ae39-d338-4074-a112-c5106c99776d
    Kong-Service-Name: service2
    Date: Tue, 18 May 2021 20:11:13 GMT
    Last-Modified: Tue, 18 May 2021 20:11:09 GMT
    Server: SimpleHTTP/0.6 Python/3.8.0
    
    foo
    

    curl -i -X GET --header 'kong-debug: 1' http://my-gateway-url.com:8000/v1/service1/api
    
    HTTP/1.1 200 OK
    Content-Type: application/octet-stream
    Content-Length: 4
    Connection: keep-alive
    Kong-Route-Id: 92323f17-ac53-4bfb-80af-5264ab389663
    Kong-Route-Name: route1
    Kong-Service-Id: c402fba2-4cbe-47b0-a2fd-c9f3dd9974e1
    Kong-Service-Name: service1
    Date: Tue, 18 May 2021 20:11:20 GMT
    Last-Modified: Tue, 18 May 2021 20:11:09 GMT
    Server: SimpleHTTP/0.6 Python/3.8.0
    
    foo
    

    【讨论】:

    • 仅仅通过设置路径并对每个路径发出请求似乎还不够。如果我不将hosts 属性添加到每个路径,那么Kong 似乎不知道将请求发送到哪个服务。例如当我出于某种原因尝试向/v1/service1/ 发出请求时,它试图将请求代理到server2,这是预期的吗?我确实有preserve_host: true
    • 您能否尝试获取更多详细信息,将'kong-debug: 1' 添加到您的请求中以获取使用了哪些服务和路由?我已经添加了我得到的东西
    猜你喜欢
    • 2019-12-30
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多