有沒有辦法覆蓋通過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)。 –