API管理政策-基于代理标签的后台

问题描述 投票:0回答:1

我需要实施一项策略,根据 URL(机构代码)中的内容将请求转发到正确的后端

例如,

我想使用类似的东西https://github.com/Azure/api-management-policy-snippets/blob/master/examples/Random%20load%20balancer.policy.xml

我不知道

  • 如何从网址中提取#AGENCYCODE#
  • 如何重写网址删除#AGENCYCODE#

谢谢!

azure-api-management
1个回答
0
投票

您可以使用下面给出的策略来提取

AGENCYCODE
并重写 Url。

<policies>
    <inbound>
        <base />
        <set-variable name="agencyCode" value="@(System.Text.RegularExpressions.Regex.Match(context.Request.OriginalUrl.Path, @"(?<=/int/my/v1/)(.*)(?=/system/ping)").Value)" />
        <rewrite-uri template="/int/my/v1/system/ping" />
        <choose>
            <when condition="@((string)context.Variables["agencyCode"] == "AABB1")">
                <set-backend-service base-url="http://10.10.10.1:8080" />
            </when>
            <when condition="@((string)context.Variables["agencyCode"] == "AABB2")">
                <set-backend-service base-url="http://10.10.10.2:8080" />
            </when>
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

追踪-

enter image description here enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.