2017-04-24 2 views
0

하드 코딩 된 URL을 대상에 반환하는 함수가 있습니다. 내 manifest.yml 파일에 도착 URL을 정의하고 거기에서 URL을 읽고 싶습니다.스프링 부트 응용 프로그램에서 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"}] 

가, 내가 응용 프로그램을 시작할 때 값 https://example.com를 얻을 싶어요. 나는 System.getenv("bpmrulesrepository")을 시도해 왔지만, null을 돌려줍니다.

+0

그냥 라인'bpmrulesrepository를 추가 할 수 없습니다 ? CF는 그 json을 환경에서 이름/값 쌍으로 분리하는 방법을 알 수 없습니다. –

답변

0

manifest.yml 파일은 cf cli에 응용 프로그램 배포 방법을 알려줍니다. 구성을 여기에 입력 할 수 있지만 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를 말하는 것 :

는 예를 들어, 당신의 manifest.yml 파일의 경우는 값을해야 할 것입니다

  1. TARGET_RUNTIMEtomcat
  2. destinations 값을 가지려고합니다. [ {"name":"bpmrulesrepository", "url":"https://example.com"}]

url 키를 가져 오려면 env 변수의 내용을 가져와 구문 분석 (JSON 모양) 한 다음 url 값을 읽어야합니다.

위의 주석에서 Scott이 언급 한 것처럼 환경 변수를 정의하여 응용 프로그램에서 더 쉽게 읽을 수 있도록 자신을 저장하면됩니다. 예를 들어 destinations_bpmrulesrepository_urlhttps://example.com으로 설정하면 하나의 env 변수를 읽고 URL을 사용하면됩니다.

가치가있는 내용으로 구성을 application.properties 또는 application.yml 안에 두는 것이 좋습니다. 당신은 스프링 부트 (Spring Boot) 애플리케이션을 가지고 있기 때문에 일반적으로 설정이 이루어질 것이다. 또한 Spring Boot이기 때문에 필요에 따라 환경 변수를 통해 이러한 파일의 구성 설정을 쉽게 대체 할 수 있습니다.HTTPS : // example.com` 대신`destinations` 라인의

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

-1

yaml 파일을 참조해야합니다. 스프링 부트 응용 프로그램에이 구성 클래스를 추가해야합니다.

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; 
    } 


} 

거기에서 환경 빈을 주입 할 수 있습니다.

@Autowired 
public Environment environment; 

삽입 된 bean에서 이와 같이 정의 된 등록 정보를 참조 할 수 있습니다. environment.getProperty ("thePropertyKey");

+1

manifest.yml 파일은 앱이 Cloud Foundry로 푸시 될 때 'cf'CLI에서 사용됩니다. 파일은 앱과 함께 배포되지 않으므로 앱은 런타임에 콘텐츠를 읽을 수 없습니다. 그래서이 해결책은 효과가 없을 것입니다. –