使用 Azure Api 管理作为直通
Use Azure Api Management as a passthrough
我想在 Azure API 管理中创建一个策略,将所有以路径 "proxy/search" 开头的调用转发到另一个 url。但是,我不想在 APIM 中为每一种可能性都使用 import/create 端点,因为这使它成为维护的噩梦。例如..
- 获取https://whatever.azure-api.net/proxy/search?q=dogs
- 获取https://whatever.azure-api.net/proxy/search/categories?q=dogs
- 获取https://whatever.azure-api.net/proxy/search/categories/x/y/z/etc....?q=blah
对应...
- 获取https://mysearchapi.com/?q=dogs
- 获取https://mysearchapi.com/categories?q=dogs
- 获取https://mysearchapi.com/categories/x/y/z/etc....?q=blah
我已经建立了下面的策略,但看起来 APIM 想要从它映射到后端的确切路由。我不想这样做,因为这个代理可能会转发到很多很多路由 api 等...
<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>
你让你的生活比需要的更艰难。只需创建一个使用 /proxy/*
作为模板的操作,它将匹配您识别的所有 URL。
然后只需为执行 set-backend-service 的操作创建一个策略。
添加到 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>
我想在 Azure API 管理中创建一个策略,将所有以路径 "proxy/search" 开头的调用转发到另一个 url。但是,我不想在 APIM 中为每一种可能性都使用 import/create 端点,因为这使它成为维护的噩梦。例如..
- 获取https://whatever.azure-api.net/proxy/search?q=dogs
- 获取https://whatever.azure-api.net/proxy/search/categories?q=dogs
- 获取https://whatever.azure-api.net/proxy/search/categories/x/y/z/etc....?q=blah
对应...
- 获取https://mysearchapi.com/?q=dogs
- 获取https://mysearchapi.com/categories?q=dogs
- 获取https://mysearchapi.com/categories/x/y/z/etc....?q=blah
我已经建立了下面的策略,但看起来 APIM 想要从它映射到后端的确切路由。我不想这样做,因为这个代理可能会转发到很多很多路由 api 等...
<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>
你让你的生活比需要的更艰难。只需创建一个使用 /proxy/*
作为模板的操作,它将匹配您识别的所有 URL。
然后只需为执行 set-backend-service 的操作创建一个策略。
添加到 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>