当尝试从多个源获取属性时,如何从Spring Cloud Config Server获得一组稳定的属性



我需要从Spring云配置服务器获取一组稳定的属性。示例:我配置了多个,比如spring.profiles.active=jdbc,git。因此,根据优先级,云配置服务器从jdbc中获取属性,即从属性表中获取,稍后它将从git存储库中获取。org.springframework.cloud.config.server.environment.CompositeEnvironmentRepository#findOne(java.lang.String、java.lang.Sstring、java.lang.String、boolean(方法正在合并到包含List propertySources的environment对象中。在这种情况下,列表是多个PropertySource对象。问题陈述:我在属性表(JDBC后端(中有一个名为app.produller.timeout=9000的属性,git-profile.properties文件中也存在与app.produler.timeout=9001相同的属性。如果您看到最终的Environment对象,它将在两个PropertySources中都具有属性。我知道合并将在云配置客户端通过准备引导属性源来完成。

有没有什么方法可以从Spring Cloud Config Server本身准备或获得一组稳定的propertySource?即列表中的单个propertySource对象。

注意:在上面的例子中,我只提到了一个属性,但我希望对发送到云配置客户端的所有属性进行合并。

感谢@ryanjbaxter提供解决方案。CustomCompositeEnvironmentRepository应扩展CompositeEnviironmentRepository。

然后在您的配置类中添加@AutoConfigureBefore(EnvironmentRepositoryConfiguration.class)

您的bean应该定义如下

@Bean
@Primary
@ConditionalOnMissingBean(SearchPathLocator.class)
public CompositeEnvironmentRepository customCompositeEnvironmentRepository() {
return new CustomCompositeEnvironmentRepository(this.environmentRepos, properties.isFailOnCompositeError());
}

完整的解决方案可在以下GitHub位置获得:链路

最新更新