Java 如何在Spring引导应用程序中读取manifest.yml中的目标?

Java 如何在Spring引导应用程序中读取manifest.yml中的目标?,java,spring-boot,cloud-foundry,Java,Spring Boot,Cloud Foundry,我有一个函数,它将硬编码的URL返回到目标。我想在manifest.yml文件中定义目标URL并从中读取URL 这是我的清单。yml: 从这个文件中,我想得到这个值https://example.com 当我启动应用程序时。我一直在尝试System.getenvbpmrulesrepository,但它返回null。您需要引用yaml文件。 您应该在spring boot应用程序中添加此配置类 package xyx.partas.provider.lezzgoshop.config; im

我有一个函数,它将硬编码的URL返回到目标。我想在manifest.yml文件中定义目标URL并从中读取URL

这是我的清单。yml:


从这个文件中,我想得到这个值https://example.com 当我启动应用程序时。我一直在尝试System.getenvbpmrulesrepository,但它返回null。

您需要引用yaml文件。 您应该在spring boot应用程序中添加此配置类

 package xyx.partas.provider.lezzgoshop.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;

import java.io.File;

/**
 * Created by afernandez on 16/09/2016.
 */
@Configuration
@Slf4j
public class AppConfig {


    private static String YML_FILE = "manifest.yml";
    //Change the path
    public static final String DEFAULT_ETC_DIR = "/etc/partas-config";
    /**
     * The default etc directory can be overridden by setting the CORE_API_ETC environment variable.
     */
    public static final String CUSTOM_ETC_DIR_VAR = "CORE_API_ETC";

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {

        final String envEtcDir = System.getenv(CUSTOM_ETC_DIR_VAR);
        final String configDir = (envEtcDir == null) ? DEFAULT_ETC_DIR : envEtcDir;

        final String configFile = configDir + File.separatorChar + YML_FILE;
        log.info("Looking up configuration file: {}", configFile);
        File ymlFile = new File(configFile);
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        if (ymlFile.exists()) {
            log.info("Loading Configuration File: {}", ymlFile.getAbsoluteFile());

            yaml.setResources(new FileSystemResource(configFile));
            propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        } else {
            log.info("Using project default Configuration File");
            //yaml.setResources(new ClassPathResource("application-default.yml"));
           // propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        }

        return propertySourcesPlaceholderConfigurer;
    }


}
从那里,您可以注入环境Bean

 @Autowired
 public Environment environment;
从注入的bean中,您可以像这样引用定义的属性。 environment.getPropertythePropertyKey

manifest.yml文件告诉cf cli如何部署应用程序。您可以将配置放在这里,但这只是因为manifest.yml文件可用于定义环境变量。换句话说,manifest.yml文件可用于设置特定的环境变量,应用程序稍后可以从中读取配置值。应用程序无法直接读取manifest.yml

例如,如果这是manifest.yml文件:

然后告诉cf cli定义两个环境变量:

目标运行时,它将具有值tomcat 将具有值[{name:bpmrulesrepository,url:https://example.com}] 因此,如果要获取url键,首先需要获取destinations env变量的内容,将其解析为JSON,然后读取url的值

正如Scott在上面的评论中提到的,您可以通过定义环境变量来节省一些工作,以便应用程序更容易读取这些变量。例如,如果将destinations_bpmrulesrepository_url设置为https://example.com,那么您所需要做的就是读取一个env变量,然后就有了URL

为了实现它的价值,您可能还希望将配置放在application.properties或application.yml中。您有一个Spring Boot应用程序,因此这通常是您配置的地方。另外,由于它是Spring引导,如果需要,您可以通过环境变量轻松地覆盖这些文件中的配置设置


将应用程序推送到Cloud Foundry时,“cf”CLI将使用manifest.yml文件。该文件未与应用一起部署,因此应用无法在运行时读取其内容。因此,此解决方案不起作用。不能仅添加行bpmrulesrepository:https://example.com 而不是终点线?CF不知道如何在环境中将json拆分为名称/值对。
 @Autowired
 public Environment environment;
applications:
  - name: rule_runtime 
    buildpack: java_buildpack
    path: target/com.sap.brms.web.app.0.0.1-SNAPSHOT.war
    env:
      TARGET_RUNTIME: tomcat
      destinations: [  {"name":"bpmrulesrepository", "url":"https://example.com"}]