如何配置 krakend.json 以转发参数化 url



基本上我只想转发这个请求:

http://somehost:4321/api/v1/{uid}/profile

进入这个:

http://123.45.67.89:4321/api/{uid}/profile

我已经在krakend.json中完成了此操作:

{
      "version": 2,
      "timeout": "3000ms",
      "cache_ttl": "300s",
      "name": "myapi",
      "output_encoding": "json",
      "port": 4321,
      "endpoints": [
          {
              "endpoint": "/api/v1/{uid}/profile",
              "method": "GET",
              "headers_to_pass": [ "*" ],
              "querystring_params": [ "*" ],
              "output_encoding": "no-op",
              "concurrent_calls": 1,
              "backend": [
                  {
                      "url_pattern": "/api/{uid}/profile",
                      "encoding": "no-op",
                      "host": [
                          "http://123.45.67.89:4321"
                      ]
                  }
              ]
          }
      ]
}

但最终出现错误:

panic:通配符路由":uid

"与路径"/api/v1/:uid/profile"中的现有子路由冲突

有什么线索吗?

事实证明,我已经声明了与端点相同的模式:

{
      "version": 2,
      "timeout": "3000ms",
      "cache_ttl": "300s",
      "name": "myapi",
      "output_encoding": "json",
      "port": 4321,
      "endpoints": [
          {
              "endpoint": "/api/v1/me/profile",
              "method": "GET",
              "headers_to_pass": [ "*" ],
              "output_encoding": "no-op",
              "concurrent_calls": 1,
              "backend": [
                  {
                      "url_pattern": "/api/me/profile",
                      "encoding": "no-op",
                      "host": [
                          "http://123.45.67.89:4321"
                      ]
                 }
             ]
          },
          {
              "endpoint": "/api/v1/{uid}/profile",
              "method": "GET",
              "headers_to_pass": [ "*" ],
              "querystring_params": [ "*" ],
              "output_encoding": "no-op",
              "concurrent_calls": 1,
              "backend": [
                  {
                      "url_pattern": "/api/{uid}/profile",
                      "encoding": "no-op",
                      "host": [
                          "http://123.45.67.89:4321"
                      ]
                  }
              ]
          }
      ]
}

改成这样后,效果很好:

{
      "version": 2,
      "timeout": "3000ms",
      "cache_ttl": "300s",
      "name": "myapi",
      "output_encoding": "json",
      "port": 4321,
      "endpoints": [
          {
              "endpoint": "/api/v1/me/profile",
              "method": "GET",
              "headers_to_pass": [ "*" ],
              "output_encoding": "no-op",
              "concurrent_calls": 1,
              "backend": [
                  {
                      "url_pattern": "/api/me/profile",
                      "encoding": "no-op",
                      "host": [
                          "http://123.45.67.89:4321"
                      ]
                 }
             ]
          },
          {
              "endpoint": "/api/v1/user/{uid}/profile",
              "method": "GET",
              "headers_to_pass": [ "*" ],
              "querystring_params": [ "*" ],
              "output_encoding": "no-op",
              "concurrent_calls": 1,
              "backend": [
                  {
                      "url_pattern": "/api/user/{uid}/profile",
                      "encoding": "no-op",
                      "host": [
                          "http://123.45.67.89:4321"
                      ]
                  }
              ]
          }
      ]
}

问候。

最新更新