准备要部署到 Azure Spring Cloud 中的应用程序Prepare an application for deployment in Azure Spring Cloud

本主题介绍如何准备现有的需要部署到 Azure Spring Cloud 的 Java Spring 应用程序。This topic shows how to prepare an existing Java Spring application for deployment to Azure Spring Cloud. 在配置正确的情况下,Azure Spring Cloud 可以提供强大的服务来监视、缩放和更新 Java Spring Cloud 应用程序。If configured properly, Azure Spring Cloud provides robust services to monitor, scale, and update your Java Spring Cloud application.

在运行此示例之前,可以尝试基础知识快速入门Before running this example, you can try the basic quickstart.

其他示例说明了在配置 POM 文件时,如何将应用程序部署到 Azure Spring Cloud。Other examples explain how to deploy an application to Azure Spring Cloud when the POM file is configured.

本文介绍所需的依赖项,以及如何将它们添加到 POM 文件。This article explains the required dependencies and how to add them to the POM file.

Java 运行时版本Java Runtime version

只有 Spring/Java 应用程序能够在 Azure Spring Cloud 中运行。Only Spring/Java applications can run in Azure Spring Cloud.

Azure Spring Cloud 支持 Java 8 和 Java 11。Azure Spring Cloud supports both Java 8 and Java 11. 托管环境包含用于 Azure 的最新版 Azul Zulu OpenJDK。The hosting environment contains the latest version of Azul Zulu OpenJDK for Azure. 若要详细了解用于 Azure 的 Azul Zulu OpenJDK,请参阅安装 JDKFor more information about Azul Zulu OpenJDK for Azure, see Install the JDK.

Spring Boot 和 Spring Cloud 版本Spring Boot and Spring Cloud versions

若要准备要部署到 Azure Spring Cloud 的现有 Spring Boot 应用程序,请按以下部分中所述,在应用程序 POM 文件中包含 Spring Boot 和 Spring Cloud 依赖项。To prepare an existing Spring Boot application for deployment to Azure Spring Cloud include the Spring Boot and Spring Cloud dependencies in the application POM file as shown in the following sections.

Azure Spring Cloud 支持 Spring Boot 版本 2.2、2.3、2.4。Azure Spring Cloud supports Spring Boot version 2.2, 2.3, 2.4. 下表列出了支持的 Spring Boot 和 Spring Cloud 组合:The following table lists the supported Spring Boot and Spring Cloud combinations:

Spring Boot 版本Spring Boot version Spring Cloud 版本Spring Cloud version
2.22.2 Hoxton.SR8Hoxton.SR8
2.32.3 Hoxton.SR8Hoxton.SR8
2.4.1+2.4.1+ 2020.0.02020.0.0

备注

我们发现 Spring Boot 2.4.0 在应用和 Eureka 之间的 TLS 身份验证存在问题,请使用 2.4.1 版或更高版本。We've identified an issue with Spring Boot 2.4.0 on TLS authentication between your apps and Eureka, please use 2.4.1 or above. 如果坚持使用 2.4.0,请参阅我们的常见问题解答以获取解决方法。Please refer to our FAQ for the workaround if you insist on using 2.4.0.

Spring Boot 版本 2.2/2.3 的依赖项Dependencies for Spring Boot version 2.2/2.3

对于 Spring Boot 版本 2.2,请将以下依赖项添加到应用程序 POM 文件中。For Spring Boot version 2.2 add the following dependencies to the application POM file.

    <!-- Spring Boot dependencies -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <!-- Spring Cloud dependencies -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Spring Boot 版本 2.4 的依赖项Dependencies for Spring Boot version 2.4

对于 Spring Boot 版本 2.2,请将以下依赖项添加到应用程序 POM 文件中。For Spring Boot version 2.2 add the following dependencies to the application POM file.

    <!-- Spring Boot dependencies -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1.RELEASE</version>
    </parent>

    <!-- Spring Cloud dependencies -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

警告

请勿在配置中指定 server.portDon't specify server.port in your configuration. Azure Spring Cloud 会将此设置重写为固定端口号。Azure Spring Cloud will overide this setting to a fixed port number. 也请遵从此设置,不要在代码中指定服务器端口。Please also respect this setting and not specify server port in your code.

若要启用服务注册表到分布式跟踪的 Azure Spring Cloud 内置功能,你还需要在应用程序中包含以下依赖项。To enable the built-in features of Azure Spring Cloud from service registry to distributed tracing, you need to also include the following dependencies in your application. 如果不需要特定应用的相应功能,你可以删除这些依赖项。You can drop some of these dependencies if you don't need corresponding features for the specific apps.

服务注册表Service Registry

若要使用托管的 Azure 服务注册表服务,请在 pom.xml 文件中包括 spring-cloud-starter-netflix-eureka-client 依赖项,如下所示:To use the managed Azure Service Registry service, include the spring-cloud-starter-netflix-eureka-client dependency in the pom.xml file as shown here:

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

服务注册表服务器的终结点自动作为应用的环境变量注入。The endpoint of the Service Registry server is automatically injected as environment variables with your app. 然后,应用程序可自行注册到服务注册表服务器,并发现其他依赖性微服务。Applications can register themselves with the Service Registry server and discover other dependent microservices.

EnableDiscoveryClient 注释EnableDiscoveryClient annotation

将以下注释添加到应用程序源代码中。Add the following annotation to the application source code.

@EnableDiscoveryClient

有关示例,请参阅前面示例中的 piggymetrics 应用程序:For example, see the piggymetrics application from earlier examples:

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

分布式配置Distributed Configuration

若要启用分布式配置,请在 pom.xml 文件的 dependencies 节中包括以下 spring-cloud-config-client 依赖项:To enable Distributed Configuration, include the following spring-cloud-config-client dependency in the dependencies section of your pom.xml file:

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

警告

请勿在启动配置中指定 spring.cloud.config.enabled=falseDon't specify spring.cloud.config.enabled=false in your bootstrap configuration. 否则,应用程序将再也不能与配置服务器配合使用。Otherwise, your application stops working with Config Server.

指标Metrics

在 pom.xml 文件的 dependencies 节中包括 spring-boot-starter-actuator 依赖项,如下所示:Include the spring-boot-starter-actuator dependency in the dependencies section of your pom.xml file as shown here:

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

指标会定期从 JMX 终结点拉取。Metrics are periodically pulled from the JMX endpoints. 可以通过 Azure 门户将指标可视化。You can visualize the metrics by using the Azure portal.

警告

请在配置属性中指定 spring.jmx.enabled=truePlease specify spring.jmx.enabled=true in your configuration property. 否则,无法在 Azure 门户中直观显示指标。Otherwise, metrics can't be visualized in Azure portal.

分布式跟踪Distributed Tracing

还需让 Azure Application Insights 实例能够兼容 Azure Spring Cloud 服务实例。You also need to enable an Azure Application Insights instance to work with your Azure Spring Cloud service instance. 若要了解如何将 Application Insights 与 Azure Spring Cloud 配合使用,请参阅有关分布式跟踪的文档For information about how to use Application Insights with Azure Spring Cloud, see the documentation on distributed tracing.

Spring Boot 2.2/2.3Spring Boot 2.2/2.3

在 pom.xml 文件的 dependencies 节中包括下面的 spring-cloud-starter-sleuthspring-cloud-starter-zipkin 依赖项:Include the following spring-cloud-starter-sleuth and spring-cloud-starter-zipkin dependencies in the dependencies section of your pom.xml file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

Spring Boot 2.4Spring Boot 2.4

在 pom.xml 文件的 dependencies 节中包括下面的 spring-cloud-sleuth-zipkin 依赖项:Include the following spring-cloud-sleuth-zipkin dependency in the dependencies section of your pom.xml file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

另请参阅See also

后续步骤Next steps

本主题介绍了如何配置 Java Spring 应用程序,以便将其部署到 Azure Spring Cloud。In this topic, you learned how to configure your Java Spring application for deployment to Azure Spring Cloud. 若要了解如何设置配置服务器实例,请参阅设置配置服务器实例To learn how to set up a Config Server instance, see Set up a Config Server instance.

GitHub 中提供了更多示例:Azure Spring Cloud 示例More samples are available on GitHub: Azure Spring Cloud Samples.