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

注意

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

本文介绍如何准备现有的需要部署到 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 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 服务注册表服务,请在 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);
    }
}

分布式配置

若要启用分布式配置,请在 pom.xml 文件的 dependencies 节中包括以下 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。 否则,应用程序将再也不能与配置服务器配合使用。

指标

在 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 示例