0

我在同一个配置多个服务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"

从它提到的文档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。

4

1 回答 1

0

如果我正确理解你想路由/v1/service1/api到你的 service1 和/v1/service2/api你的 service2。

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

您需要path为每个服务 service1 设置您的路线:

  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
于 2021-05-15T22:11:16.973 回答