當我訪問應用程序的/ env端點時,Spring Cloud Config Server接受多個配置文件並返回所有配置文件的屬性。響應列出特定於每個配置文件的屬性。如果在兩個不同的屬性文件中存在相同的屬性,則最後定義的屬性優先。有沒有辦法獲得應用程序將使用的屬性鍵和值的最終列表?列出屬性的最終列表 - Spring Cloud Config Server
回答
對於雲配置客戶端應用程序
我嘗試不同的方法,發現如下(不小心):配置屬性
對於雲配置服務器應用程序的
GET /env/.*
回報完整列表
事實證明,這已經實施,但沒有文件記錄湖所有你需要的是根據圖案要求json
,yml
或properties
:
/{application}-{profile}.{ext}
/{label}/{application}-{profile}.{ext}
import java.util.properties;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
public class MyClass {
@Autowired
private Environment env;
Properties getProperties() {
Properties props = new Properties();
CompositePropertySource bootstrapProperties = (CompositePropertySource) ((AbstractEnvironment) env).getPropertySources().get("bootstrapProperties");
for (String propertyName : bootstrapProperties.getPropertyNames()) {
props.put(propertyName, bootstrapProperties.getProperty(propertyName));
}
return props;
}
}
對不起...這是我第一次來回答問題。我創建了一個專門針對 回答這個問題的帳號,因爲我在研究相同問題時遇到了這個問題。我發現了一個爲我工作並決定分享它的 解決方案。
這裏不用我的做了什麼解釋:
我初始化一個新的「屬性」對象(可能是一個HashMap或其他任何你想要的)
我查找的屬性源這是一個CompositePropertySource對象的「bootstrapProperties」。 此屬性源包含加載的所有應用程序屬性。
我循環遍歷CompositePropertySource對象 上的「getPropertyNames」方法返回的所有屬性名稱,並創建一個新的屬性條目。
我返回屬性對象。
請注意:代碼只有答案是不鼓勵的。它總是更好地添加一些層次的解釋。 – GhostCat
我沒有看到所有的屬性。例如'bootstrap.yml'中的'logging.config'。但是,我看到它使用執行器。 –
謝謝@Todd Jones! –
- Externalized Configuration
- 在你的home目錄Devtools全局設置屬性(〜/ .spring啓動-devtools.properties時devtools有效)。
- @TestPropertySource在您的測試上的註釋。
- @ SpringBootTest#properties註釋屬性在您的測試。
- 命令行參數。
- 來自SPRING_APPLICATION_JSON的屬性(內嵌在環境變量或系統屬性中的JSON)
- ServletConfig init參數。
- ServletContext初始參數。
- 來自java:comp/env的JNDI屬性。
- Java系統屬性(System.getProperties())。
- OS環境變量。
- 僅具有隨機屬性的RandomValuePropertySource。*。
- 打包的罐子以外特定資料的應用程序的屬性(應用 - {輪廓}的.properties和YAML變體)包裝您的罐內
- 特定資料的應用程序的屬性(應用 - {輪廓}的.properties和YAML變體)
- 打包jar(application.properties和YAML變體)之外的應用程序屬性。
- 打包在jar中的應用程序屬性(application.properties和YAML變體)。
- @PropertySource @Configuration類的註釋。
- 默認屬性(使用SpringApplication.setDefaultProperties指定)。
春季啓動,您可以外部化配置,所以你可以在不同環境中的同一個應用程序代碼的工作。您可以使用屬性文件,YAML文件,環境變量和命令行參數來外部化配置。屬性值可以使用@Value註釋直接注入到bean中,可以通過Spring的Environment抽象來訪問,也可以通過@ConfigurationProperties綁定到結構化對象。
Spring Boot使用非常特殊的PropertySource命令,該命令旨在允許明智地重寫值。 屬性按以下順序考慮:
下面的程序從spring引導環境打印屬性。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.StandardServletEnvironment;
@Component
public class EnvironmentLogger extends ApplicationObjectSupport {
@Override
protected void initApplicationContext(ApplicationContext context) throws BeansException {
Environment environment = context.getEnvironment();
String[] profiles = environment.getActiveProfiles();
if(profiles != null && profiles.length > 0) {
for (String profile : profiles) {
System.out.print(profile);
}
} else {
System.out.println("Setting default profile");
}
//Print the profile properties
if(environment != null && environment instanceof StandardServletEnvironment) {
StandardServletEnvironment env = (StandardServletEnvironment)environment;
MutablePropertySources mutablePropertySources = env.getPropertySources();
if(mutablePropertySources != null) {
for (PropertySource<?> propertySource : mutablePropertySources) {
if(propertySource instanceof MapPropertySource) {
MapPropertySource mapPropertySource = (MapPropertySource)propertySource;
if(mapPropertySource.getPropertyNames() != null) {
System.out.println(propertySource.getName());
String[] propertyNames = mapPropertySource.getPropertyNames();
for (String propertyName : propertyNames) {
Object val = mapPropertySource.getProperty(propertyName);
System.out.print(propertyName);
System.out.print(" = " + val);
}
}
}
}
}
}
}
}
謝謝Sudhakar。我會試試這個。 –
- 1. 我可以從Spring Cloud Config Server獲得單個屬性值嗎?
- 2. Library Library可以從Spring Cloud Config Server讀取屬性嗎?
- 3. 如何使用Spring Boot(1.5.3.RELEASE)和Spring Cloud Config(1.3.0.RELEASE)編寫Spring Cloud Config Server?
- 4. spring cloud config searchPaths
- 5. 問題spring-cloud-config和spring-cloud-bus
- 6. Spring Cloud Config中的加密
- 7. Spring Cloud Config服務器問題
- 8. 外部化Spring Cloud數據流配置 - Spring Cloud Config服務器
- 9. Spring Cloud Config如何理解外部屬性?
- 10. Spring Cloud Config - 加密密碼
- 11. spring cloud config如何使用本地屬性覆蓋遠程屬性
- 12. 使用Spring-Cloud-Connectors配置Spring Boot以使用PWS Config-Server
- 13. Spring Cloud Config定製環境存儲庫
- 14. 反序列化最終名單屬性
- 15. Hoverfly simulationMode與單元測試中的Spring Cloud Config Server相沖突
- 16. Spring Cloud Config(Vault後端)過早提早
- 17. Spring Cloud Config Server無法在啓動時找到PropertySource
- 18. spring cloud config ssh連接失敗
- 19. 最終屬性中的Spring屬性注入@Value - Java
- 20. Spring Cloud - Config Client減慢度量/健康
- 21. 最終屬性?
- 22. 如何使用Spring對bean的列表屬性做出貢獻?
- 23. Spring Cloud Config Client - Angel.SR4 vs Boot 1.3.1?
- 24. Spring Cloud config feign fallback(CircuitBreaker)規則
- 25. Spring Cloud Config服務器 - Logback錯誤
- 26. 使用spring-cloud-dataflow時spring-cloud-stream應用中的屬性不起作用
- 27. Maven屬性列表
- 28. 從Spring MVC列表中填充HTML排序列表屬性
- 29. 如何使用spring-cloud-config-client配置自定義RestTemplate?
- 30. 過濾列表的列表python,如何創建最終列表?
我非常懷疑有這樣的事情。沒有遇到任何。但是,獲得這個開箱即用,並得到這個問題的答案會很棒。 –
感謝您的更新,@GrinishNepal! –