将 Spring Cloud Config Server 连接到本地 Git 存储库

17 Mar 2025 | 4 分钟阅读

在本节中,我们将学习如何将 spring-cloud-config-server 连接到本地 git 仓库。 首先,我们将找到文件夹路径。

右键单击 git-localconfig-repo -> 属性 -> 复制 位置 标签地址并将其粘贴到 application.properties 文件中。

在 SpringCloudConfigServerApplication.java 文件中添加注解 @EnableConfigServer

在浏览器中输入以下 URL

localhost:8888/limits-service/default

输出

在此,我们建立了 SprinCloudConfigServerGit 仓库 之间的连接。

我们可以看到它显示了一组属性和值。它还会检索属性文件的文件名,这些值(最小值和最大值)是从该文件检索的。

SpringCloudConfigServer 的重要之处在于,它存储多个服务的配置。它还可以存储每个服务在不同环境下的配置。

Connect Spring Cloud Config Server to Local Git Repository

在上图中,有三个服务 CurrencyCalculationServiceCurrencyExchangeServiceLimitsService。 LimitsService 有四个环境服务 Dev、QA、StageProduction。 我们可以在 SpringCloudConfigServer 中配置这三个服务。

在 Git 仓库中配置多个环境

服务 Dev、QA、StageProduction。 我们可以在 SpringCloudConfigServer 中配置这三个服务。

在 Git 仓库中配置多个环境

在 spring-cloud-config-server 项目中,我们添加了一个指向 git-localconfig-repo 的链接,其中包含 limits-service.properties 文件。 它成为 limits-service 的默认配置。

但是,我们可以为特定环境覆盖它们。要覆盖这些值,请复制 limits-service.properties 并将其粘贴到文件夹 git-localconfig-repo 中,并将其重命名为 limits-service-dev.properties。 现在更新最小值和最大值。

再次复制相同的文件并将其粘贴到同一文件夹中。 将其重命名为 limits-service-qa.properties。 现在更新最小值和最大值。

如果我们想选择最大值的默认值而不是修改后的值,请在语句的开头放置一个 introduction-to-currency-conversion-and-currency-exchange-service 符号。 现在,第二条语句变成注释。

当我们执行它时,它会从默认属性文件中选取最大值 888,而不是最大值 111。每当我们更改文件时,请将更改提交到本地仓库。

现在打开 Git Bash 并执行以下命令

创建我们要添加文件的目录。

将文件添加到 Git 仓库中。

现在检查要提交的文件的状态。

Connect Spring Cloud Config Server to Local Git Repository

现在提交更改

Connect Spring Cloud Config Server to Local Git Repository

现在我们可以访问属性 Dev 和 QA。

在浏览器的地址栏中输入以下内容。

输出

我们可以观察到它正在检索属性源。 这些属性列表具有优先级列表。 最高优先级是在 QA 文件中配置的任何值。

如果 QA 文件中不存在某个值,则将从默认文件中选取该值。 因此,QA 文件中的任何内容都具有最高优先级。

将 limits-service 连接到 Spring Cloud Config Server

在本节中,我们将连接 limits-service 以从 spring-cloud-config-server 中选取配置。 我们不需要在 application.properties 文件中配置值。 移动到 limits-service 项目并将 application.properties 文件重命名为 bootstrap.properties。 我们不需要在 bootstrap.properties 中配置值。 所有配置值都从 spring-cloud-config-server 中选取。 在 bootstrap.properties 中指定 URI。

limits-service 是 bootstrap.properties 的关键路径。 基于应用程序名称,我们将从本地 Git 仓库中选取值。 现在重新启动 LimitsServiceApplication.java.

配置 Limit Service 的 Profile

此处要理解的关键点是 limits-service 的所有配置都来自 Git 仓库。 我们没有在 limits-service 中配置任何内容。 在 Git 仓库中配置内容的优势在于,limits-service 的整个配置与 limits-service 的部署分离。 它将自动从 Git 仓库中选取。

现在打开 bootstrap.properties 并将 dev profile 添加到其中。

当我们运行 limits 时,它会显示以下输出

如果我们查看 limits-service-dev.properties 文件,这些值是从那里获取的。

假设我们想从 limits-service.properties 中选择最大值,并从 limits-service-dev.properties 中选择最小值,然后从 limits-service-dev.properties 中删除最大值。 limits-service-dev.properties 文件如下所示

现在使用以下命令提交更改

Connect Spring Cloud Config Server to Local Git Repository

现在启动 LimitsServiceApplication.java。 当我们启动 LimitsServiceApplication 时,它会从 SpringCloudConfigServer 中选取值。 我们可以观察到它从 limits-service.properties (默认服务) 中选取最大值,即 888,并从 limit-service-dev.properties 中选取最小值,即 1. 但是,我们已经覆盖了默认服务的最小值。

让我们看看当我们更改 profile devqa 时会发生什么。 打开 bootstrap.properties 并在 dev 的位置写入 qa。 应用程序将启动并选取更改。 现在执行 limits.

输出

{
maximum: 222,
minimum: 2
}

这些值来自 qa 环境配置。