如何配置krakend.json转发参数化的URL?

时间:2019-06-09 06:01:37

标签: go api-gateway

基本上我只想转发此请求:

  

http://somehost:4321/api/v1/ {uid} /个人资料

对此:

  

http://123.45.67.89:4321/api/ {uid} /个人资料

我已经在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"
                      ]
                  }
              ]
          }
      ]
}

但最终出现错误:

  

恐慌:通配符路由':uid'与路径'/ api / v1 /:uid / profile'中的现有子节点冲突

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

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

{
      "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"
                      ]
                  }
              ]
          }
      ]
}

致谢。