使用Azure Api管理作为直通

时间:2017-01-25 22:02:09

标签: azure azure-api-management

我想在Azure API Management中创建一个策略,该策略转发以路径" proxy / search"开头的所有呼叫。到另一个网址。但是,我不想在APIM中导入/创建端点,因为这会使其成为维护的噩梦。例如..

到相应的......

我已经制定了以下政策,但看起来APIM希望确切的路线从它映射到后端。我不想这样做,因为这个代理可能会转发到很多很多路线apis等...

<policies>
    <inbound>
        <base />
        <set-variable name="baseUrlSearch" value="https://mysearchapi.com/" />
        <set-variable name="matchSearch" value="proxy/search" />
        <set-variable name="isRoutingComplete" value="false" />
        <set-variable name="apiVersionDefaultSearch" value="1.0" />
        <choose>
            <when condition="@{return context.Request.Url.Path.Contains(context.Variables.GetValueOrDefault<string>("matchSearch"));}">
                <set-backend-service base-url="@(context.Variables.GetValueOrDefault<string>("baseUrlSearch"))" />
                <rewrite-uri template="@(context.Request.Url.Path.Replace(context.Variables.GetValueOrDefault<string>("matchSearch"), ""))" />
                <set-header name="Api-Version" exists-action="skip">
                    <value>@(context.Variables.GetValueOrDefault<string>("apiVersionDefaultSearch"))</value>
                </set-header>
                <set-variable name="isRoutingComplete" value="true" />
            </when>
            <when condition="@(!context.Variables.GetValueOrDefault<bool>("isRoutingComplete"))">
                <return-response>
                    <set-status code="400" reason="Bad Request Through Proxy" />
                </return-response>
            </when>
        </choose>
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>

2 个答案:

答案 0 :(得分:5)

你正在使你的生活变得更加艰难。只需创建一个使用/proxy/*作为模板的操作,它就会匹配您标识的所有网址。

然后只为该操作创建一个策略set-backend-service。

答案 1 :(得分:5)

加入Darrel Miller的回答,这就是我如何运作......

添加操作......

{
  "name": "Search_GET",
  "method": "GET",
  "urlTemplate": "/search/*",
  "policies": null
}       

为该操作添加政策......

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="https://mysearchapi.com/" />
        <rewrite-uri template="@(context.Request.Url.Path.Replace("search/", ""))" />
        <set-header name="Api-Version" exists-action="skip">
            <value>1.0</value>
        </set-header>
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>
相关问题