问题描述

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: FreeSharedBasicStandard).
  • WEBSITE_COMPUTE_MODE - Specifies whether website is on a dedicated or shared VM/s (Possible values: SharedDedicated).
  • 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 by WEBSITE_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 environmenthttps://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)呢?的更多相关文章

  1. 【Azure 应用服务】一个 App Service 同时部署运行两个及多个 Java 应用程序(Jar包)

    问题描述 如何在一个AppService下同时部署运行多个Java 应用程序呢? 问题解答 因为App Service的默认根目录为 wwwroot.如果需要运行多个Java 应用程序,需要在 www ...

  2. 【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法

    问题描述 在App Service for Windows的环境中,当前只提供了PHP 7.4 版本的选择情况下,如何实现自定义PHP Runtime的版本呢? 如 PHP Version 8.1.9 ...

  3. 【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https

    问题描述 在上篇博文"[Azure 应用服务]App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)"中,实现了通过 HT ...

  4. 【Azure 应用服务】Azure App Service 自带 FTP服务

    问题描述 Azure PaaS服务是否有FTP/S服务呢? 回答问题 应用服务(Web App/App Service)在创建时候,默认创建了FTP服务并自动开启,用于应用部署.但它不是适合作为FTP ...

  5. 【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启动项目,访问效果如下: 但是,当把项目 ...

  6. 【Azure 应用服务】Azure Web App的服务(基于Windows 操作系统部署)在被安全漏洞扫描时发现了TCP timestamps漏洞

    问题背景 什么是TCP timestamps(TCP 时间戳)? The remote host implements TCP Timestamps, as defined by RFC1323 (h ...

  7. 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误

    问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...

  8. 【Azure 应用服务】Azure Function HTTP 触发后, 230秒就超时。而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间?

    问题描述 Azure Function HTTP 触发后, 230秒就超时,而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间? 问题分析 查阅官方文档,对函数应用超时持续时间有 ...

  9. 【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed

    问题描述 编写Powershell Function,登录到China Azure并获取Azure AD User信息,但是发现遇见了 [Error] ERROR: ManagedIdentityCr ...

  10. 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

    一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...

随机推荐

  1. 用Unity3D做游戏开发在Android上的常用调试方法

    Hdg Remote Debug 远程调试 游戏运行在手机上,可以通过pc端的unity来随时修改当前场景中GameObject的变量,从而改变手机上运行时的表现.比如,我可以勾掉下图中的" ...

  2. vim 从嫌弃到依赖(23)——最后的闲扯

    截止到上一篇文章,关于vim的基础操作都已经讨论完了,这篇我主要就是闲扯,瞎聊.就想毕业论文都有一个致谢一样,这篇我们就作为整个系列的致谢吧 学习vim到底能给我们带来什么 学习vim到底能给我们带来 ...

  3. TienChin 活动管理-删除活动

    后端 ActivityController.java @PreAuthorize("hasPermission('tienchin:activity:remove')") @Log ...

  4. 安装 Nginx 修改默认端口

    用远程工具连接我们上次购买的机器,这里我要介绍一个知识点,博主使用的工具是 MobaXterm,这个工具有一个多操作的功能,在下图的位置可以开启多操作,然后连接你的服务器机子即可: 首先我们将机子里面 ...

  5. 一图看懂iPhone 15系列:15/Plus/Pro/Pro Max有啥区别?详细配置对比

    距离iPhone 15系列发布只剩下2天(北京时间9月13日凌晨1点),即将推出预计分别是iPhone 15.iPhone 15 Plus,以及Pro系列的iPhone 15 Pro以及iPhone ...

  6. Primo Ramdisk SCSI虚拟硬盘和Direct-IO虚拟硬盘

    Primo Ramdisk 使用不同的虚拟技术可创建两种类型的虚拟硬盘:SCSI 虚拟硬盘和 Direct-IO 虚拟硬盘. 本篇主要比较两者之间的差异. SCSI 虚拟硬盘遵循SCSI规范,行为上几 ...

  7. 20.3 DLL入口函数--《Windows核心编程》

    如果在执行一些与进程或者线程有关的初始化或者销毁工作的时候,需要 DllMain.如果只需要创建一个包含资源的DLL,不需要这个函数. B00L WINAPI DllMain(HINSTANCE hi ...

  8. P3089 Pogo-Cow S

    [USACO13NOV] Pogo-Cow S 题目传送门 题解 首先,一眼DP,想想怎么推状态转移方程 朴素DP 定义二维数组 \(f[i][j]\),其中第一维表示当前所在的目标点是\(i\),第 ...

  9. Hive中insert into 和 insert overwrite的区别

    相同点 insert into 和 insert overwrite 都是往表中插入数据的. 不同点 区别1: insert into :其实是将数据追加到表的末尾,注意:不是覆盖,是追加. inse ...

  10. win10远程桌面连接,使用正确的用户名和密码仍然不能成功连接

    最近笔记本重置后,台式使用"远程桌面连接"远程笔记本失败了,总是提示"登录没有成功". 开始自查:win10专业版,允许远程的相关设置也都开了,连接的ip正确, ...