【Azure API 管理】APIM如何实现对部分固定IP进行访问次数限制呢?如60秒10次请求

问题描述

使用Azure API Management, 想对一些固定的IP地址进行访问次数的限制,如被限制的IP地址一分钟可以访问10次,而不被限制的IP地址则可以无限访问?

ChatGPT 解答

最近ChatGPT爆火,所以也把这个问题让ChatGPT来解答,然后人工验证它的回答正确与否?

根据对APIM Policy的文档参考, choose 和 rate-limit 策略组合理论上的确可以实现要求, 接下来就让我们实际验证:

  • choose策略:Azure API 管理策略参考 | Azure Docs ,choose 策略根据布尔表达式的求值结果应用括住的策略语句,类似于编程语言中的 if-then-else 或开关构造。
  • rate-limit策略:Azure API 管理策略参考 | Azure Docs , rate-limit 策略可以对调用速率进行限制,使每个指定时段的调用不超出指定的数目,避免单个订阅的 API 使用量暴增。 超过调用速率时,调用方会收到 429 Too Many Requests 响应状态代码。

验证步骤

1)在API的Inbound 策略中添加 choose策略

(策略具体内容,见文末)

2) 测试验证,连续对该API访问10次以上,得到429 Too Many Requests错误

3)以上证明,ChatGPT针对这个问题的解答是正确的!

工程师解答

在参考ChatGPT给出的 choose + rate limit 组合后,我们也发现另一个选项。使用 rate-limit-by-key 策略实现对特定IP的速率限制。

  • rate-limit-by-key 策略:Azure API 管理策略参考 | Azure Docs , 可以对调用速率进行限制,使指定时段的调用不超出指定的数目,避免单个密钥的 API 使用量暴增。 密钥的值可以是任意字符串,通常使用策略表达式来提供密钥。 可以添加可选增量条件,指定在决定是否到达限制值时应该进行计数的请求。 超过此调用速率时,调用方会收到 429 Too Many Requests 响应状态代码。

在官方文档中给出的示例中,是针对所有的IP(context.Request.IpAddress)都进行了10次/60秒请求的限制,而本示例中则特指“某些固定IP”限制。那么如何来完成这个需求呢?

答案 就在“rate-limit-by-key 策略”的说明中,”可以添加可选增量条件,指定在决定是否到达限制值时应该进行计数的请求”, 所以,只要可选增量条件(increment-condition) 的值根据输入的IP地址动态赋值True/False, 就能完美匹配以上要求。

理论推断,只需要实现如下逻辑,即可以实现终极需求“想对一些固定的IP地址进行访问次数的限制,如被限制的IP地址一分钟可以访问10次,而不被限制的IP地址则可以无限访问?

只需两步:

1)通过设置一个变量(set-variable) 值,用C#代码来计算变量值,在赋值语句中,预先定义一个IP限制列表,通过 contains 检查当前请求IP是否在列表中,返回True or False 。True表示当前请求的IP需要速率限制, 否则,不需要。

2) 然后,在rate-limit-by-key 的 increment-condition条件中使用上一步参数值,进行判断是否计入限制

验证步骤

1)在API的 Inbound 策略中添加 rate-limit-by-key策略

(策略具体内容,见文末)

2)验证在30秒,访问5次以上后,同样得到429 Too Many Requests错误

 

3) 当在请求Headers中添加Ocp-Apim-Trace: true 和 Ocp-Apim-Subscription-Key: {订阅Key}后,可以查看请求在APIM中执行的日志跟踪。可以查看rate-limit-by-key 策略的执行情况.

 

总结

想实现固定IP地址访问次数的限制,至少有如下两种解决方案。

方案一:Choose + rate-limit 策略组合

复制代码

<!--IMPORTANT:- Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.- To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the <inbound> section element.- To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the <outbound> section element.- To add a policy, place the cursor at the desired insertion point and select a policy from the sidebar.- To remove a policy, delete the corresponding policy statement from the policy document.- Position the <base> element within a section element to inherit all policies from the corresponding section element in the enclosing scope.- Remove the <base> element to prevent inheriting policies from the corresponding section element in the enclosing scope.- Policies are applied in the order of their appearance, from the top down.- Comments within policy elements are not supported and may disappear. Place your comments between policy elements or at a higher level scope.
-->
<policies><inbound><base /><set-variable name="IsCountIpLimit" value="@{string ipAddress =context.Request.IpAddress; List<string> cidrList = new List<string>(){"167.xxx. xxx.135","167.xxx. xxx.136","167.xxx. xxx.137"};return cidrList.Contains(ipAddress);}" /><choose><when condition="@((bool)context.Variables["IsCountIpLimit"])"><rate-limit calls="10" renewal-period="60" /></when></choose></inbound><backend><base /></backend><outbound><base /></outbound><on-error><base /></on-error>
</policies>

复制代码

方案二:rate-limit-by-key策略

复制代码

<!--IMPORTANT:- Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.- To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the <inbound> section element.- To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the <outbound> section element.- To add a policy, place the cursor at the desired insertion point and select a policy from the sidebar.- To remove a policy, delete the corresponding policy statement from the policy document.- Position the <base> element within a section element to inherit all policies from the corresponding section element in the enclosing scope.- Remove the <base> element to prevent inheriting policies from the corresponding section element in the enclosing scope.- Policies are applied in the order of their appearance, from the top down.- Comments within policy elements are not supported and may disappear. Place your comments between policy elements or at a higher level scope.
-->
<policies><inbound><base /><set-variable name="IsCountIpLimit" value="@{string ipAddress =context.Request.IpAddress; List<string> limitIPs = new List<string>(){"167.xxx. xxx.135","167.xxx. xxx.136","167.xxx. xxx.137"};return limitIPs.Contains(ipAddress);}" /><rate-limit-by-key calls="5" renewal-period="30" counter-key="@(context.Request.IpAddress)" increment-condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 300 && (bool)context.Variables["IsCountIpLimit"])" /></inbound><backend><base /></backend><outbound><base /></outbound> <on-error><base /></on-error>
</policies>

复制代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/79827.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

openGauss学习笔记-48 openGauss 高级数据管理-函数

文章目录 openGauss学习笔记-48 openGauss 高级数据管理-函数48.1 数学函数48.2 三角函数列表48.3 字符串函数和操作符48.4 类型转换相关函数 openGauss学习笔记-48 openGauss 高级数据管理-函数 openGauss常用的函数如下&#xff1a; 48.1 数学函数 abs(x) 描述&#xff1a;…

MATLAB R2023a for Mac Update_5

MATLAB是一种高级的计算机编程环境和开发工具&#xff0c;主要用于数值计算、数据分析、算法开发和可视化。它由MathWorks公司开发&#xff0c;被广泛应用于科学研究、工程设计、数据分析和教育等领域。 MATLAB提供了丰富的数学和工程函数库&#xff0c;可以进行矩阵运算、信号…

什么是卷积神经网络

目录 什么是卷积神经网络 全链接相对笨重&#xff1a;大胖子​编辑 ​编辑 参数众多&#xff1a;容易造成过拟合 ​编辑 卷积核&#xff1a;进行图像特征提取&#xff0c;源于卷积原理&#xff1a;求相交面积 卷积的作用 卷积的意义 ​编辑 通过卷积核减少参数 深度卷积…

Spring练习31---用户添加操作--添加数据到数据库,最终添加讲师,助教操作最终完毕

视频链接&#xff1a;16-Spring练习-用户添加操作-添加数据到数据库_哔哩哔哩_bilibili 104 1、当你点击提交时&#xff0c;form表单进行提交 2、这个名字必须跟你当前封装的属性名一样 3、要封装跟user的属性名一样&#xff0c;这样springMVc才能自动封装进去 4、提交时选择r…

python 面向对象的属性,类方法,静态方法,实例方法的区别及用法详解

一. 前言 在Python的面向对象编程中&#xff0c;类属性和实例属性是两个不同的概念&#xff0c;它们在作用域和使用方式上有所区别。Python中的面向对象编程中有三种方法&#xff1a;实例方法、类方法和静态方法&#xff0c;它们之间的差异主要体现在参数传递和调用方式上。 …

python 基础篇 day 3 运算符大全

文章目录 什么是运算符算术运算符种类举例注意运算顺序&#xff1a;整数除法和浮点数除法取模运算幂运算字符串拼接注意整数与浮点数之间的运算注意溢出问题 赋值运算符种类举例注意事项赋值顺序多重赋值增量赋值运算符赋值运算符链式操作注意可变对象的赋值注意不可变对象的赋…

【Unity3D赛车游戏】【二】如何制作一个真实模拟的汽车

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;Uni…

AWS 提示证书签名过期无法自动更新

如果域名没有通过验证的话&#xff0c;证书的过去是没有办法自动更新的。 验证的方式也非常简单&#xff0c;通过下面的配置&#xff0c;把 CNAME添加到你的域名上面&#xff0c;AWS 就可会自动完成验证了。 当添加完成后&#xff0c;AWS 验证需要的时间大致在 30 分钟到 1 个…

androidstudio Please specify a signing configuration for this variant (release)

当直接运行release版本时&#xff0c;报错Error: The apk for your currently selected variant cannot be signed. Please specify a signing configuration for this variant (package64-release). 解决报错&#xff1a;添加签名&#xff0c;signingConfigs 写在buildTypes前…

static相关知识点详解

文章目录 一. 修饰成员变量二. 修饰成员方法三. 修饰代码块四. 修饰类 一. 修饰成员变量 static 修饰的成员变量&#xff0c;称为静态成员变量&#xff0c;该变量不属于某个具体的对象&#xff0c;是所有对象所共享的。 public class Student {private String name;private sta…

F5负载均衡器参与的Kubernetes架构选项介绍

F5负载均衡器在业内有着很高的知名度&#xff0c;因为它不仅是F5的代表作&#xff0c;负载均衡&#xff08;Load Balance&#xff09;这一词汇正是由F5发明并引入国内的。当前&#xff0c;F5的能力不断拓展&#xff0c;从早期聚焦F5负载均衡器到现在的分布式云应用架构&#xf…

如何在网页下载腾讯视频为本地MP4格式

1.打开腾讯视频官网地址 腾讯视频 2.搜索你想要下载的视频 3. 点击分享,选择复制通用代码 <iframe frameborder="0" src="ht