共用方式為


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

注意

基本標準和企業方案將從 2025 年 3 月中旬開始淘汰,並停用 3 年。 建議您轉換至 Azure Container Apps。 如需詳細資訊,請參閱 Azure Spring Apps 淘汰公告

標準 耗用量和專用 方案將從 2024 年 9 月 30 日起淘汰,並在六個月後完成關閉。 建議您轉換至 Azure Container Apps。 如需詳細資訊,請參閱 將 Azure Spring Apps 標準取用和專用方案遷移至 Azure Container Apps

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

本文會示範如何準備現有的 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,請在呼叫 ConfigureWebHostDefaults 之後呼叫 UseAzureSpringCloudService,若已呼叫,再請呼叫 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 應用程式中執行應用程式時,在其使用的設定來源中,將 spring.application.name 設定為與部署專案的 Azure Spring 應用程式應用程式相同的名稱。

例如,如果您將名為 EurekaDataProvider 的 .NET 專案部署到名為 planet-weather-providerappSettings.json 檔案的 Azure Spring Apps 應用程式,應該包含下列 JSON:

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

執行服務探索

若要使用 Eureka 伺服器服務探索來呼叫服務,請向 http://<app_name> 提出 HTTP 要求,其中 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 應用程式支援發行 30 天後起最新的 Spring Boot 或 Spring Cloud 的主要版本。 Azure Spring 應用程式一旦發行,就會支援最新的次要版本。 您可以從 Spring Boot 發行版本得到支援的 Spring Boot 版本並從 Spring Cloud 發行版本取得支援的 Spring Cloud 版本。

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

Spring Boot 版本 Spring Cloud 版本 結束支援
3.2.x 2023.0.x 也稱為 Leyton 2024-11-23
3.1.x 2022.0.3+ 也稱為 Kilburn 2024-05-18
3.0.x 2022.0.3+ 也稱為 Kilburn 2023-11-24
2.7.x 2021.0.3+ 也稱為 Jubilee 2023-11-24

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

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

服務登錄

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

<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);
    }
}

分散式設定

若要啟用分散式設定,請在 pom.xml 檔案的相依性區段中納入下列 spring-cloud-config-client 相依性:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

警告

請勿在啟動程序設定中指定 spring.cloud.config.enabled=false。 否則,應用程式會停止使用 Config Server。

計量

請在 pom.xml 檔案的相依性區段中納入 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。 若要瞭解如何設定 Config Server 執行個體,請參閱設定 Config Server 執行個體

更多範例請至 GitHub:Azure Spring Apps 範例