本文是APIM系列的一部分,完整内容参见:

  1. (视频)Azure API Management(1)创建Azure APIM并添加后端API
  2. (视频)Azure API Management(2)创建 Product/User/Subscription/体验Developer Portal
  3. (视频)Azure API Management(3)体验APIM Policy-流控/隐藏后端api url/修改response header
  4. (视频)Azure API Management(4)体验APIM 版本管理
  5. (视频)Azure API Management(5)缓存
  6. (视频)Azure API Management(6)Validate JWT Token

 

 

 


本文介绍 Azure API Management 中policy使用案例:

1. Inbound Processing Policy-增加API流控 : Limit call rate

2.Outbound Processing Policy- 修改后端API返回值的header:Set headers

3.Outbound Processing Policy-  隐藏后端真实的url :Mask urls in content

 


 

视频演示:

 

 

 

图文演示:


 
1. Inbound Processing Policy-增加API流控 : Limit call rate

 

如果为一组API统一增加Policy,则选中“All Operations”-“Design”-“Add Policy”;

 

增加Policy可以点击如下图按钮3,通过可视化界面添加,也可以通过按钮4进入代码编辑模式添加。

 

 

 

如果为某一个特定的API增加Policy,则选中该API后在执行“Design”-“Add Policy”,如下图针对“GetSpeakers”这个API进行操作。

 

本例子中选择Limit call rate:

 

填写如下信息:表示每个IP地址,每10秒可以调用1次 API

Number of calls:1

Renewal period(in seconds):10

Counter Key:IP Adderss

 

测试API,连续两次点击send 按钮,第二次会出现如下提示:

http/1.1 429 too many requests,同时会有多少秒之后try again的结果。

 


 
2.Outbound Processing Policy- 修改后端API返回值的header:Set headers

本案例将后端服务器返回的header中的x-powered-by隐藏掉,如下图所示:

故而应该选择outbound processing中增加policy。

 

 

选择 Set headers:

 

此处可以修改,增加,删除 header,本例中,我们执行了删除操作。

 

测试结果,返回值中没有了x-powered-by header。

 

在 developer portal中测试,结果的response中同样没有了 x-powered-by header:

 


 
3.Outbound Processing Policy-  隐藏后端真实的url :Mask urls in content

本例中将原始后端API的url替换为 apim的url以达到保护后端真实api的目的,如下图所示,要隐藏掉如下图:“https://conferenceapi.azurewebsites.net:443”

 

 

点击下图所示的编辑代码模式:

 

点击 如下图所示show snippets按钮,则可以显示所有内置的policy:

 

本例中选择“Mask urls in content”:

将自动添加的内容移动到 outbound 节点下,如下图所示:

 

测试结果,发现内容中的原始的“https://conferenceapi.azurewebsites.net:443”均已经被APIM 的url替换掉了。