準備應用程式以在 Azure Spring Apps 中部署

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但是您暫時還是會在某些位置看到舊的名稱。我們正在致力更新螢幕擷取畫面、影片和圖表等資產。

本文適用於: ✔️基本/標準✔️企業

本文說明如何準備現有的 Steeltoe 應用程式以部署至 Azure Spring Apps。 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, UseAzureSpringCloudService 如果呼叫後 ConfigureWebHostDefaults 和之後 AddConfigServer 呼叫:

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

針對 Steeltoe 3.0.0,請在任何 Steeltoe 設定程式代碼之前ConfigureWebHostDefaults和之前呼叫UseAzureSpringCloudService

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

啟用 Eureka 伺服器服務探索

注意

Eureka 不適用於企業方案。 如果您使用企業方案,請參閱 使用服務登錄

在 Azure Spring Apps 中執行應用程式時所使用的組態來源中,將 設定 spring.application.name 為與部署專案之 Azure Spring Apps 應用程式相同的名稱。

例如,如果您將名為 的 .NET 專案部署到名為 EurekaDataProviderplanet-weather-provider應用程式的 Azure Spring Apps 應用程式 設定.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]),
    };
}

本文說明如何準備現有的 Java Spring 應用程式以部署至 Azure Spring Apps。 如果設定正確,Azure Spring Apps 會提供強大的服務,以監視、調整及更新 Java Spring 應用程式。

執行此範例之前,您可以嘗試 基本快速入門

其他範例說明如何在設定 POM 檔案時將應用程式部署至 Azure Spring Apps。

本文說明必要的相依性,以及如何將它們新增至 POM 檔案。

Java 運行時間版本

如需詳細資訊,請參閱 Azure Spring Apps 常見問題Java 運行時間和 OS 版本一節。

Spring Boot 和 Spring Cloud 版本

若要準備現有的 Spring Boot 應用程式以部署至 Azure Spring Apps,請在應用程式 POM 檔案中包含 Spring Boot 和 Spring Cloud 相依性,如下列各節所示。

Azure Spring Apps 支援從發行 30 天后開始的最新 Spring Boot 或 Spring Cloud 主要版本。 Azure Spring Apps 一旦發行,就會支援最新的次要版本。 您可以從 Spring Boot 版本和 Spring Cloud 版本取得支援的 Spring Boot 版本。

下表列出支援的 Spring Boot 和 Spring Cloud 組合:

Spring Boot 版本 Spring Cloud 版本 終止商業支援
3.2.x 2023.0.x 也稱為萊頓 2026-02-23
3.1.x 2022.0.3+ 也稱為基爾本 2025-08-18
3.0.x 2022.0.3+ 也稱為基爾本 2025-02-24
2.7.x 2021.0.3+ 也稱為禧年 2025-08-24
2.6.x 2021.0.3+ 也稱為禧年 2024-02-24

如需詳細資訊,請參閱下列頁面:

若要啟用 Azure Spring Apps 從服務登錄到分散式追蹤的內建功能,您也必須在應用程式中包含下列相依性。 如果您不需要特定應用程式的對應功能,則可以卸除其中一些相依性。

服務登錄

若要使用受控 Azure 服務登錄服務,請在spring-cloud-starter-netflix-eureka-clientpom.xml檔案中包含相依性,如下所示:

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

Service Registry 伺服器的端點會自動插入為應用程式的環境變數。 應用程式可以向 Service Registry 伺服器註冊自己,並探索其他相依應用程式。

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 的應用程式組態服務可讓您管理 Kubernetes 原生 ConfigMap 資源,這些資源會從一或多個 Git 存放庫中定義的屬性填入。

在企業方案中,沒有 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>
    

計量

spring-boot-starter-actuator相依性包含在pom.xml檔案的 [相依性] 區段中,如下所示:

<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。 若要瞭解如何設定 Config Server 實例,請參閱 設定 Config Server 實例

GitHub 提供更多範例: Azure Spring Apps 範例