【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 ...
随机推荐
- 信创-飞腾CPU路线图
- Vue.use和install之间的关系
创建一个plugins.js文件 跟main.js同级下,创建一个plugins.js文件 export default { // install是vue给我们提供的.它会自动去执行install. ...
- 【主流技术】15 分钟掌握 Redis 的安装部署和基本特性
目录 前言 一.Redis 概述 1.1Redis 是什么? 1.2Redis 能做什么? 1.3基础知识 二.Redis 安装与基本命令 2.1Windows 安装 方式一 方式二 2.2Linux ...
- 多路io复用epoll [补档-2023-07-20]
多路io- epoll 4-1简介 它是linux中内核实现io多路/转接复用的一个实现.(epoll不可跨平台,只能用于Linux)io多路转接是指在同一个操作里,同时监听多个输入输出源,在其中 ...
- 遥感图像处理笔记之【FastAI Multi-label image classification】
遥感图像处理学习(4) 前言 遥感系列第4篇.遥感图像处理方向的学习者可以参考或者复刻 本文初编辑于2023年12月15日 2024年1月24日搬运至本人博客园平台 文章标题:FastAI Multi ...
- Mygin中间件优化及日志中间件
本篇是mygin的第七篇,参照gin框架,感兴趣的可以从 Mygin第一篇 开始看,Mygin从零开始完全手写,在实现的同时,带你一窥gin框架的核心原理实现. 目的 中间件Middleware优化 ...
- 推荐一款开源的Diffy自动化测试框架
1. 前言 软件测试是软件开发生命周期一个十分重要的环节,测试工作开展的好坏,很大程度上决定了产品质量的好坏,但软件产品随着版本的持续迭代,功能日益增多,系统愈加复杂,而从质量保障的角度,除了要保障好 ...
- KB0003.申请和加载DoraCloud的软件许可
KB0003.申请和加载DoraCloud的软件许可 DoraCloud安装后,默认处于30天试用状态.如果您购买了软件授权,可以申请许可证. 在[系统][License管理][获取License文件 ...
- electron、nodejs、typescript、idea 组合开发,安装和配置 以及错误 等 详细说明
1. 创建项目,创建时 选择 nodejs 项目,因为 开发 electron 与 开发 nodejs 基本一致. 2.安装 electron npm i -D electron@beta 看目录 ...
- TStringList,的IndexOf,find,IndexOfName 例子
a=wokao b=in c=wori d=ri e=我靠 f=我插 procedure TForm1.Button1Click(Sender: TObject); var MyList: TStri ...