你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
注意
基本、标准和企业计划将从 2025 年 3 月中旬开始弃用,停用期为 3 年。 建议转换到 Azure 容器应用。 有关详细信息,请参阅 Azure Spring Apps 停用公告。
标准消耗和专用计划将于 2024 年 9 月 30 日开始弃用,并在六个月后完全关闭。 建议转换到 Azure 容器应用。 有关详细信息,请参阅将 Azure Spring Apps 标准消耗和专用计划迁移到 Azure 容器应用。
本文适用于: ❎ 基本计划/标准计划 ✅ 企业计划
本文介绍在将 Azure Spring Apps 企业计划与 Java 应用程序配合使用时如何从应用程序配置服务 (ACS) 迁移到 Spring Cloud Config Server。
Spring Cloud Config Server 提供集中式配置服务,应用程序可以使用该服务从外部源(如 Git 存储库)提取配置属性。
预配 Spring Cloud Config Server
如果有一个启用了应用程序配置服务的 Azure Spring Apps 企业计划实例,则从 ACS 迁移到 Spring Cloud Config Server 的第一步是在 Azure Spring Apps 环境中预配 Config Server。 可以使用 Azure 门户或 Azure CLI 对其进行预配。
使用以下步骤预配 Spring Cloud Config Server:
- 在 Azure 门户中导航到 Azure Spring Apps 企业实例。
- 在菜单中,选择“Spring Cloud Config Server”。
- 选择“管理”以查看是否启用了 Spring Cloud Config Server。 如果没有,请启用它,然后选择“应用”进行保存。
- 成功更新后,可以在“概述”选项卡中看到 Config Server 的“预配状态”为“已成功”。
配置 Spring Cloud Config Server
在预配 Spring Cloud Config Server 后,下一步是为应用程序配置它以确保顺利迁移。
使用以下步骤在 Azure 门户中配置 Spring Cloud Config Server:
在 Azure Spring Apps 实例中,导航到“Spring Cloud Config Server”。
在“设置”选项卡中,将 ACS 的所有存储库的配置映射到 Spring Cloud Config Server。
如果在 ACS 中只有一个存储库,请将其映射到 Spring Cloud Config Server 的默认存储库,而无需名称和模式。 如果在 ACS 中有多个存储库,请选择一个存储库作为 Spring Cloud Config Server 的默认存储库,并将其他存储库用作附加存储库。 将属性(包括
uri
、label
、search path
、name
和authentication
)从 ACS 迁移到 Spring Cloud Config Server。映射配置后,请选择“验证”以验证配置。
验证成功后,请选择“应用”以完成 Spring Cloud Config Server 的配置。
若要应用更改,请在“应用绑定”选项卡中,选择“绑定应用”,然后选择所有应用以使用 Spring Cloud Config Server。
若要迁移 ACS 的属性 pattern
,请务必确保应用的 Azure Spring Apps 名称与 Git 存储库中的配置文件名称匹配。
- 如果 Azure Spring Apps 的应用名称与配置文件的文件名匹配,Spring Cloud Config Server 会自动将具有匹配名称的配置文件应用于应用,而无需额外配置。
- 如果名称不匹配,则需要创建一个新应用,并以该名称作为配置文件名称。
有关更多配置,请参阅 Spring Cloud Config Server 文档。
更新应用程序配置
由于 ACS 和 Config Server 之间的实现机制存在差异,因此需要一些应用配置更改来适应提取配置的方式。
在预配和配置 Spring Cloud Config Server 后,需要按照以下步骤调整配置:
通过将以下必需的 Spring Cloud 配置依赖项添加到 Maven 的 pom.xml 文件或 Gradle 的 build.gradle 文件来更新 Spring Boot 依赖项。
配置配置文件。
在 ACS 中,在 Azure Spring Apps 部署中提供配置文件作为模式,而在 Spring Cloud Config Server 中,在应用程序的源代码中配置配置文件。
确保应用程序使用正确的配置文件(
dev
、prod
等),以便 Spring Cloud Config Server 可以提供特定于环境的配置。使用正确的配置属性更新应用程序中的 bootstrap.yml 或 application.yml 文件,以指向 Spring Cloud Config Server,如以下示例所示:
spring: cloud: config: profile: dev
确保 Azure Spring Apps 的应用名称与 git 存储库中的配置文件名匹配。 此外,请避免在应用程序的代码中配置
spring.application.name
。
重新部署应用程序
在本地测试应用程序后,可以在 Azure Spring Apps 中重新部署它,以通过以下 Azure CLI 命令使用 Spring Cloud Config Server:
az spring app deploy \
--name <app-name> \
--artifact-path <path-to-your-app> \
--config-file-patterns '""'
使用 --config-file-patterns '""'
参数,它会从应用程序中清除应用程序配置服务的引用。 应用程序通过 Spring Cloud Config Server 而不是应用程序配置服务使用配置。
禁用应用程序配置服务
在所有应用程序完成迁移到 Spring Cloud Config Server 后,可以将这些应用程序取消绑定到应用程序配置服务并禁用 ACS。
- 在 Azure Spring Apps 实例中,导航到“应用程序配置服务”
- 打开“应用绑定”选项卡,然后选择要取消绑定的每个绑定应用程序。
- 取消绑定所有应用程序后,请选择“管理”以禁用应用程序配置服务。
通过仔细遵循这些步骤,可以确保顺利迁移并在 Azure Spring Apps 中利用 Spring Cloud Config Server 的优势。
已知限制
将 ACS 迁移到 Spring Cloud Config Server 仅适用于 Java 应用程序,因为 ACS 使用 Kubernetes 本机 ConfigMap
管理配置。 此方法在 Kubernetes 环境中启用动态配置更新,使其适用于具有多种编程语言的不同应用程序。 Spring Cloud Config Server 主要为 Java 应用程序设计,使用 Spring Framework 功能,因此仅支持 Java 的配置管理。