【Azure 应用服务】在Azure App Service多实例的情况下,如何在应用中通过代码获取到实例名(Instance ID)呢?
问题描述
App Service开启多实例后,如何在代码中获取当前请求所真实到达的实例ID(Instance ID)呢?
问题答案
App Service 通过 环境变量的方式 显示的输出实例ID等信息,如:
Website Environment Variables
WEBSITE_SITE_NAME
- The name of the site.WEBSITE_SKU
- The sku of the site (Possible values:Free
,Shared
,Basic
,Standard
).WEBSITE_COMPUTE_MODE
- Specifies whether website is on a dedicated or shared VM/s (Possible values:Shared
,Dedicated
).WEBSITE_HOSTNAME
- The Azure Website's primary host name for the site (For example:site.azurewebsites.net
). Note that custom hostnames are not accounted for here.WEBSITE_INSTANCE_ID
- The id representing the VM that the site is running on (If site runs on multiple instances, each instance will have a different id).WEBSITE_NODE_DEFAULT_VERSION
- The default node version this website is using.WEBSOCKET_CONCURRENT_REQUEST_LIMIT
- The limit for websocket's concurrent requests.WEBSITE_COUNTERS_ALL
- (Windows only) all perf counters (ASPNET, APP and CLR) in JSON format. You can access specific one such as ASPNET byWEBSITE_COUNTERS_ASPNET
.
可以通过App Service的高级管理工具(Kudu站点)来查看这些信息:
所以如果要在代码中获取Instance ID信息,可以直接通过 var instanceID = Configuration["WEBSITE_INSTANCE_ID"] 获取。全部示例代码为:
Index.cshtml.cs :
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace MyFirstAzureWebApp.Pages; public class IndexModel : PageModel { private readonly IConfiguration Configuration; private readonly ILogger<IndexModel> _logger; public IndexModel(ILogger<IndexModel> logger, IConfiguration configuration) { _logger = logger; Configuration = configuration; } public void OnGet() { var instanceID = Configuration["WEBSITE_INSTANCE_ID"]; ViewData["InstanceID"] = instanceID; //return Content($"Instance ID value: {instanceID} \n"); } }
如要快速创建一个ASP.NET应用来验证Instance ID,可以参考文档:https://docs.azure.cn/zh-cn/app-service/quickstart-dotnetcore?tabs=net60#create-an-aspnet-web-app
Index.cshtml 文件内容为:
@page @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="text-center"> <h1 class="display-4">Welcome</h1> <div> The current Instance ID is:@ViewData["InstanceID"]; </div> <hr /> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div>
发布到App Service后,就可以检验效果:
此外,如果App Service开启了ARR Affinity(请求粘连性)功能后,在客户端的Cookie值ARRAffinity和ARRAffinitySameSite中,也是可以找到Instance ID信息。如:
附录一:在Function App中可以通过 Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"); 来获取实例ID, App Service中也一样
using System; using System.Configuration; using System.Threading; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; namespace FunctionAppforTime { public class Function5 { [FunctionName("Function5")] public void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log) { var instanceid = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"); log.LogInformation($"function host in: {instanceid}"); log.LogInformation($"function host in: {instanceid} and sleep 100s"); Thread.Sleep(100000); log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); } } }
参考资料
Azure runtime environment:https://github.com/projectkudu/kudu/wiki/Azure-runtime-environment#website-environment-variables
快速入门:部署 ASP.NET Web 应用:https://docs.azure.cn/zh-cn/app-service/quickstart-dotnetcore?tabs=net60#create-an-aspnet-web-app
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?