使用Azure Api管理作为传递 [英] Use Azure Api Management as a passthrough
问题描述
我想在Azure API管理中创建一个策略,以将所有以代理/搜索"路径开头的调用转发到另一个URL.但是,我不想为每种可能性都在APIM中导入/创建端点,因为这使它成为维护的噩梦.例如.
I would like to create a policy in Azure API Management that forwards all calls that start with the path "proxy/search" to another url. However, i don't want to have to import/create endpoints in APIM for every possibility since this makes it a maintenance nightmare. For example..
- 获取 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
- GET https://whatever.azure-api.net/proxy/search?q=dogs
- GET https://whatever.azure-api.net/proxy/search/categories?q=dogs
- GET 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
- GET https://mysearchapi.com/?q=dogs
- GET https://mysearchapi.com/categories?q=dogs
- GET https://mysearchapi.com/categories/x/y/z/etc....?q=blah
我已经在下面构建了策略,但APIM似乎希望从其映射到后端的确切路由.我不想这样做,因为此代理可能正在转发到许多路由api等...
I've built the policy below but it looks like APIM wants exact routes to map from it to the backend. I don't want to do this because this proxy may be forwarding to many, many routes apis etc...
<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匹配.
You are making your life much harder than it needs to be. Simply create an operation that uses /proxy/*
as the template and it will match to all the URLs you identified.
然后只需为执行set-backend-service的操作创建一个策略.
Then just create a policy for that operation that does set-backend-service.
这篇关于使用Azure Api管理作为传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!