【问题标题】:List final list of properties - Spring Cloud Config Server列出属性的最终列表 - Spring Cloud Config Server
【发布时间】:2017-10-22 15:58:08
【问题描述】:

当我访问应用程序的 /env 端点时,Spring Cloud Config Server 接受多个配置文件并返回所有配置文件的属性。响应列出了特定于每个配置文件的属性。如果相同的属性存在于 2 个不同的属性文件中,则最后定义的属性优先。有没有办法获取应用程序将使用的属性键和值的最终列表?

【问题讨论】:

  • 我非常怀疑是否有类似的东西。一个都没遇到但是开箱即用并得到这个问题的答案会很棒。
  • 感谢您的更新,@GrinishNepal!

标签: spring-boot spring-cloud-netflix spring-cloud-config


【解决方案1】:

对于云配置客户端应用程序

我尝试了不同的方法并发现了以下(意外):

GET /env/.* 返回配置属性的完整列表

对于云配置服务器应用程序

事实证明这已经实现,但没有很好地记录。您只需根据模式请求jsonymlproperties

/{application}-{profile}.{ext}
/{label}/{application}-{profile}.{ext}

【讨论】:

  • 要在此处添加,可以使用配置服务器配置文件中的属性management.context-path 为所有 Spring Config Server 内置端点添加前缀。因此,如果您发现内置端点不起作用,请检查您的属性以找到该属性的值。如果已设置,则使用 GET /prefixValue/env 而不仅仅是 GET /env
  • 不是“env”,它是一个变量。就我而言,它是GET <bootstrap.yml's sping application name>/*
【解决方案2】:
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. 我查找“bootstrapProperties”的属性源,它是一个 CompositePropertySource 对象。 此属性源包含已加载的所有应用程序属性。

  3. 我循环遍历从 CompositePropertySource 对象的“getPropertyNames”方法返回的所有属性名称 并创建一个新的属性条目。

  4. 我返回属性对象。

【讨论】:

  • 请注意:不鼓励仅使用代码回答。添加某种程度的解释总是更好。
  • 我没有看到所有的属性。例如 logging.config 来自 bootstrap.yml。但是,我看到它使用执行器。
  • 谢谢@Todd Jones!!
【解决方案3】:

这似乎是 Spring 框架的故意限制。

here

您可以破解它并注入 PropertySources 接口,然后遍历所有单独的 PropertySource 对象,但您必须知道要查找的属性。

【讨论】:

  • 你的最初想法是由 todd-jones 和 sudhakar 实现的,特别感谢你!
【解决方案4】:
  1. Externalized Configuration

Spring Boot 允许您将配置外部化,以便您可以在不同的环境中使用相同的应用程序代码。您可以使用属性文件、YAML 文件、环境变量和命令行参数来外部化配置。属性值可以使用 @Value 注释直接注入到您的 bean 中,通过 Spring 的 Environment 抽象访问或通过 @ConfigurationProperties 绑定到结构化对象。

Spring Boot 使用一个非常特殊的 PropertySource 顺序,旨在允许明智地覆盖值。 按以下顺序考虑属性:

  • 您的主目录上的 Devtools 全局设置属性(当 devtools 处于活动状态时为 ~/.spring-boot-devtools.properties)。
  • 您的测试中的@TestPropertySource 注释。
  • @SpringBootTest#properties 注释属性添加到您的测试中。
  • 命令行参数。
  • 来自 SPRING_APPLICATION_JSON 的属性(嵌入在环境变量或系统属性中的内联 JSON)
  • ServletConfig 初始化参数。
  • ServletContext 初始化参数。
  • 来自 java:comp/env 的 JNDI 属性。
  • Java 系统属性 (System.getProperties())。
  • 操作系统环境变量。
  • 仅具有随机属性的 RandomValuePropertySource。*。
  • 打包 jar 之外的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)
  • 打包在您的 jar 中的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)
  • 打包 jar 之外的应用程序属性(application.properties 和 YAML 变体)。
  • 应用程序属性打包在您的 jar 中(application.properties 和 YAML 变体)。
  • @Configuration 类上的@PropertySource 注释。
  • 默认属性(使用 SpringApplication.setDefaultProperties 指定)。

下面的程序从 spring boot 环境打印属性。

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。我会试试这个。
猜你喜欢
  • 2019-03-26
  • 1970-01-01
  • 2019-09-03
  • 2015-05-14
  • 2015-08-28
  • 2016-07-30
  • 2021-02-01
  • 2021-03-22
  • 2016-06-04
相关资源
最近更新 更多