如何将请求路由到Kong Api-Gateway上的动态端点

发布于 2025-02-07 06:38:02 字数 1955 浏览 2 评论 0 原文

我有一个名为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上的动态端点?

此内容似乎相关,但无法弄清楚它如何设置它

I have a service named alpha (created using python-django) that runs on http://127.0.0.1:9000 and has these two endpoings

  • /health returns {"health": "OK"} status 200
  • /codes/<str:code> returns {"code": code} status 200

I also have a kong api-gateway in the db-less declarative mode that runs on localhost port 80

in kong.yaml I have two services

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
  • If I send a GET request to http://127.0.0.1/alpha/health it returns {"health": "OK"} status 200 which shows kong is working.
  • I want to send a request such as http://127.0.0.1/alpha/code/123 and I expect to receive {"code": 123} status 200 but I don't know how to setup kong.yaml file to do this. If I send a request to http://127.0.0.1/alpha/code/123 I get 404 from (from the alpha django application) which means kong is routing the request to alpha service but if I send a request to http://127.0.0.1/alpha/code/abc I get {"message": "no Route matched with those values"} which shows the regex is working

I could do this

services:
  - name: local-alpha-health
    url: http://host.docker.internal:9000/
    routes:
      - name: local-alpha-health
        methods:
          - GET
        paths:
          - /alpha
        strip_path: true

Then a request sent to http://127.0.0.1/alpha/code/123 would go to ``http://127.0.0.1:9000/code/123` but I cannot control with regex

Any idea How to route requests to a dynamic endpoint on kong api-gateway?

This content seems related but cannot figure it out how to set it up
https://docs.konghq.com/gateway-oss/2.5.x/proxy/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2025-02-14 06:38:02

请注意, 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服务:

$ 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

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 have strip_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:

$ docker run --rm -d -p "8080:80" kennethreitz/httpbin

Start Kong with the following config:

_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

Note that strip_path is set to false, so the entire matching path is proxied to the upstream.

Test it out with:

$ http :8000/status/200                                       
HTTP/1.1 200 OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文