属性 使用 vault 和 consul 时的占位符解析优先级

Property placeholder resolution precedence when using vault and consul

我对使用 consul-configvault-config 时的占位符解析优先级有疑问 我使用 信息

创建了简单的应用程序

我的依赖项是:

dependencies {
compile('org.springframework.cloud:spring-cloud-starter-consul-config')
compile('org.springframework.cloud:spring-cloud-starter-vault-config')

compile('org.springframework.boot:spring-boot-starter-webflux')
compile('org.springframework.cloud:spring-cloud-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

请注意,我没有使用服务发现。

下一步我创建了 属性 foo.prop = consul(在 consul 存储中) 和 foo.prop = vault.

使用时:

@Value("${foo.prop}")
private String prop;

我得到 vault 作为输出,但是当我从 vault 中删除 foo.prop 并重新启动应用程序时,我将得到 consul.

我在不同的组合中做了几次,似乎 vault 配置比 consul 有更高的优先级。

我的问题是在哪里可以找到有关解决策略的信息。(假设我们添加为第三个 zookeeper-config)。似乎 spring-核心文档对此保持沉默。

根据我通过调试 Spring 源代码了解到的情况...现在 Vault 具有优先权。

我的调查结果: PropertySourceBootstrapConfiguration.java 负责在 bootstrap 阶段初始化所有 属性 源。在定位属性之前,它按顺序对所有 属性SourceLocators 进行排序:

AnnotationAwareOrderComparator.sort(this.propertySourceLocators);

Vault 总是 "wins" 因为 LeasingVaultPropertySourceLocator (at least this one was created during my debugging) implements PriorityOrdered interface. Instance of ConsulPropertySourceLocator has @Order(0) annotation. According to OrderComparator 的实例:PriorityOrdered 的实例是 'more important'。

如果您有另一个 PriorityOrdered 属性 来源(例如自定义来源),您可以通过为 Vault 设置 spring.cloud.vault.config.order 来影响此顺序。

目前没有自定义,我不知道如何在 Vault 和 Consul 之间更改优先级。