如何将请求路由到Kong Api-Gateway上的动态端点
我有一个名为alpha(使用python-django创建的服务),该服务在 http://127.0.0.0.1:9000
上运行,并具有这两个Endpoings
-
/health
返回{“ health”:“ ok”}状态200
-
/codes/< str:code>
返回{“ code':code}状态200
i也 Localhost端口80上运行的无DB声明模式,请在无DB的声明模式下使用Kong Api-Gateway
在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
- 。 0.1/alpha/health 它返回
{“ health”:“ ok”}状态200
,显示kong正在工作。 - 我想发送一个请求,例如
http://127.0.0.0.1/alpha/code/123
,我希望接收{“代码”:123} status 200
但是我不知道如何设置kong.yaml文件来执行此操作。如果我将请求发送到http://127.0.0.1/alpha/code/123
我从(从Alpha Django应用程序中获得404),这意味着Kong正在将请求路由到Alpha服务,但是如果我将请求发送到http://127.0.0.0.1/alpha/code/abc
i get{“ message”:“没有路由与这些值匹配”}
,显示显示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.0.0.1/alpha/code/123
将转到`http://127.0.0.0.1.1:9000/code/123/code/123/code/123/code/123 `但是我无法控制正则
想法如何将请求路由到Kong Api-Gateway上的动态端点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,
http://127.0.0.1/alpha/code/abc
的请求确实将不匹配您添加的规则,因为\ d+
(它匹配的是一个或多个数字)。另外,http://127.0.0.1/alpha/code/123
将以/
的要求到达上游,因为您有strip_path
设置为true。我已经用一些小调整测试了您的示例,以代理本地HTTPBIN服务,该服务具有相似的端点(
/status/&lt; code&gt;
)。启动本地HTTPBIN服务:
使用以下配置启动Kong:
请注意,
strip_path
设置为false
,因此将整个匹配路径定位到上游。测试以下测试:
Note that a request like
http://127.0.0.1/alpha/code/abc
will indeed not match the rule you have added, because of the\d+
(which matches one or more digits). Also,http://127.0.0.1/alpha/code/123
will reach the upstream as a request to/
, since you havestrip_path
set to true.I have tested your example with some minor tweaks to proxy to a local httpbin service, which has a similar endpoint (
/status/<code>
).Start a local httpbin service:
Start Kong with the following config:
Note that
strip_path
is set tofalse
, so the entire matching path is proxied to the upstream.Test it out with: