个性化阅读
专注于IT技术分析

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

点击下载

本文概述

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

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

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

在浏览器中键入以下URL:

本地主机:8888 / limits-service /默认

输出

{
name: "limits-service", -profiles: [
"default"
], label: null, version:"0898c54ae1deb62733728e37e4c7962f529ee9ad", state: null, -propertySources: [
- {
name: C:\Users\Anubhav\git-localconfig-repo\limits-service.properties", -source: {
limits-service-minimum: "8", limits-service-maximum: "88"
}
}
]
}

在此, 我们建立了SprinCloudConfigServer和Git存储库之间的连接。

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

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

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

在上图中, 有三个服务CurrencyCalculationService, CurrencyExchangeService和LimitsService。 LimitsService具有四个环境服务Dev, QA, Stage和Production。我们可以在SpringCloudConfigServer中配置这三个服务。

在Git仓库中配置多个环境

服务开发, 质量检查, 阶段和生产。我们可以在SpringCloudConfigServer中配置这三个服务。

在Git仓库中配置多个环境

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

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

limits-service.minimum=1
limits-service.maximum=111

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

limits-service.minimum=2
limits-service.maximum=222

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

limits-service.minimum=1
introduction-to-currency-conversion-and-currency-exchange-servicelimits-service.maximum=111

当我们执行它时, 它将从默认属性文件中获取最大值888, 而不是最大值111。无论何时在文件中进行更改, 都应在本地存储库中提交更改。

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

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

cd git-localconfig-repo

将文件添加到Git存储库中。

git add -A

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

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

现在提交更改

git commit -m "Dev and QA"
将Spring Cloud Config Server连接到本地Git存储库

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

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

localhost:8888/limits-service/qa

输出

{
name: "limits-service", -profiles: [
"qa"
], label: null, version:"0898c54ae1deb62733728e37e4c7962f529ee9ad", state: null, -propertySources: [
- {
name: C:\Users\Anubhav\git-localconfig-repo\limits-service-qa.properties", -source: {
limits-service-minimum: "2", limits-service-maximum: "222"
}
}, -{
name: C:\Users\Anubhav\git-localconfig-repo\limits-service.properties?, -source: {
limits-service-minimum: "8", limits-service-maximum: "888"
}
}
]
}

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

如果质量检查文件中没有该值, 则将从默认文件中提取该值。因此, 质量检查文件中的所有内容都具有最高的属性。

将限制服务连接到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。

spring.application.name=limits-service
spring.cloud.config.uri=http://localhost:8888

限制服务是bootstrap.properties的关键路径。根据应用程序名称, 我们将从本地Git存储库中获取值。现在重新启动LimitsServiceApplication.java。

Fetching config from the server at http://localhost:8888
Located environment: name=limits-service, profiles=[default], label= null, version="0898c54ae1deb62733728e37e4c7962f529ee9ad", state=null, 

配置限制服务的配置文件

这里要理解的是, limits-service的所有配置都来自Git存储库。我们没有在limits-service中配置任何内容。在Git存储库中配置内容的优势在于, 限制服务的整个配置与限制服务的部署是分开的。它将自动从Git存储库中提取。

现在打开bootstrap.properties并将dev配置文件添加到其中。

spring .profile.active=dev

当我们运行限制时, 它显示以下输出:

{
maximum: 111, minimum:1
}

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

假设我们要从limits-service.properties中选择一个最大值, 然后从limits-service-dev.properties中选择一个最小值, 然后从limits-service-dev.properties中选择一个最大值。 limits-service-dev.properties文件如下所示:

limits-service-minimum: 1

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

git add *;
git status
git commit -m "removed configuration for maximum "
将Spring Cloud Config Server连接到本地Git存储库

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

让我们看看将配置文件dev更改为qa时会发生什么。打开bootstrap.properties并在开发位置写qa。该应用程序将启动并获取更改。现在执行限制。

输出

{
maximum: 222, minimum: 2
}

这些是来自qa环境配置的值。


赞(0)
未经允许不得转载:srcmini » Spring Cloud Config Server连接到本地Git存储库

评论 抢沙发

评论前必须登录!