Programming

스프링 부트에서 application.yml 또는 bootstrap.yml에 속성을 넣는 것의 차이점은 무엇입니까?

procodes 2020. 5. 12. 20:22
반응형

스프링 부트에서 application.yml 또는 bootstrap.yml에 속성을 넣는 것의 차이점은 무엇입니까?


스프링 부트에서 application.yml 또는 bootstrap.yml에 속성을 넣는 것의 차이점은 무엇입니까? logging.config의 경우 응용 프로그램이 다르게 작동합니다.


나는 방금 Spring Cloud남자들 에게 물었고 나는 여기에있는 정보를 공유해야한다고 생각했다.

bootstrap.yml전에로드 application.yml됩니다.

일반적으로 다음에 사용됩니다.

  • 봄 클라우드 구성 서버를 사용하는 경우, 당신은 지정해야합니다 spring.application.namespring.cloud.config.server.git.uri내부bootstrap.yml
  • 일부 encryption/decryption정보

기술적 bootstrap.yml으로 부모 Spring에 의해로드됩니다 ApplicationContext. 해당 부모는를 사용하는 부모 ApplicationContext보다 먼저로드 application.yml됩니다.


bootstrap.yml 또는 bootstrap.properties

Spring Cloud를 사용 하고 있고 애플리케이션 구성이 원격 구성 서버 (예 : Spring Cloud Config 서버)에 저장된 경우에만 사용 / 필요합니다 .

설명서에서 :

Spring Cloud 애플리케이션은 기본 애플리케이션의 상위 컨텍스트 인 "부트 스트랩"컨텍스트를 작성하여 작동합니다. 기본적으로 외부 소스에서 구성 속성을로드 하고 로컬 외부 구성 파일의 속성을 해독해야합니다.

참고 그 bootstrap.yml또는 bootstrap.properties 수있는 추가 구성 (예 : 기본값)하지만, 일반적으로는 여기 부트 스트랩 설정을 둘 필요가 포함되어 있습니다.

일반적으로 두 가지 속성이 있습니다.

  • 구성 서버의 위치 ( spring.cloud.config.uri)
  • 응용 프로그램 이름 ( spring.application.name)

시작시 Spring Cloud는 애플리케이션 이름으로 구성 서버를 HTTP 호출하고 해당 애플리케이션의 구성을 다시 검색합니다.

application.yml 또는 application.properties

표준 응용 프로그램 구성을 포함합니다. 부트 스트랩 프로세스 중에 검색된 구성이 여기에 정의 된 구성보다 우선하기 때문에 일반적으로 기본 구성입니다.


이 답변은 매우 아름답게 "책에서 설명하고있다 자바 개발자 (봄 부팅, 봄 클라우드, 클라우드 네이티브 응용 프로그램)의 경우, Microservices의 인터뷰 질문 에 의해 Munish Chandel , 버전 1.30, 2018년 3월 25일.

다음 내용은이 책에서 가져 왔으며이 답변에 대한 총 학점은 책의 저자 즉 Munish Chandel에게 있습니다.

application.yml

application.yml / application.properties 파일은 스프링 부트 애플리케이션에 따라 다릅니다. 애플리케이션의 외부 속성 위치를 변경하지 않으면 스프링 부트는 항상 다음 위치에서 application.yml로드 합니다 .

/src/main/resources/application.yml

이 파일에 응용 프로그램의 모든 외부 속성을 저장할 수 있습니다. 모든 Spring Boot 프로젝트에서 사용할 수있는 일반적인 속성은 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 에서 확인할 수 있습니다. 응용 프로그램 요구에 따라. 샘플 파일은 다음과 같습니다.

spring:
    application:
        name: foobar
    datasource:
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost/test
server:
    port: 9000

bootstrap.yml

반면 bootstrap.ymlspring-cloud-config 에만 적용 되며 응용 프로그램 전에로드 됩니다.

bootstrap.yml 은 스프링 클라우드를 사용하고 마이크로 서비스 구성이 원격 스프링 클라우드 구성 서버에 저장된 경우에만 필요합니다.

bootstrap.yml에 대한 중요 사항

  1. Spring Cloud Config 서버와 함께 사용하는 경우 아래 속성을 사용하여 application-name 및 config git 위치를 지정해야합니다.

spring.application.name : "애플리케이션 이름"
spring.cloud.config.server.git.uri : "git-uri-config"

  1. 마이크로 서비스 (클라우드 구성 서버 이외)와 함께 사용하는 경우 아래 속성을 사용하여 구성 서버의 응용 프로그램 이름과 위치를 지정해야합니다.

spring.application.name : 
spring.cloud.config.uri : 
  1. 이 특성 파일에는 eureka 서버 위치, 암호화 / 암호 해독 관련 특성과 같은 Spring Cloud 환경과 관련된 기타 구성이 포함될 수 있습니다.

Upon startup, Spring Cloud makes an HTTP(S) call to the Spring Cloud Config Server with the name of the application and retrieves back that application’s configuration.

application.yml contains the default configuration for the microservice and any configuration retrieved (from cloud config server) during the bootstrap process will override configuration defined in application.yml


Bootstrap.yml is used to fetch config from the server. It can be for a Spring cloud application or for others. Typically it looks like:

spring:
  application:
    name: "app-name"
  cloud:
    config:
      uri: ${config.server:http://some-server-where-config-resides}

When we start the application it tries to connect to the given server and read the configuration based on spring profile mentioned in run/debug configuration. bootstrap.yml loads the first

If the server is unreachable application might even be unable to proceed further. However, if configurations matching the profile are present locally the server configs get overridden.

Good approach:

Maintain a separate profile for local and run the app using different profiles.


Just my 2 Cents here ..

Bootstrap.yml or Bootstrap.properties is used to fetch the config from Spring Cloud Server.

For Example, in My Bootstrap.properties file I have the following Config

spring.application.name=Calculation-service
spring.cloud.config.uri=http://localhost:8888

On starting the application , It tries to fetch the configuration for the service by connecting to http://localhost:8888 and looks at Calculation-service.properties present in Spring Cloud Config server

You can validate the same from logs of Calcuation-Service when you start it up

INFO 10988 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888


Well, I totally agree with answers already exist on this point:

  • bootstrap.yml is used to save parameters that point out where the remote configuration is and Bootstrap Application Context is created with these remote configuration.

Actually, it is also able to store normal properties just the same as what application.yml do. But pay attention on this tricky thing:

  • If you do place properties in bootstrap.yml, they will get lower precedence than almost any other property sources, including application.yml. As described here.

Let's make it clear, there are two kinds of properties related to bootstrap.yml:

  • Properties that are loaded during the bootstrap phase. We use bootstrap.yml to find the properties holder (A file system, git repository or something else), and the properties we get in this way are with high precedence, so they cannot be overridden by local configuration. As described here.
  • Properties that are in the bootstrap.yml. As explained early, they will get lower precedence. Use them to set defaults maybe a good idea.

So the differences between putting a property on application.yml or bootstrap.yml in spring boot are:

  • Properties for loading configuration files in bootstrap phase can only be placed in bootstrap.yml.
  • As for all other kinds of properties, place them in application.yml will get higher precedence.

참고URL : https://stackoverflow.com/questions/32997352/what-is-the-difference-between-putting-a-property-on-application-yml-or-bootstra

반응형