2017-05-22 20 views
8

當我訪問應用程序的/ env端點時,Spring Cloud Config Server接受多個配置文件並返回所有配置文件的屬性。響應列出特定於每個配置文件的屬性。如果在兩個不同的屬性文件中存在相同的屬性,則最後定義的屬性優先。有沒有辦法獲得應用程序將使用的屬性鍵和值的最終列表?列出屬性的最終列表 - Spring Cloud Config Server

+0

我非常懷疑有這樣的事情。沒有遇到任何。但是,獲得這個開箱即用,並得到這個問題的答案會很棒。 –

+0

感謝您的更新,@GrinishNepal! –

回答

3

這似乎是Spring框架的有意限制。

here

你可以破解它並注入PropertySources接口,然後在所有的個人PropertySource對象循環,但你必須知道你在找什麼屬性。

4

對於雲配置客戶端應用程序

我嘗試不同的方法,發現如下(不小心):配置屬性

對於雲配置服務器應用程序的

GET /env/.*回報完整列表

事實證明,這已經實施,但沒有文件記錄湖所有你需要的是根據圖案要求jsonymlproperties

/{application}-{profile}.{ext} 
/{label}/{application}-{profile}.{ext} 
3
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; 
    } 

} 

對不起...這是我第一次來回答問題。我創建了一個專門針對 回答這個問題的帳號,因爲我在研究相同問題時遇到了這個問題。我發現了一個爲我工作並決定分享它的 解決方案。

這裏不用我的做了什麼解釋:

  1. 我初始化一個新的「屬性」對象(可能是一個HashMap或其他任何你想要的)

  2. 我查找的屬性源這是一個CompositePropertySource對象的「bootstrapProperties」。 此屬性源包含加載的所有應用程序屬性。

  3. 我循環遍歷CompositePropertySource對象 上的「getPropertyNames」方法返回的所有屬性名稱,並創建一個新的屬性條目。

  4. 我返回屬性對象。

+0

請注意:代碼只有答案是不鼓勵的。它總是更好地添加一些層次的解釋。 – GhostCat

+0

我沒有看到所有的屬性。例如'bootstrap.yml'中的'logging.config'。但是,我看到它使用執行器。 –

+0

謝謝@Todd Jones! –

2
  • Externalized Configuration
  • 春季啓動,您可以外部化配置,所以你可以在不同環境中的同一個應用程序代碼的工作。您可以使用屬性文件,YAML文件,環境變量和命令行參數來外部化配置。屬性值可以使用@Value註釋直接注入到bean中,可以通過Spring的Environment抽象來訪問,也可以通過@ConfigurationProperties綁定到結構化對象。

    Spring Boot使用非常特殊的PropertySource命令,該命令旨在允許明智地重寫值。 屬性按以下順序考慮:

    • 在你的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指定)。

    下面的程序從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); 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
    } 
    
    +0

    謝謝Sudhakar。我會試試這個。 –

    相關問題