快速入门:部署第一个 Azure Spring Cloud 应用程序Quickstart: Deploy your first Azure Spring Cloud application

本快速入门介绍如何部署简单的 Azure Spring Cloud 微服务应用程序,以便在 Azure 上运行。This quickstart explains how to deploy a simple Azure Spring Cloud microservice application to run on Azure.

本教程中使用的应用程序代码是使用 Spring Initializr 生成的简单应用。The application code used in this tutorial is a simple app built with Spring Initializr. 完成此示例后,你可以在线访问应用程序,并可通过 Azure 门户对其进行管理。When you've completed this example, the application will be accessible online and can be managed via the Azure portal.

本快速入门介绍如何:This quickstart explains how to:

  • 生成基本 Spring Cloud 项目Generate a basic Spring Cloud project
  • 预配服务实例Provision a service instance
  • 使用公共终结点生成并部署应用Build and deploy the app with a public endpoint
  • 实时流式传输日志Stream logs in real time


完成本快速入门教程需要:To complete this quickstart:

生成 Spring Cloud 项目Generate a Spring Cloud project

首先,使用 Spring Initializr 生成一个包含推荐的 Azure Spring Cloud 依赖项的示例项目。Start with Spring Initializr to generate a sample project with recommended dependencies for Azure Spring Cloud. 下图显示了为此示例项目设置的 Initializr。The following image shows the Initializr set up for this sample project.


Initializr 页


我们已经确认,Spring Boot 2.4 在应用与 Eureka 之间进行 TLS 身份验证时出现问题,我们目前正在与 Spring 社区协作,以解决此问题。We've identified an issue with Spring Boot 2.4 on TLS authentication between your apps and Eureka and are currently working with the Spring community to resolve it. 请参阅我们的常见问题解答以获取解决方法。Please refer to our FAQ for the workaround.

  1. 设置好所有依赖项后,单击“生成”。Click Generate when all the dependencies are set. 下载包并将其解压缩,然后按照如下所示添加 src/main/java/com/example/hellospring/HelloController.java,以便为简单的 Web 应用程序创建 Web 控制器:Download and unpack the package, then create a web controller for a simple web application by adding src/main/java/com/example/hellospring/HelloController.java as follows:

    package com.example.hellospring;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    public class HelloController {
        public String index() {
            return "Greetings from Azure Spring Cloud!";

预配 Azure Spring Cloud 的实例Provision an instance of Azure Spring Cloud

以下步骤使用 Azure 门户创建 Azure Spring Cloud 的实例。The following procedure creates an instance of Azure Spring Cloud using the Azure portal.

  1. 在新选项卡中,打开 Azure 门户In a new tab, open the Azure portal.

  2. 在顶部搜索框中,搜索“Azure Spring Cloud”。From the top search box, search for Azure Spring Cloud.

  3. 从结果中选择“Azure Spring Cloud”。Select Azure Spring Cloud from the results.

    ASC 图标 - 启动

  4. 在“Azure Spring Cloud”页上,单击“+ 添加”。On the Azure Spring Cloud page, click + Add.

    ASC 图标 - 添加

  5. 在 Azure Spring Cloud“创建”页中填写表单。Fill out the form on the Azure Spring Cloud Create page. 遵循以下指南:Consider the following guidelines:

    • 订阅:选择要在其中收取此资源费用的订阅。Subscription: Select the subscription you want to be billed for this resource.
    • 资源组:最佳做法是为新资源创建新的资源组。Resource group: Creating new resource groups for new resources is a best practice. 后面的步骤会将其用作 <resource group name>。This will be used in later steps as <resource group name>.
    • 服务详细信息/名称:指定 <service instance name>。Service Details/Name: Specify the <service instance name>. 该名称必须为 4 到 32 个字符,只能包含小写字母、数字及连字符。The name must be between 4 and 32 characters long and can contain only lowercase letters, numbers, and hyphens. 服务名称的第一个字符必须是字母,最后一个字符必须是字母或数字。The first character of the service name must be a letter and the last character must be either a letter or a number.
    • 位置:为服务实例选择区域。Location: Select the region for your service instance.

    ASC 门户启动

  6. 单击“审阅并创建”。Click Review and create.

生成并部署应用Build and deploy the app

以下步骤使用 Azure CLI 生成并部署应用程序。The following procedure builds and deploys the application using the Azure CLI. 在项目的根目录中,执行以下命令。Execute the following command at the root of the project.

  1. 使用 Maven 生成项目:Build the project using Maven:

    mvn clean package -DskipTests
  2. (如果尚未安装)安装适用于 Azure CLI 的 Azure Spring Cloud 扩展:(If you haven't already installed it) Install the Azure Spring Cloud extension for the Azure CLI:

    az extension add --name spring-cloud
  3. 创建已分配公共终结点的应用:Create the app with public endpoint assigned:

    az spring-cloud app create -n hellospring -s <service instance name> -g <resource group name> --is-public
  4. 为应用部署 Jar 文件(在 Windows 上为 target\hellospring-0.0.1-SNAPSHOT.jar):Deploy the Jar file for the app (target\hellospring-0.0.1-SNAPSHOT.jar on Windows):

    az spring-cloud app deploy -n hellospring -s <service instance name> -g <resource group name> --jar-path <jar file path>
  5. 完成应用程序部署需要几分钟时间。It takes a few minutes to finish deploying the application. 若要确认它是否已经部署,请在 Azure 门户中转到“应用”边栏选项卡。To confirm that it has deployed, go to the Apps blade in the Azure portal. 你应该会看到应用程序的状态。You should see the status of the application.

完成部署后,就可以通过 https://<service instance name>-hellospring.microservices.azure.cn/ 访问应用。Once deployment has completed, you can access the app at https://<service instance name>-hellospring.microservices.azure.cn/.

从浏览器访问应用 Access app from browser

实时流式处理日志Streaming logs in real time

使用以下命令从应用获取实时日志。Use the following command to get real time logs from the App.

az spring-cloud app logs -n hellospring -s <service instance name> -g <resource group name> --lines 100 -f

日志随即出现在结果中:Logs appear in the results:

流式处理日志 Streaming Logs


使用 az spring-cloud app logs -h 浏览更多参数和日志流功能。Use az spring-cloud app logs -h to explore more parameters and log stream functionalities.

如需查看高级日志分析功能,请访问 Azure 门户菜单中的“日志”选项卡。For advanced logs analytics features, visit Logs tab in the menu on Azure portal. 这里日志会延迟几分钟。Logs here have a latency of a few minutes.

日志分析 Logs Analytics

清理资源Clean up resources

在前面的步骤中,你创建了 Azure 资源,这些资源在订阅中保留期间会继续产生费用。In the preceding steps, you created Azure resources that will continue to accrue charges while they remain in your subscription. 如果认为将来不需要这些资源,请通过门户删除资源组,或在 Azure CLI 中运行以下命令来这样做:If you don't expect to need these resources in the future, delete the resource group from the portal or by running the following command in the Azure CLI:

az group delete --name <your resource group name; for example: hellospring-1558400876966-rg> --yes

后续步骤Next steps

在此快速入门中,读者学习了如何:In this quickstart, you learned how to:

  • 生成基本 Azure Spring Cloud 项目Generate a basic Azure Spring Cloud project
  • 预配服务实例Provision a service instance
  • 使用公共终结点生成并部署应用Build and deploy the app with a public endpoint
  • 实时流式传输日志Stream logs in real time

若要了解如何使用更多 Azure Spring 功能,请转到将示例应用程序部署到 Azure Spring Cloud 的快速入门系列:To learn how to use more Azure Spring capabilities, advance to the quickstart series that deploys a sample application to Azure Spring Cloud:

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