你当前正在访问 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 容器应用。
本文适用于: ❎ 基本计划/标准计划 ✅ 企业计划
本文介绍如何将应用程序配置服务 (ACS) 和 Config Server 迁移到 Azure 容器应用中的 Config Server for Spring。 Azure 容器应用管理 Config Server for Spring,其功能与 Azure Spring Apps 中的 ACS 或 Spring Cloud Config Server 类似。
先决条件
- 启用了应用程序配置服务的 Azure Spring Apps 企业计划实例。
- 用于 Config Server 的 Azure 容器应用环境和 Azure 容器应用实例。
预配 Config Server
以下步骤演示如何在 Azure 容器应用中预配 Config Server for Spring:
在 Azure 门户中导航到 Azure 容器应用环境。
在菜单中,选择“服务”>“服务”。
打开“配置”下拉列表,然后选择“Java 组件”。
在“配置 Java 组件”面板中,输入以下值:
属性 Value Java 组件类型 选择“Config Server for Spring”。 Java 组件名称 输入“configserver”。 在“Git 存储库”部分中,选择“添加”,然后将值从应用程序配置服务迁移到此处,如下表所示。 选择一个存储库作为 Config Server for Spring 的默认存储库。
属性 值 类型 选择“DEFAULT”。 URI 输入存储库的 URI 值。 分支名称 输入存储库的标签值。 搜索路径 输入存储库的搜索路径值。 身份验证 选择存储库的身份验证类型并输入相应的信息。 将其余字段保留为默认值,然后选择“添加”。
如果有多个存储库,请选择“添加”以迁移其他存储库。 对于“类型”,请选择“其他”,然后迁移其他属性,如下一步和下表所示:
属性 值 类型 选择“DEFAULT”。 模式 以 {application}
或{application}/{profile}
格式输入存储库的“模式”值。在“绑定”部分中,打开下拉列表以选择要绑定到 Config Server for Spring 的应用。
选择下一步。
若要设置 Config Server,请在“查看”选项卡上,选择“配置”,然后按照配置部分中的说明进行操作。
成功创建后,可以看到 Config Server for Spring 的“预配状态”为“成功”。
资源分配
Azure 容器应用中托管 Config Server 的容器资源分配固定为以下值:
- CPU:0.5 vCPU
- 内存:1 Gi
若要配置 Config Server for Spring 的实例计数,需要用相同的值更新参数 --min-replicas
和 --max-replicas
。 此配置可确保实例计数保持固定。 目前,系统不支持动态缩放的自动缩放配置。
配置 Config Server
从应用程序配置服务 (ACS) 中选择一个 Git 存储库作为 Config Server 中的默认存储库,而选择其他存储库作为附加存储库。
下表提供 ACS 中的属性与 Config Server 中的相应配置之间的映射:
ACS 中的属性名称 | CONFIGURATION_KEY |
CONFIGURATION_VALUE |
---|---|---|
name |
无需将此值映射到 Config Server。 | |
pattern |
无需将此值映射到 Config Server。 | |
uri |
spring.cloud.config.server.git.uri spring.cloud.config.server.git.repos.{repoName}.uri |
远程存储库的 URI。 |
search path |
spring.cloud.config.server.git.search-paths spring.cloud.config.server.git.repos.{repoName}.search-paths |
在本地工作副本中使用的搜索路径。 默认情况下,仅搜索根目录。 |
label |
spring.cloud.config.server.git.default-label spring.cloud.config.server.git.repos.{repoName}.default-label |
用于 Git 的标签。 |
username |
spring.cloud.config.server.git.username spring.cloud.config.server.git.repos.{repoName}.username |
如果身份验证类型为 HTTP Basic ,则用于与远程存储库进行身份验证的用户名。 |
password |
spring.cloud.config.server.git.password spring.cloud.config.server.git.repos.{repoName}.password |
如果身份验证类型为 HTTP Basic ,则用于与远程存储库进行身份验证的密码。 |
private key |
spring.cloud.config.server.git.private-key spring.cloud.config.server.git.repos.{repoName}.private-key |
如果身份验证类型为 SSH ,则为有效的 SSH 私钥。 |
host key |
spring.cloud.config.server.git.host-key spring.cloud.config.server.git.repos.{repoName}.host-key |
如果身份验证类型为 SSH ,则为有效的 SSH 主机密钥。 如果还设置了 host-key-algorithm ,则必须设置。 |
host key algorithm |
spring.cloud.config.server.git.host-key-algorithm spring.cloud.config.server.git.repos.{repoName}.host-key-algorithm |
如果身份验证类型为 SSH ,则为 ssh-dss 、ssh-rsa 、ssh-ed25519 、ecdsa-sha2-nistp256 、ecdsa-sha2-nistp384 或 ecdsa-sha2-nistp521 之一。 如果还设置了 host-key ,则必须设置。 |
有关更多 Config Server 属性,请参阅在 Azure 容器应用中连接到适用于 Spring 的托管配置服务器的配置选项部分。
例如,假设在 ACS 中有以下配置:
"settings": {
"gitProperty": {
"repositories": [
{
"name": "r1",
"patterns": [
"application"
],
"label": "master",
"uri": "https://github.com/Azure-Samples/spring-petclinic-microservices-config"
},
{
"name": "r2",
"patterns": [
"customers-service"
],
"label": "master",
"uri": "https://github.com/Azure-Samples/spring-petclinic-microservices-config"
},
{
"name": "r3",
"patterns": [
"payment"
],
"label": "main",
"uri": "https://github.com/Azure-Samples/acme-fitness-store-config"
}
]
}
}
使用此 ACS 配置,可以通过以下配置迁移到 Config Server for Spring:
spring.cloud.config.server.git.uri=https://github.com/Azure-Samples/spring-petclinic-microservices-config
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.git.repos.repo1.uri=https://github.com/Azure-Samples/acme-fitness-store-config
spring.cloud.config.server.git.repos.repo1.default-label=main
更改应用程序配置
在预配和配置 Config Server for Spring 后,请使用以下步骤调整应用程序配置以有效地使用它:
更新 Spring Boot 依赖项。 将以下 Spring Cloud 配置依赖项添加到 Maven 的 pom.xml 或 Gradle 的 build.gradle。
配置一个配置文件。 ACS 的配置文件在 Azure Spring Apps 部署中作为模式提供,而在 Config Server 中,配置文件是在应用程序的源代码中配置的。
确保应用程序使用正确的配置文件(
dev
、prod
等),以便 Config Server 可以提供特定于环境的配置。使用正确的配置属性更新应用程序中的 bootstrap.yml 或 application.yml 文件,以指向 Config Server,如以下示例所示:
spring: cloud: config: profile: dev
配置刷新间隔。 如果在 ACS 中设置刷新间隔,则还可以在 Config Server for Spring 的
spring.cloud.config.server.git.refreshRate
配置中指定相应的值。 此值确定 Config Server for Spring 从 Git 后端提取更新的配置数据的频率。若要在应用程序的代码中加载属性更改,请执行以下步骤:
- 注册计划任务以按给定的间隔刷新上下文。
- 启用
autorefresh
并在 application.yml 文件中设置适当的刷新间隔。 - 向代码添加
@RefreshScope
。
有关详细信息,请参阅刷新 Config Server。
将应用程序部署到 Azure 容器应用
在本地测试应用程序后,可以将新映像部署到 Azure 容器应用应用程序。
使用以下步骤部署:
- 在 Azure 门户中导航到 Azure 容器应用应用程序。
- 在菜单中,选择“应用程序”>“容器”。
- 选择“编辑并部署”以打开“创建和部署新的修订”页。
- 在“容器映像”部分中,选择该映像,然后选择“编辑”。
- 在“编辑容器”部分的“属性”选项卡上,选择应用程序的新映像。
- 在“环境变量”选项卡上,对于“名称”,请指定 spring.application.name。 然后,对于“源”,请选择“手动输入”并指定应用程序使用配置的配置文件名称。
- 选择“保存”以部署新修订。
疑难解答
可以使用 Log Analytics 在 Azure 容器应用中查看适用于 Spring 的托管配置服务器的日志。 请使用以下步骤:
在 Azure 门户中导航到 Azure 容器应用环境。
选择“监视”>“日志”菜单。
若要查看日志,请在
ContainerAppSystemLogs_CL
表的查询编辑器中输入查询,如以下示例所示:ContainerAppSystemLogs_CL | where ComponentType_s == "SpringCloudConfig" | project Time=TimeGenerated, ComponentName=ComponentName_s, Message=Log_s | take 100
有关查询日志的详细信息,请参阅 Azure 容器应用中托管 Java 组件的可观测性。
已知限制
将 ACS 迁移到 Config Server for Spring 仅适用于 Java 应用程序。 因为 ACS 使用 Kubernetes 本机 ConfigMap
来管理配置。 此方法在 Kubernetes 环境中启用动态配置更新,使其适用于具有多种编程语言的不同应用程序。 而 Spring Cloud Config Server 主要为 Java 应用程序设计,使用 Spring Framework 功能,因此仅支持 Java 的配置管理。