【问题标题】:APIM Combine throttling policy approachAPIM 组合节流策略方法
【发布时间】:2018-12-09 07:28:31
【问题描述】:

目前在 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>

【问题讨论】:

  • 这真的取决于您的策略是如何配置的。你能把它们贴在这里吗?
  • @VitaliyKurokhtin 我已经用细节更新了问题

标签: azure-api-management api-management


【解决方案1】:

理解 rate-limit-by-key 和 rate-limit 的计数器是独立的,这一点很重要。

当 rate-limit-by-key 允许请求通过时,它的计数器会增加。当速率限制允许请求通过时,它会增加它的计数器s。在您的配置中,当 rate-limit-by-key 限制时,请求 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 级别实现。所以我的要求是 API 级别限制。
  • 此政策仅适用于产品级别,是的。但是上面的配置只会限制某些 API(因为在速率限制级别调用设置为 0)。如果您需要将速率限制策略置于 API 策略级别,则必须使用 rate-limit-by-key。
【解决方案2】:

因此,为了满足我的要求,我提出了速率限制 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>

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    确认一下 - 您正在基于订阅密钥在 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-keyrate-limit)看起来不接受。我已经更新了问题的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2018-12-18
    • 2017-08-28
    • 2022-11-17
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多