问题描述

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. 从源代码构建TensorFlow流程记录

    京东科技隐私计算产品部 曹雨晨 为什么从源代码构建 通常情况下,直接安装构建好的.whl即可.不过,当需要一些特殊配置(或者闲来无事想体会 TensorFlow 构建过程到底有多麻烦)的时候,则需要选 ...

  2. React Hooks源码深度解析

    作者:京东零售 郑炳懿 前言 React Hooks是React16.8 引入的一个新特性,它允许函数组件中使用state和其他 React 特性,而不必使用类组件.Hooks是一个非常重要的概念,因 ...

  3. python中,Microsoft Visual C++ 14.0 or greater is required问题解决方案

    今天在写一个小程序,安装依赖的时候发现这个问题,平时都是直接安装Visual Studio解决,但是这个安装太大了,所以解决看看怎么安装是最方便的,最容易解决的. 下面这个就是出现的问题: build ...

  4. python排序之快速排序

    快速排序 快速排序是比较常用的一种排序方式,通过递归的方法进行排序 首先使用递归方式我们先要解决两个问题:1找到基准条件 2找到递归条件 基线条件为数组为空或只包含一个元素.在这种情况下,只需原样返回 ...

  5. 守护线程(Python)

    import time from threading import Thread def son(): while True: print('in son') time.sleep(1) def so ...

  6. 2000元内最超值游戏处理器!锐龙5 7500F首发评测:轻松超频5.6GHz游戏追平i5-13600K

    一.前言:首款不带核显的锐龙7000处理器 以往的桌面锐龙处理器,带核显型号的很少,而到了Zen4时代,此前已上市的锐龙7000系列处理器都集成了核显. 现在,AMD锐龙5 7500F来了,这是AMD ...

  7. 联想T30瘦客户机安装DoraOS体验

    硬件配置:J4125 .8G RAM. 128G ROM 联想T30台式电脑,它是一台迷你计算机,尺寸小巧玲珑,重量适中,方便携带.它的性能十分强大,能够运行各种应用程序,包括网页浏览器.视频播放器等 ...

  8. electron useContentSize的详解

    useContentSize作用就是  由于window窗体有边框和title区域menu等,该区域不能显示自己的html页面(new BrowserWindow 时设置frame=false禁用边框 ...

  9. 让python程序一直在window后台进程运行

    一.让python程序后台运行 1.创建一个app.py文件,如 while 1: print(123)2.创建一个set_py.bat文件,里面写 python app.py3.创建一个start_ ...

  10. nginx 配置mp4文件播放

    nginx 配置mp4文件播放 ​ 由于工作需要一个离线的视频播放地址,就想简单一点直接把视频文件放到nginx里面实现视频播放,但是把mp4文件放上去之后地址栏输入地址直接就是下载文件,这跟我想象的 ...