你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

准备要部署到 Azure Spring Apps 中的应用程序

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文适用于:✔️ 基本版/标准版 ✔️ 企业版

本文介绍了如何准备现有的需要部署到 Azure Spring Apps 的 Steeltoe 应用程序。 Azure Spring Apps 提供强大的服务来托管、监视、缩放和更新 Steeltoe 应用。

本文介绍了在 Azure Spring Apps 中运行 .NET Core Steeltoe 应用所需的依赖项、配置和代码。 有关如何将应用程序部署到 Azure Spring Apps 的信息,请参阅在 Azure Spring Apps 中部署你的首个 Spring Boot 应用

注意

针对 Azure Spring Apps 的 Steeltoe 支持目前以公共预览版的形式提供。 使用公共预览版产品/服务,客户可以在产品/服务正式发布之前体验新功能。 公共预览功能和服务并非供生产使用。 有关预览期间支持的详细信息,请参阅常见问题解答或提交支持请求

支持的版本

Azure Spring Apps 支持:

  • .NET Core 3.1
  • Steeltoe 2.4 和 3.0

依赖项

对于 Steeltoe 2.4,请将最新的 Microsoft.Azure.SpringCloud.Client 1.x.x 包添加到项目文件中:

<ItemGroup>
  <PackageReference Include="Microsoft.Azure.SpringCloud.Client" Version="1.0.0-preview.1" />
  <PackageReference Include="Steeltoe.Discovery.ClientCore" Version="2.4.4" />
  <PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" Version="2.4.4" />
  <PackageReference Include="Steeltoe.Management.TracingCore" Version="2.4.4" />
  <PackageReference Include="Steeltoe.Management.ExporterCore" Version="2.4.4" />
</ItemGroup>

对于 Steeltoe 3.0,请将最新的 Microsoft.Azure.SpringCloud.Client 2.x.x 包添加到项目文件中:

<ItemGroup>
  <PackageReference Include="Microsoft.Azure.SpringCloud.Client" Version="2.0.0-preview.1" />
  <PackageReference Include="Steeltoe.Discovery.ClientCore" Version="3.0.0" />
  <PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" Version="3.0.0" />
  <PackageReference Include="Steeltoe.Management.TracingCore" Version="3.0.0" />
</ItemGroup>

更新 Program.cs

Program.Main 方法中,调用 UseAzureSpringCloudService 方法。

对于 Steeltoe 2.4.4,请在 ConfigureWebHostDefaultsAddConfigServer 之后调用 UseAzureSpringCloudService(如调用):

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
        .AddConfigServer()
        .UseAzureSpringCloudService();

对于 Steeltoe 3.0.0,请在 ConfigureWebHostDefaults 和任何 Steeltoe 配置代码之前调用 UseAzureSpringCloudService

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseAzureSpringCloudService()
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
        .AddConfigServer();

启用 Eureka Server 服务发现

注意

Eureka 不适用于企业计划。 如果使用企业计划,请参阅使用服务注册表

在该应用在 Azure Spring Apps 中运行时所使用的配置源中,将 spring.application.name 设置为与项目所部署到的 Azure Spring Apps 应用相同的名称。

例如,如果将名为 EurekaDataProvider 的 .NET 项目部署到名为 planet-weather-provider 的 Azure Spring Apps 应用中,则 appSettings.json 文件应包含以下 JSON

"spring": {
  "application": {
    "name": "planet-weather-provider"
  }
}

使用服务发现

若要通过使用 Eureka Server 服务发现来调用服务,请将 HTTP 请求发送给 http://<app_name>,其中 app_name 是目标应用的 spring.application.name 的值。 例如,以下代码调用 planet-weather-provider 服务:

using (var client = new HttpClient(discoveryHandler, false))
{
    var responses = await Task.WhenAll(
        client.GetAsync("http://planet-weather-provider/weatherforecast/mercury"),
        client.GetAsync("http://planet-weather-provider/weatherforecast/saturn"));
    var weathers = await Task.WhenAll(from res in responses select res.Content.ReadAsStringAsync());
    return new[]
    {
        new KeyValuePair<string, string>("Mercury", weathers[0]),
        new KeyValuePair<string, string>("Saturn", weathers[1]),
    };
}

本文介绍如何准备现有的需要部署到 Azure Spring Apps 的 Java Spring 应用程序。 在配置正确的情况下,Azure Spring Apps 可以提供强大的服务来监视、缩放和更新 Java Spring 应用程序。

在运行此示例之前,可以尝试基础知识快速入门

其他示例说明了在配置 POM 文件时,如何将应用程序部署到 Azure Spring Apps。

本文介绍所需的依赖项,以及如何将它们添加到 POM 文件。

Java 运行时版本

有关详细信息,请参阅 Azure Spring Apps 常见问题解答Java 运行时和 OS 版本部分。

Spring Boot 和 Spring Cloud 版本

若要准备要部署到 Azure Spring Apps 的现有 Spring Boot 应用程序,请按以下部分所述,在应用程序 POM 文件中包含 Spring Boot 和 Spring Cloud 依赖项。

从最新的 Spring Boot 或 Spring Cloud 主版本发布后 30 天开始,Azure Spring Apps 将支持这些主版本。 一旦最新的次要版本发布,Azure Spring Apps 就会支持这些次要版本。 可以从 Spring Boot 版本获取支持的 Spring Boot 版本,从 Spring Cloud 版本获取 Spring Cloud 版本。

下表列出了支持的 Spring Boot 和 Spring Cloud 组合:

Spring Boot 版本 Spring Cloud 版本 商业支持结束
3.2.x 2023.0.x 也称为 Leyton 2026-02-23
3.1.x 2022.0.3+ 也称为 Kilburn 2025-08-18
3.0.x 2022.0.3+ 也称为 Kilburn 2025-02-24
2.7.x 2021.0.3+ 也称为 Jubilee 2025-08-24
2.6.x 2021.0.3+ 也称为 Jubilee 2024-02-24

有关详细信息,请参阅以下页面:

若要启用服务注册表到分布式跟踪的 Azure Spring Apps 内置功能,你还需要在应用程序中包含以下依赖项。 如果不需要特定应用的相应功能,你可以删除这些依赖项。

服务注册表

若要使用托管的 Azure 服务注册表服务,请在 pom.xml 文件中包括 spring-cloud-starter-netflix-eureka-client 依赖项,如下所示:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

服务注册表服务器的终结点自动作为应用的环境变量注入。 然后,应用程序可自行注册到服务注册表服务器,并发现其他依赖性应用程序。

EnableDiscoveryClient 注释

将以下注释添加到应用程序源代码中。

@EnableDiscoveryClient

有关示例,请参阅前面示例中的 piggymetrics 应用程序:

package com.piggymetrics.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy

public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

分布式配置

若要在企业计划中启用分布式配置,请使用 VMware Tanzu 的应用程序配置服务,该服务是一个专有的 VMware Tanzu 组件。 Tanzu 的应用程序配置服务是 Kubernetes 本机的,与 Spring Cloud Config Server 不同。 Tanzu 的应用程序配置服务可以管理从一个或多个 Git 存储库中定义的属性填充的 Kubernetes 本机 ConfigMap 资源。

在企业计划中,没有 Spring Cloud Config Server,但是可以使用 Tanzu 的应用程序配置服务来管理集中式配置。 有关详细信息,请参阅使用 Tanzu 的应用程序配置服务

若要使用 Tanzu 的应用程序配置服务,请针对每个应用执行以下步骤:

  1. 添加显式应用绑定,以声明应用需要使用 Tanzu 的应用程序配置服务。

    注意

    更改绑定/取消绑定状态时,必须重启或重新部署应用才能使更改生效。

  2. 设置配置文件模式。 通过配置文件模式可以选择应用将使用的应用程序和配置文件。 有关详细信息,请参阅使用 Tanzu 的应用程序配置服务模式部分。

    另一种方法是在应用部署的同时设置配置文件模式,如以下示例中所示:

       az spring app deploy \
           --name <app-name> \
           --artifact-path <path-to-your-JAR-file> \
           --config-file-pattern <config-file-pattern>
    

指标

在 pom.xml 文件的 dependencies 节中包括 spring-boot-starter-actuator 依赖项,如下所示:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

指标会定期从 JMX 终结点拉取。 可以通过 Azure 门户将指标可视化。

警告

必须在配置属性中指定 spring.jmx.enabled=true。 否则,无法在 Azure 门户中直观显示指标。

另请参阅

后续步骤

本文介绍了如何配置 Java Spring 应用程序,以便将其部署到 Azure Spring Apps。 若要了解如何设置配置服务器实例,请参阅设置配置服务器实例

GitHub 中提供了更多示例:Azure Spring Apps 示例