ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Azure 应用服务】在Azure App Service多实例的情况下,如何在应用中通过代码获取到实例名(Instance ID)呢?

2021-12-23 20:05:35  阅读:207  来源: 互联网

标签:WEBSITE Service App site Instance 实例 Azure ID


问题描述

App Service开启多实例后,如何在代码中获取当前请求所真实达到的实例呢?

问题答案

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信息。如:

 

 

参考资料

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

 

标签:WEBSITE,Service,App,site,Instance,实例,Azure,ID
来源: https://www.cnblogs.com/lulight/p/15724960.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有