【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
【Azure 应用服务】在Azure App Service多实例的情况下,如何在应用中通过代码获取到实例名(Instance ID)呢?的更多相关文章
- 【Azure 应用服务】一个 App Service 同时部署运行两个及多个 Java 应用程序(Jar包)
问题描述 如何在一个AppService下同时部署运行多个Java 应用程序呢? 问题解答 因为App Service的默认根目录为 wwwroot.如果需要运行多个Java 应用程序,需要在 www ...
- 【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
问题描述 在App Service for Windows的环境中,当前只提供了PHP 7.4 版本的选择情况下,如何实现自定义PHP Runtime的版本呢? 如 PHP Version 8.1.9 ...
- 【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
问题描述 在上篇博文"[Azure 应用服务]App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)"中,实现了通过 HT ...
- 【Azure 应用服务】Azure App Service 自带 FTP服务
问题描述 Azure PaaS服务是否有FTP/S服务呢? 回答问题 应用服务(Web App/App Service)在创建时候,默认创建了FTP服务并自动开启,用于应用部署.但它不是适合作为FTP ...
- 【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found -- The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
问题描述 使用NodeJS的后端应用,开发一个Mobile App的服务端,手机端通过REST API来访问获取后端数据.在本地编译好后,通过npm start启动项目,访问效果如下: 但是,当把项目 ...
- 【Azure 应用服务】Azure Web App的服务(基于Windows 操作系统部署)在被安全漏洞扫描时发现了TCP timestamps漏洞
问题背景 什么是TCP timestamps(TCP 时间戳)? The remote host implements TCP Timestamps, as defined by RFC1323 (h ...
- 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误
问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...
- 【Azure 应用服务】Azure Function HTTP 触发后, 230秒就超时。而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间?
问题描述 Azure Function HTTP 触发后, 230秒就超时,而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间? 问题分析 查阅官方文档,对函数应用超时持续时间有 ...
- 【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed
问题描述 编写Powershell Function,登录到China Azure并获取Azure AD User信息,但是发现遇见了 [Error] ERROR: ManagedIdentityCr ...
- 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题
一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...
随机推荐
- Harbor 简要安装说明
Harbor 简要安装说明 下载最新的离线安装文件 链接:https://pan.baidu.com/s/1ZEjgnI3YmhsdVOm7h7SWcQ 提取码:GSNB 复制这段内容后打开百度网盘手 ...
- SignalR系列文章02---netCoreMvc创建Demo
1. 新建.net core MVC项目,并引入nuget包 2. 添加客户端库 3. 修改startUp.cs文件,增加services.AddSignalR();和endpoints.Map ...
- LyScript 实现绕过反调试保护
LyScript插件中内置的方法可实现各类反调试以及屏蔽特定API函数的功能,这类功能在应对病毒等恶意程序时非常有效,例如当程序调用特定API函数时我们可以将其拦截,从而实现保护系统在调试时不被破坏的 ...
- 【链表】链表的合并【经典面试OJ详解】【力扣21,力扣23】超详细的算法教程
链表的合并 导航小助手 说在前面 题目链接 链表结构 OJ21.合并两个有序链表 题目描述和算法分析 接口的完整实现代码 OJ23.合并K个升序链表 题目描述和算法分析 接口的完整实现代码 尾声 说在 ...
- Android 相册
- git常用命令(企业级)
一: 常用git命令 # 初始化,将已有的文件初始化为git仓库 git init # 查询文件状态[绿色暂存区,红色表示工作区更改了,没有提交到暂存区] git status git status ...
- SOCKS5协议解析
socks的官方文档:https://www.ietf.org/rfc/rfc1928.txt 本文改变其他作者之手,在原文基础上加入客户端的编写,完善了服务端代码,原文是Linux端的程序代码,本文 ...
- delphi 里 多用TArray 而不是 array of
今天写代码发现个bug,是delphi 编译器 核心层面的: unit ddx.att; interface uses System.Generics.Collections, System.Rtti ...
- Python 国内常用python模块下载地址
国内常用python模块下载地址 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 中国科技大学 https://pypi.mirrors.ustc.edu. ...
- NC19885 [AHOI2009]CHESS 中国象棋
题目链接 题目 题目描述 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮的行走方式大家应该很清楚吧.一个炮要能攻击另一个炮他们必须要 ...