Skip to content

【Azure 应用服务】App Service多个部署槽(Slot)之间,设置Traffic百分比后,如何来判断请求是由那一个槽(Slot)来进行处理呢?

LuBu0505 edited this page Sep 27, 2021 · 1 revision

问题描述

当我们部署应用到App Service后,为了实现对生成的最小影响,通常是把新版本部署在一个预生产的槽中,然后进行验证。另一方面,为了进行A/B验证,需要把生成槽的流量,切入一部分到预生产槽中. 这个时候,我们就可以通过 路由流量(Traffic %) 来实现这个目的。  No alt text provided for this image

只是问题是:如果无法从访问的页面显示中判断出是生成槽在提供服务还是预生产槽在提供服务,**是否有其他的办法来进行确认呢? **

问题分析

浏览器访问应用时,会自动路由到特定槽后,在该客户端会话生存期内都将“固定”到该槽。 在客户端浏览器上,可以通过查看 HTTP 标头中的 x-ms-routing-name cookie 来查看会话固定到哪个槽。 路由到“暂存”槽的请求具有 cookie x-ms-routing-name=staging。 路由到生产槽的请求具有 cookie x-ms-routing-name=self。

使用 curl -v 能非常方便的查看到 x-ms-routing-name 的值: No alt text provided for this image

浏览器的开发者模式中,查看Request Cookie值: No alt text provided for this image

附录一:附上修改Routing rule的REST API 方法:

使用PostMan发送 PUT 请求到: 

https://management.chinacloudapi.cn/subscriptions//resourceGroups//providers/Microsoft.Web/sites//config/web?api-version=2018-11-01

如果是需要请求之前所有Traffic设置,就在Body中设置如下属性:

{
   "properties": {
       "routingRules": [],
       "experiments": {
           "rampUpRules": []
       }
   }
}

如果是需要对Traffic rule进行设置,可以借鉴:

{
   "properties": {
   	"routingRules": [
   		{
   			"actionHostName": "<your site name>-slot1.chinacloudsites.cn",
   			"reroutePercentage": 50.0,
   			"changeStep": null,
   			"changeIntervalInMinutes": null,
   			"minReroutePercentage": null,
   			"maxReroutePercentage": null,
   			"changeDecisionCallbackUrl": null,
   			"name": "slot1"
   		},
   		{
   			"actionHostName": "<your site name>-slot2.chinacloudsites.cn",
   			"reroutePercentage": 50.0,
   			"changeStep": null,
   			"changeIntervalInMinutes": null,
   			"minReroutePercentage": null,
   			"maxReroutePercentage": null,
   			"changeDecisionCallbackUrl": null,
   			"name": "slot2"
   		}
   	],
   	"experiments": {
   		"rampUpRules": [
   			{
   				"actionHostName": "<your site name>-slot1.chinacloudsites.cn",
   				"reroutePercentage": 50.0,
   				"changeStep": null,
   				"changeIntervalInMinutes": null,
   				"minReroutePercentage": null,
   				"maxReroutePercentage": null,
   				"changeDecisionCallbackUrl": null,
   				"name": "slot1"
   			},
   			{
   				"actionHostName": "<your site name>-slot2.chinacloudsites.cn",
   				"reroutePercentage": 50.0,
   				"changeStep": null,
   				"changeIntervalInMinutes": null,
   				"minReroutePercentage": null,
   				"maxReroutePercentage": null,
   				"changeDecisionCallbackUrl": null,
   				"name": "slot2"
   			}
   		]
   	}
   }
}

**注意:**在发送请求时候,需要携带Authorization Token。(最开的获取方式为Azure 门户,通过F12查看发送请求中的Authorization,复制出来用在Postman中) No alt text provided for this image

参考资料

路由流量: https://docs.azure.cn/zh-cn/app-service/deploy-staging-slots#route-traffic

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

分类: 【Azure 应用服务】

标签: 部署槽流量分布App Service

Clone this wiki locally