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

How to Read destination in manifest.yml in Spring boot application?

我有一个 returns 硬编码 URL 到目的地的功能。我想在我的 manifest.yml 文件中定义目标 URL 并从那里读取 URL。

这是我的 manifest.yml:

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"}]

我想在启动应用程序时从该文件中获取值 https://example.com。我一直在尝试 System.getenv("bpmrulesrepository"),但它正在返回 null

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

 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.getProperty("thePropertyKey");

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

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

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"}]

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

  1. TARGET_RUNTIME 将具有值 tomcat
  2. destinations 将具有值 [ {"name":"bpmrulesrepository", "url":"https://example.com"}]

因此,如果你想获得 url 密钥,你需要首先获取 destinations 环境变量的内容,解析它(看起来像 JSON)和然后读取 url.

的值

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

为了它的价值,您可能还想将您的配置放在 application.propertiesapplication.yml 中。您有一个 Spring 启动应用程序,因此这通常是您配置的位置。另外,因为它是 Spring 引导,所以如果需要,您可以通过环境变量轻松地覆盖这些文件的配置设置。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html