共用方式為

为 Azure 应用服务配置 Aspire 应用

本文介绍如何配置部署到 Azure 应用服务的Aspire 应用。 Aspire 提供了一种简洁且有指导性的方式,用于构建可观察的、生产就绪的云原生应用程序。通过与应用服务的集成,它允许你通过代码自定义底层 Azure 基础设施。

如果尚未将 Aspire 应用部署到应用服务,请先参阅 快速入门指南

先决条件

了解预配的资源内容

调用 AddAzureAppServiceEnvironment时,Aspire 默认预配以下 Azure 资源:

资源 Description
应用服务计划 基于 Linux 的高级 P0V3 托管计划
Azure 容器注册表 用于存储容器映像的基本 SKU 注册表
用户分配的托管标识 用于在应用服务和容器注册表之间进行安全访问
角色分配 分配给托管标识的 ACR 拉取角色

这些资源提供将容器化 Aspire 应用部署到 Azure 应用服务所需的基础结构。

连接到现有的应用服务计划

如果有现有的 Azure 应用服务计划,可以连接到它,而不是预配新计划。 AsExisting使用该方法引用现有资源:

var builder = DistributedApplication.CreateBuilder(args);

var existingAppServicePlanName = builder.AddParameter("existingAppServicePlanName");
var existingResourceGroup = builder.AddParameter("existingResourceGroup");

var appServiceEnv = builder.AddAzureAppServiceEnvironment("app-service-env")
    .AsExisting(existingAppServicePlanName, existingResourceGroup);

builder.AddProject<Projects.WebApi>("api")
    .PublishAsAzureAppServiceWebsite((infra, website) =>
    {
        // Optional: customize the Azure App Service website here
    });

builder.Build().Run();

这种方法在您希望实现以下目标时很有用:

  • 跨多个应用程序共享应用服务计划
  • 使用在 Aspire 外部部署的应用服务计划
  • 连接到其他资源组中的资源

将项目发布为应用服务网站

使用 PublishAsAzureAppServiceWebsite 方法将计算资源部署为 Azure 应用服务网站:

var builder = DistributedApplication.CreateBuilder(args);

var appServiceEnv = builder.AddAzureAppServiceEnvironment("app-service-env");

builder.AddProject<Projects.WebApi>("api")
    .PublishAsAzureAppServiceWebsite((infra, website) =>
    {
        // Optional: customize the Azure App Service website here
    });

builder.Build().Run();

在本地开发期间(使用 F5 或 dotnet run运行时),项目在本地运行。 当您使用 azd up 发布应用程序时,项目将会在预配环境中部署为一个 Azure 应用服务网站。

配置应用服务计划 SKU 和定价层

可以使用该方法 ConfigureInfrastructure 自定义应用服务计划 SKU、层和容量。 此方法允许访问和修改 Aspire 预配的基础 Azure 资源。

AppHost.cs 文件中,使用自定义计划设置配置应用服务环境:

using Azure.Provisioning.AppService;

var builder = DistributedApplication.CreateBuilder(args);

builder.AddAzureAppServiceEnvironment("app-service-env")
    .ConfigureInfrastructure((infra) =>
    {
        var plan = infra.GetProvisionableResources().OfType<AppServicePlan>().Single();
        plan.Sku = new AppServiceSkuDescription
        {
            Name = "P1V3",
            Tier = "Premium"
        };
    });

builder.Build().Run();

ConfigureInfrastructure 回调提供对 Azure 预配资源的直接访问权限。 在本示例中:

  • GetProvisionableResources() 返回正在预配的所有 Azure 资源。
  • OfType<AppServicePlan>() 使用筛选器以获取应用服务计划。
  • 然后,可以修改属性,例如 Sku.NameSku.TierSku.Capacity (实例数)。

配置 Aspire 仪表板

部署到 Azure 应用服务时,默认包含 Aspire 仪表板,让你能够查看已部署的应用程序:

builder.AddAzureAppServiceEnvironment("app-service-env");
// Dashboard is included by default at https://[prefix]-aspiredashboard-[unique string].chinacloudsites.cn

部署的仪表板提供与本地开发相同的体验:查看生产环境的日志、跟踪、指标和应用程序拓扑。

若要禁用仪表板,请执行以下操作:

builder.AddAzureAppServiceEnvironment("app-service-env")
    .WithDashboard(enable: false);

配置 Azure Application Insights

启用 Azure Application Insights 以实现全面的监视和遥测:

builder.AddAzureAppServiceEnvironment("app-service-env")
    .WithAzureApplicationInsights();

启用后,Aspire 会自动:

  • 创建 Log Analytics 工作区。
  • 创建 Application Insights 资源。
  • 使用连接字符串配置所有应用服务 Web 应用。
  • APPLICATIONINSIGHTS_CONNECTION_STRING 注入到您的应用程序中。

还可以引用现有的 Application Insights 资源:

var insights = builder.AddAzureApplicationInsights("insights");

builder.AddAzureAppServiceEnvironment("app-service-env")
    .WithAzureApplicationInsights(insights);

配置应用设置

可以通过 PublishAsAzureAppServiceWebsite 方法结合基础设施配置将自定义应用设置添加到 App Service 应用。

builder.AddProject<Projects.aspire_starter_Web>("webfrontend")
    .WithExternalHttpEndpoints()
    .WithReference(apiService)
    .WaitFor(apiService)
    .PublishAsAzureAppServiceWebsite((infra, website) =>
    {
        website.SiteConfig.AppSettings.Add(new AppServiceNameValuePair
        {
            Name = "WEBSITE_LOAD_CERTIFICATES",
            Value = "*"
        });
        website.SiteConfig.AppSettings.Add(new AppServiceNameValuePair
        {
            Name = "MyCustomSetting",
            Value = "MyCustomValue"
        });
    });

可以通过 SiteConfig.AppSettings 集合添加任何应用服务应用程序设置。

为资源添加标签

标记有助于组织和管理 Azure 资源。 可以将标记添加到网站和应用服务计划。

将标记添加到网站:

builder.AddProject<Projects.aspire_starter_Web>("webfrontend")
    .PublishAsAzureAppServiceWebsite((infra, website) =>
    {
        website.Tags.Add("Environment", "Production");
        website.Tags.Add("Team", "Engineering");
    });

将标签添加到应用服务计划中:

builder.AddAzureAppServiceEnvironment("app-service-env")
    .ConfigureInfrastructure(infra =>
    {
        var plan = infra.GetProvisionableResources().OfType<AppServicePlan>().Single();
        plan.Tags.Add("Environment", "Production");
        plan.Tags.Add("CostCenter", "Engineering");
    });

配置运行状况探测

运行状况探测允许 Azure 应用服务监视应用程序的运行状况并做出路由决策。 可以使用该方法 WithHttpProbe 配置不同的探测类型。

#pragma warning disable ASPIREPROBES001
builder.AddProject<Projects.aspire_starter_Web>("webfrontend")
    .WithHttpProbe(ProbeType.Liveness, "/healthz")
    // ... other configuration
#pragma warning restore ASPIREPROBES001

注释

使用 WithHttpProbe 可能需要禁止 ASPIREPROBES001 显示诊断警告,因为此功能处于预览状态。

确保应用程序提供运行状况检查接口。 对于 ASP.NET Core 应用,可以使用内置运行状况检查中间件:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHealthChecks();

var app = builder.Build();

app.MapHealthChecks("/healthz");

app.Run();

配置外部终结点

将 Aspire 应用部署到应用服务时,服务到服务通信需要外部 HTTP 终结点。 与容器应用不同,应用服务当前不通过内部终结点管理应用之间的流量。

var apiService = builder.AddProject<Projects.aspire_starter_ApiService>("apiservice")
    .WithExternalHttpEndpoints()
    .WithHttpHealthCheck("/health");

builder.AddProject<Projects.aspire_starter_Web>("webfrontend")
    .WithExternalHttpEndpoints()
    .WithReference(apiService)
    .WaitFor(apiService);

该方法 WithExternalHttpEndpoints() 将项目配置为可通过公共 HTTP 终结点访问。 这是以下条件所必需的:

  • Aspire 应用中其他服务需调用的后端服务
  • 用户直接访问的前端应用程序