我有一个名为alpha的服务(使用python创建),它运行在http://127.0.0.1:9000
上,并具有这两个端点。
/health
返回{"health": "OK"} status 200
/codes/<str:code>
返回{"code": code} status 200
我还有一个在本地主机端口80上运行的无db声明模式的kong api网关。
在kong.yaml中,我有两个服务
services:
- name: local-alpha-health
url: http://host.docker.internal:9000/health
routes:
- name: local-alpha-health
methods:
- GET
paths:
- /alpha/health
strip_path: true
- name: local-alpha-code
url: http://host.docker.internal:9000/code/ # HOW TO WRITE THIS PART???
routes:
- name: local-alpha-code
methods:
- GET
paths:
- /alpha/code/(?<appcode>\d+) # Is this right???
strip_path: true
http://127.0.0.1/alpha/health
发送GET
请求,它会返回{"health": "OK"} status 200
,这表明http://127.0.0.1/alpha/health
在工作。http://127.0.0.1/alpha/code/123
,我希望收到{"code": 123} status 200
,但是我不知道如何设置kong.yaml文件来完成这个任务。如果我向http://127.0.0.1/alpha/code/123
发送请求,我从(从alpha django应用程序)获得404,这意味着kong正在将请求路由到alpha服务,但是如果我向http://127.0.0.1/alpha/code/abc
发送请求,则得到{"message": "no Route matched with those values"}
,这表明regex正在运行。
我能做到的
services:
- name: local-alpha-health
url: http://host.docker.internal:9000/
routes:
- name: local-alpha-health
methods:
- GET
paths:
- /alpha
strip_path: true
然后发送给http://127.0.0.1/alpha/code/123
的请求将转到http://127.0.0.1:9000/code/123`,但我无法用regex控制
如何将请求路由到江港api-网关上的动态端点?
此内容似乎相关,但无法解决如何设置https://docs.konghq.com/gateway-oss/2.5.x/proxy/。
发布于 2022-06-15 13:34:09
注意,像http://127.0.0.1/alpha/code/abc
这样的请求实际上与您添加的规则不匹配,因为\d+
(它匹配一个或多个数字)。此外,http://127.0.0.1/alpha/code/123
将作为对/
的请求到达上游,因为strip_path
设置为true。
我已经通过一些小的调整来测试您的示例,以代理到具有类似端点(/status/<code>
)的本地httpbin服务。
启动本地httpbin服务:
$ docker run --rm -d -p "8080:80" kennethreitz/httpbin
使用以下配置启动Kong:
_format_version: "2.1"
services:
- name: local-alpha-code
url: http://localhost:8080
routes:
- name: local-mockbin-status
methods:
- GET
paths:
- /status/(?<appcode>\d+)
strip_path: false
请注意,strip_path
设置为false
,因此整个匹配路径被代理到上游。
用以下方法进行测试:
$ http :8000/status/200
HTTP/1.1 200 OK
https://stackoverflow.com/questions/72618713
复制相似问题