覆盖 Spring Cloud Config 中的远程属性值
1、概览 Spring Cloud Config 是 Spring Cloud 全家桶的一个子项目。它通过集中式独立的服务来管理各个服务的配置。Spring Cloud Config 拥有自己的属性管理库,但也可以集成 Git、Consul 和 Eureka 等开源项目。
在本文中,我们将了解在 Spring Cloud Config 中覆盖远程属性值的不同方法,以及 Spring 从 2.4 版本开始强制实施的限制,以及 3.0 版本中的变化。在本教程中,我们使用 Spring Boot 2.7.2。
2、创建 Spring Config Server 使用 Spring Cloud Config 创建外部化的配置服务器。
2.1、创建配置文件 在 application.properties 文件中定义的配置与所有客户端应用共享。也可以为指定应用或指定 Profile 定义特定配置。
首先,创建一个配置文件,其中包含为客户端应用提供的属性。
客户端应用命名为 baeldung,在 /resources/config 文件夹中创建 baeldung.properties 文件。
2.2、添加属性 在 baeldung.properties 文件中添加一些属性,然后在客户端应用中使用这些属性:
hello=Hello Jane Doe! welcome=Welcome Jane Doe! 还要在 resources/config/application.properties 文件中添加一个共享属性,Spring 将在所有客户端中共享该属性:
shared-property=This property is shared accross all client applications 2.