注意
基本标准计划于 2025 年 3 月 17 日进入退休期。 有关详细信息,请参阅 Azure Spring Apps 停用公告。
标准消耗和专用计划于 2024 年 9 月 30 日进入停用期,并将在 2025 年 3 月底之前完全关闭。
本文介绍如何生成 Spring 应用程序并将其部署到 Azure Spring Apps。 可以使用 Azure CLI、Maven 插件或 IntelliJ。 本文将介绍每个可选方案。
- 完成此系列中之前的快速入门:
- JDK 17
- Maven 3.0 或更高版本
- 一个 Azure 订阅。 如果你没有订阅,请在开始之前创建一个试用版订阅。
- (可选)Azure CLI 2.45.0 或更高版本。 使用以下命令安装 Azure Spring Apps 扩展:
az extension add --name spring
- (可选)Azure Toolkit for IntelliJ。
使用以下命令克隆示例存储库,导航到示例文件夹,然后生成项目。
git clone https://github.com/azure-samples/spring-petclinic-microservices
cd spring-petclinic-microservices
mvn clean package -DskipTests -Denv=cloud
编译项目需要 5 至 10 分钟时间。 编译项目后,相应的文件夹中应会包含每个服务单独的 JAR 文件。
按照以下步骤,使用 CLI 在 Azure Spring Apps 上创建和部署应用。
如果在之前的快速入门中未运行以下命令,请立即运行这些命令来设置 CLI 默认值。
az configure --defaults group=<resource-group-name> spring=<service-name>
为 PetClinic 创建两个核心 Spring 应用程序:
api-gateway
和customers-service
。az spring app create \ --name api-gateway \ --runtime-version Java_17 \ --instance-count 1 \ --memory 2Gi \ --assign-endpoint az spring app create \ --name customers-service \ --runtime-version Java_17 \ --instance-count 1 \ --memory 2Gi
部署上一步骤中生成的 JAR 文件。
az spring app deploy \ --name api-gateway \ --artifact-path spring-petclinic-api-gateway/target/api-gateway-3.0.1.jar \ --jvm-options="-Xms2048m -Xmx2048m" az spring app deploy \ --name customers-service \ --artifact-path spring-petclinic-customers-service/target/customers-service-3.0.1.jar \ --jvm-options="-Xms2048m -Xmx2048m"
在部署后通过以下命令查询应用状态。
az spring app list --output table
此命令会生成类似于以下示例的输出:
Name Location ResourceGroup Production Deployment Public Url Provisioning Status CPU Memory Running Instance Registered Instance Persistent Storage ----------------- ---------- --------------- ----------------------- --------------------------------------------------- --------------------- ----- -------- ------------------ --------------------- -------------------- api-gateway chinanorth2 xxxxxx-sp default https://<service name>-api-gateway.microservices.azure.cn Succeeded 1 2 1/1 1/1 - customers-service chinanorth2 <service name> default Succeeded 1 2 1/1 1/1 -
使用前面显示的“公共 URL”(格式为 api-gateway
)从浏览器访问 customers-service
和 https://<service name>-api-gateway.microservices.azure.cn
。
提示
若要对部署进行故障排除,可以使用以下命令在每次应用运行 az spring app logs --name <app name> --follow
时实时获取日志流式处理。
若要让 PetClinic 应用正常运行所有功能(如“管理服务器”、“访问”和“兽医”功能),请通过以下命令部署其他应用:
az spring app create \
--name admin-server \
--runtime-version Java_17 \
--instance-count 1 \
--memory 2Gi \
--assign-endpoint
az spring app create \
--name vets-service \
--runtime-version Java_17 \
--instance-count 1 \
--memory 2Gi
az spring app create \
--name visits-service \
--runtime-version Java_17 \
--instance-count 1 \
--memory 2Gi
az spring app deploy \
--name admin-server \
--runtime-version Java_17 \
--artifact-path spring-petclinic-admin-server/target/admin-server-3.0.1.jar \
--jvm-options="-Xms1536m -Xmx1536m"
az spring app deploy \
--name vets-service \
--runtime-version Java_17 \
--artifact-path spring-petclinic-vets-service/target/vets-service-3.0.1.jar \
--jvm-options="-Xms1536m -Xmx1536m"
az spring app deploy \
--name visits-service \
--runtime-version Java_17 \
--artifact-path spring-petclinic-visits-service/target/visits-service-3.0.1.jar \
--jvm-options="-Xms1536m -Xmx1536m"