azure - 如何在 Azure APIM 中构建此 url 重写策略?

标签 azure azure-api-management

在 Azure APIM 中,寻找一个 url 重写策略(我认为),它会查找某个字符串并根据该字符串构造后端 url。

示例1:
转换此
https://myfrontend.com/locations/usa/remaining-url
对此
https://usa.com/remaining-url

示例1:
转换此
https://myfrontend.com/locations/canada/remaining-url
对此
https://canada.com/remaining-url

最佳答案

这取决于您的 API 管理 API/操作。

在我的示例中,我是这样设计的:

'/locations/{locationid}/*':
  get:
    summary: Locations with Id
    operationId: locations-with-id
    parameters:
      - name: locationid
        in: path
        required: true
        schema:
          enum:
            - usa
            - canada
          type: string

有一个匹配参数 locationid 将在策略中使用。
路径wildcard 结尾/* 支持剩余的 URL。

要获取剩余的 URL,请使用以下 Regex使用:
\/位置\/[^\/]*(\/[a-zA-Z0-9-?=#_]*)

具有操作 GET /locations/{locationid}/* 的 API:

enter image description here

运营政策:

<policies>
    <inbound>
        <base />
        <set-variable name="RegexLocation" value="\/locations\/[^\/]*(\/[a-zA-Z0-9-?=#_]*)" />
        <set-variable name="remainingUrl" value="@{
            string pattern = context.Variables.GetValueOrDefault<string>("RegexLocation");

            var regex = new Regex(pattern);
            var match = regex.Match(context.Request.OriginalUrl.Path.ToString());

            if (match.Success)
            {  
              return match.Groups[1].Value;;
            }       
            else
            {
                return string.Empty;
            }

        }" />
        <choose>
            <when condition="@(context.Request.MatchedParameters.GetValueOrDefault("locationid", "") == "usa")">
                <set-backend-service base-url="https://usa.com" />
            </when>
            <when condition="@(context.Request.MatchedParameters.GetValueOrDefault("locationid", "") == "canada")">
                <set-backend-service base-url="https://canada.com" />
            </when>
        </choose>
        <rewrite-uri template="@(context.Variables.GetValueOrDefault<string>("remainingUrl"))" copy-unmatched-params="true" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

choose 策略用于指定后端。
策略rewrite-uri添加重命名路径和查询

请求跟踪的输出:
获取https://rfqapiservicey27itmeb4cf7q.azure-api.net/abc/locations/usa/lorem?ipsum=1

新的后端 URL 是:
https://usa.com/lorem?ipsum=1

{
  "traceEntries": {
    "inbound": [
      {
        "source": "set-variable",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0003989",
        "data": {
          "message": "Context variable was successfully set.",
          "name": "RegexLocation",
          "value": "\\/locations\\/[^\\/]*(\\/[a-zA-Z0-9-?=#_]*)"
        }
      },
      {
        "source": "set-variable",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004445",
        "data": {
          "message": "Expression was successfully evaluated.",
          "expression": "\n            string pattern = context.Variables.GetValueOrDefault<string>(\"RegexLocation\");\n\n            var regex = new Regex(pattern);\n            var match = regex.Match(context.Request.OriginalUrl.Path.ToString());\n\n            if (match.Success)\n            {  \n              return match.Groups[1].Value;;\n            }       \n            else\n            {\n                return string.Empty;\n            }\n\n        ",
          "value": "/lorem"
        }
      },
      {
        "source": "set-variable",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004465",
        "data": {
          "message": "Context variable was successfully set.",
          "name": "remainingUrl",
          "value": "/lorem"
        }
      },
      {
        "source": "choose",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004581",
        "data": {
          "message": "Expression was successfully evaluated.",
          "expression": "context.Request.MatchedParameters.GetValueOrDefault(\"locationid\", \"\") == \"usa\"",
          "value": true
        }
      },
      {
        "source": "set-backend-service",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004690",
        "data": {
          "message": "Backend service URL was changed.",
          "oldBackendServiceUrl": "https://rfqapiservicey27itmeb4cf7q.azure-api.net/abc",
          "newBackendServiceUrl": "https://usa.com/",
          "request": { "url": "https://usa.com/locations/usa/lorem?ipsum=1" }
        }
      },
      {
        "source": "rewrite-uri",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0004757",
        "data": {
          "message": "Expression was successfully evaluated.",
          "expression": "context.Variables.GetValueOrDefault<string>(\"remainingUrl\")",
          "value": "/lorem"
        }
      },
      {
        "source": "rewrite-uri",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0005407",
        "data": {
          "message": "Updated request URL per specified rewrite template.",
          "request": { "url": "https://usa.com/lorem?ipsum=1" }
        }
      }
    ],
    "backend": [
      {
        "source": "forward-request",
        "timestamp": "2022-08-13T07:08:45.5496188Z",
        "elapsed": "00:00:00.0007386",
        "data": {
          "message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
          "request": {
            "method": "GET",
            "url": "https://usa.com/lorem?ipsum=1"
          }
        }
      }
    ]
  }
}

关于azure - 如何在 Azure APIM 中构建此 url 重写策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73341223/

相关文章:

Azure DevOps 动态发布管道创建

azure - 我可以使用管理 azure api 进行 Azure APIM 分析吗

azure-api-management - 具有 key 轮换功能的 Azure API 管理

sql-server - 如何使用 Azure Key Vault 加密 SQL 中的数据列?

HDInsight集群的Linux分布信息

Azure 搜索命中突出显示和匹配分隔符

WSDL 的 Azure APIM 导入

api - Azure API 管理 - 版本控制

azure - 在操作层面停止全局层面的政策执行

azure - 我们可以使用 azure blob 存储进行 scyladb 备份吗?