如何从源代码启动 Spring Cloud 应用程序How to Launch your Spring Cloud application from source code

Azure Spring Cloud 在 Azure 上启用了基于 Spring Cloud 的微服务应用程序。Azure Spring Cloud enables Spring Cloud based microservice applications on Azure.

可直接从 java 源代码或预构建的 JAR 启动应用程序。You can launch applications directly from java source code or from a pre-built JAR. 本文介绍部署过程。This article explains the deployment procedures.

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

  • 预配服务实例Provision a service instance
  • 为实例设置配置服务器Set a configuration server for an instance
  • 在本地构建微服务应用程序Build a microservices application locally
  • 部署每个微服务Deploy each microservice
  • 为应用程序分配公共终结点Assign public endpoint for your application

先决条件Prerequisites

在开始之前,请确保 Azure 订阅已安装所需的依赖项:Before you begin, ensure that your Azure subscription has the required dependencies:

  1. 安装 GitInstall Git
  2. 安装 JDK 8Install JDK 8
  3. 安装 Maven 3.0 或更高版本Install Maven 3.0 or above
  4. 安装 Azure CLIInstall the Azure CLI
  5. 注册 Azure 订阅Sign up for an Azure subscription

安装 Azure CLI 扩展Install the Azure CLI extension

通过以下命令安装用于 Azure CLI 的 Azure Spring Cloud 扩展Install the Azure Spring Cloud extension for the Azure CLI with the following command

az extension add --name spring-cloud

使用 Azure CLI 预配服务实例Provision a service instance using the Azure CLI

登录到 Azure CLI 并选择你的有效订阅。Login to the Azure CLI and choose your active subscription.

az login
az account list -o table
az account set --subscription

创建用于包含 Azure Spring Cloud 服务的资源组。Create a resource group to contain your Azure Spring Cloud service. 你可以详细了解 Azure 资源组You can learn more about Azure Resource Groups.

az group create --location chinaeast2 --name <resource group name>

运行以下命令以预配 Azure Spring Cloud 的实例。Run the following commands to provision an instance of Azure Spring Cloud. 准备 Azure Spring Cloud 服务的名称。Prepare a name for your Azure Spring Cloud service. 该名称必须为 4 到 32 个字符,只能包含小写字母、数字及连字符。The name must be between 4 and 32 characters 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.

az spring-cloud create -n <resource name> -g <resource group name>

部署服务实例需要大约五分钟时间。The service instance will take about five minutes to deploy.

使用以下命令设置默认的资源组名称和 Azure Spring Cloud 实例名称:Set your default resource group name and Azure Spring Cloud instance name using the following commands:

az configure --defaults group=<service group name>
az configure --defaults spring-cloud=<service instance name>

创建 Azure Spring Cloud 应用程序Create the Azure Spring Cloud application

以下命令在订阅中创建 Azure Spring Cloud 应用程序。The following command creates an Azure Spring Cloud application in your subscription. 此操作将创建一个空的服务,我们可以将其上传到应用程序。This creates an empty service to which we can upload our application.

az spring-cloud app create -n <app-name>

部署 Spring Cloud 应用程序Deploy your Spring Cloud application

可以从预构建的 JAR 或从 Gradle 或 Maven 存储库部署应用程序。You can deploy your application from a pre-built JAR or from a Gradle or Maven repository. 在下面找到每种情况的说明。Find instructions for each case below.

部署预构建的 JARDeploy a built JAR

若要从在本地计算机上构建的 JAR 进行部署,请确保构建生成 fat-JARTo deploy from a JAR built on your local machine, ensure that your build produces a fat-JAR.

将 fat-JAR 部署到活动部署To deploy the fat-JAR to an active deployment

az spring-cloud app deploy -n <app-name> --jar-path <path-to-fat-JAR e.g. "target\hellospring-0.0.1-SNAPSHOT.jar">

将 fat-JAR 部署到特定部署To deploy the fat-JAR to a specific deployment

az spring-cloud app deployment create --app <app-name> -n <deployment-name> --jar-path <path-to-fat-JAR e.g. "target\hellospring-0.0.1-SNAPSHOT.jar">

从源代码进行部署Deploy from source code

Azure Spring Cloud 使用 kpack 构建项目。Azure Spring Cloud uses kpack to build your project. 可以使用 Azure CLI 上传源代码,使用 kpack 构建项目,并将其部署到目标应用程序。You can use Azure CLI to upload your source code, build your project using kpack, and deploy it to the target application.

警告

项目只能生成一个 JAR 文件,该文件在 target(适用于 Maven 部署)或 build/libs(适用于 Gradle 部署)的 MANIFEST.MF 中具有 main-class 条目。The project must produce only one JAR file with a main-class entry in the MANIFEST.MF in target (for Maven deployments) or build/libs (for Gradle deployments). 多个带有 main-class 条目的 JAR 文件将导致部署失败。Multiple JAR files with main-class entries will cause the deployment to fail.

对于单模块 Maven/Gradle 项目:For single module Maven / Gradle projects:

cd <path-to-maven-or-gradle-source-root>
az spring-cloud app deploy -n <app-name>

对于包含多个模块的 Maven/Gradle 项目,请对每个模块重复以下步骤:For Maven / Gradle projects with multiple modules, repeat for each module:

cd <path-to-maven-or-gradle-source-root>
az spring-cloud app deploy -n <app-name> --target-module <relative-path-to-module>

显示部署日志Show deployment logs

使用以下命令查看 kpack 生成日志:Review the kpack build logs using the following command:

az spring-cloud app show-deploy-log -n <app-name> [-d <deployment-name>]

备注

如果部署是使用 kpack 从源生成的,则 kpack 日志将仅显示最新部署。The kpack logs will only show the latest deployment if that deployment was built from source using kpack.

将公共终结点分配到网关Assign a public endpoint to gateway

  1. 打开“应用程序仪表板”页。Open the Application Dashboard page.
  2. 选择 gateway 应用程序以显示“应用程序详细信息”页。Select the gateway application to show the Application Details page.
  3. 选择“分配终结点”,将一个公共终结点分配到网关。Select Assign endpoint to assign a public endpoint to gateway. 这可能需要几分钟的时间。This can take a few minutes.
  4. 在浏览器中输入分配的公共 IP 以查看正在运行的应用程序。Enter the assigned public IP into your browser to view your running application.

后续步骤Next steps

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

  • 预配服务实例Provision a service instance
  • 为实例设置配置服务器Set a configuration server for an instance
  • 在本地构建微服务应用程序Build a microservices application locally
  • 部署每个微服务Deploy each microservice
  • 编辑应用程序的环境变量Edit environment variables for applications
  • 为应用程序网关分配公共 IPAssign public IP for your application gateway

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