有没有办法覆盖通过Spring Cloud Config Server与其他属性源(特别是系统环境)设置的属性?我知道我可以通过循环访问Environment
对象的PropertySource
来手动执行此操作,但是如果我可以将其设置为bootstrapConfig
来源为最低优先级,那将是理想的。覆盖Spring Cloud配置值与环境
2
A
回答
1
FWIW,我通过编写一个自定义的ApplicationListener
来完成这个任务,这个自定义的事件在周期早期被触发,但是在配置服务的PropertySource
被加载后。我附上了代码,以防万一有兴趣。如果有一个“官方”春办法做到这一点,我仍然有兴趣,但这个工程:
package com.example;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ConfigServicePropertyDeprioritizer
implements ApplicationListener<ApplicationPreparedEvent>
{
private static final String CONFIG_SOURCE = "bootstrap";
private static final String PRIORITY_SOURCE = "systemEnvironment";
@Override
public void onApplicationEvent(ApplicationPreparedEvent event)
{
ConfigurableEnvironment environment = event.getApplicationContext()
.getEnvironment();
MutablePropertySources sources = environment.getPropertySources();
PropertySource<?> bootstrap = findSourceToMove(sources);
if (bootstrap != null)
{
sources.addAfter(PRIORITY_SOURCE, bootstrap);
}
}
private PropertySource<?> findSourceToMove(MutablePropertySources sources)
{
boolean foundPrioritySource = false;
for (PropertySource<?> source : sources)
{
if (PRIORITY_SOURCE.equals(source.getName()))
{
foundPrioritySource = true;
continue;
}
if (CONFIG_SOURCE.equals(source.getName()))
{
// during bootstrapping, the "bootstrap" PropertySource
// is a simple MapPropertySource, which we don't want to
// use, as it's eventually removed. The real values will
// be in a CompositePropertySource
if (source instanceof CompositePropertySource)
{
return foundPrioritySource ? null : source;
}
}
}
return null;
}
}
+0
这是在配置客户端还是配置服务器中完成的? –
+0
它在客户端完成。 –
相关问题
- 1. 覆盖Spring Mongo配置与Fongo
- 2. 覆盖Spring XML配置
- 3. Erlang,覆盖环境
- 4. Spring Cloud配置服务器excample - 在应用程序配置中覆盖
- 5. 配置Spring Cloud配置
- 6. 覆盖Windows环境变量值?
- 7. Azure的功能覆盖环境值
- 8. 覆盖配置值Symfony QueueBundle
- 9. 覆盖SSIS环境变量
- 10. 在Rancher中通过环境变量覆盖Eureka配置
- 11. Spring java配置bean定义覆盖
- 12. Spring Cloud Config定制环境存储库
- 13. 在类型安全配置中使用环境变量覆盖配置
- 14. 如何覆盖Spring Cloud Ribbon中的ribbon.serverListRefreshInterval默认值?
- 15. Spring环境配置文件和JPA
- 16. Spring环境属性源配置
- 17. Spring Boot:覆盖favicon
- 18. 环境配置
- 19. 覆盖dll配置
- 20. 覆盖CORS配置
- 21. 覆盖logback配置
- 22. .zshrc.symlink覆盖配置
- 23. 为spring cloud aws配置spring redis缓存?
- 24. 覆盖Cloud Foundry使用的NewRelic配置文件java build pack
- 25. Spring的AuthenticationProcessingFilter覆盖
- 26. 覆盖配置设置
- 27. PHPUnit代码覆盖不与配置
- 28. 外部化Spring Cloud数据流配置 - Spring Cloud Config服务器
- 29. ZF2配置:覆盖数组值
- 30. 如何配置没有locations配置属性的Spring java环境?
你有没有找到一个方法来做到这一点,而无需编写自己的应用程序监听器? –
不,没有其他办法可以做到这一点,至少在Spring Boot 1.2.x中不这样做。我没有检查过1.3.x(Spring Cloud Brixton)。 –