2017-05-22 21 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。我会试试这个。 –

    相关问题