APIM Combine 节流策略方法

APIM Combine throttling policy approach

在 APIM 目前我们有产品订阅密钥级别限制。但很明显,如果我们在同一产品中有多个 API,一个 API 可能会消耗更多配额 超出预期并阻止其他人使用该应用程序。因此,根据 MS 文档 (https://docs.microsoft.com/en-us/azure/api-management/api-management-sample-flexible-throttling),我们可以使用组合策略。

问题是我们是否可以像下面那样使用这种方法,

    API-1 300 calls per 60 seconds where product subscription key =123
    API-2 200 calls per 60 seconds where product subscription key =123
    API-3 200 calls per 60 seconds where product subscription key =123

如果是这样,产品订阅密钥的调用总数是多少?如果有道理。

我采用以下方法来合并策略。但是不喜欢。

    <rate-limit-by-key calls="50" renewal-period="60" counter-key="@(&quot;somevalue&quot; + context.Request.Headers.GetValueOrDefault(&quot;Ocp-Apim-Subscription-Key&quot;))" />
    <rate-limit calls="10" renewal-period="30">  
        <api name="AddressSearch API dev" calls="5" renewal-period="30" />  
            <operation name="Search_GetAddressSuggestions" calls="3" renewal-period="30" />
    </rate-limit>

确认一下 - 您将根据订阅密钥在 API 级别设置三个限制策略:

API-1: 300 calls per 60 seconds API-2: 200 calls per 60 seconds API-3: 200 calls per 60 seconds

在这种情况下,如果这些是您仅有的 API,则每 60 秒每个订阅密钥的最大请求数为: 300 + 200 + 200 = 700.

如果您有更多 API,除非您也为它们指定策略,否则它们将不会受到限制。

请务必了解 rate-limit-by-key 和 rate-limit 的计数器是独立的。

当按键速率限制允许请求通过时,它会增加它的计数器。当速率限制允许请求通过时,它会增加它的计数器s。在您的配置中,当 rate-limit-by-key throttles request rate-limit 将不会执行并且不会计算请求。

这意味着在大多数情况下下限获胜。您的配置将允许一个订阅每分钟进行 50 次调用,但这不太可能产生任何影响,因为第二次速率限制策略将在对同一产品进行 10 次调用后进行限制,因此第一个将没有机会做任何事情。

如果您想要示例中的限制,您可以使用如下配置:

<rate-limit calls="0" renewal-period="0">  
    <api name="API-1" calls="100" renewal-period="60" />  
    <api name="API-2" calls="200" renewal-period="60" />  
    <api name="API-3" calls="300" renewal-period="60" />  
</rate-limit>

因此,为了让速率限制 API 水平达到以下水平,我提出了满足我的要求的水平。

<choose>
<when condition="@(context.Operation.Id.Equals("End point name1"))">
<rate-limit-by-key calls="40" renewal-period="30" counter-key="@(context.Api.Name + context.Operation.Name + context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key"))" />
</when>
<when condition="@(context.Operation.Id.Equals("End point name2"))">
<rate-limit-by-key calls="20" renewal-period="30" counter-key="@(context.Api.Name + context.Operation.Name + context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key"))" />
</when>
<otherwise>
<rate-limit-by-key calls="15" renewal-period="30" counter-key="@(context.Api.Name + context.Operation.Name + context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key"))" />
</otherwise>
</choose>

希望对您有所帮助。