What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

前端 未结 8 805
慢半拍i
慢半拍i 2020-12-02 03:13

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot? In logging.config case, the application works different.

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 03:56

    This answer has been very beautifully explained in book "Microservices Interview Questions, For Java Developers (Spring Boot, Spring Cloud, Cloud Native Applications) by Munish Chandel, Version 1.30, 25.03.2018.

    The following content has been taken from this book, and total credit for this answer goes to the Author of the book i.e. Munish Chandel

    application.yml

    application.yml/application.properties file is specific to Spring Boot applications. Unless you change the location of external properties of an application, spring boot will always load application.yml from the following location:

    /src/main/resources/application.yml
    

    You can store all the external properties for your application in this file. Common properties that are available in any Spring Boot project can be found at: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html You can customize these properties as per your application needs. Sample file is shown below:

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

    bootstrap.yml

    bootstrap.yml on the other hand is specific to spring-cloud-config and is loaded before the application.yml

    bootstrap.yml is only needed if you are using Spring Cloud and your microservice configuration is stored on a remote Spring Cloud Config Server.

    Important points about bootstrap.yml

    1. When used with Spring Cloud Config server, you shall specify the application-name and config git location using below properties.
    
    spring.application.name: "application-name"
    spring.cloud.config.server.git.uri: "git-uri-config"
    
    
    1. When used with microservices (other than cloud config server), we need to specify the application name and location of config server using below properties
    
    spring.application.name: 
    spring.cloud.config.uri: 
    1. This properties file can contain other configuration relevant to Spring Cloud environment for e.g. eureka server location, encryption/decryption related properties.

    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

提交回复
热议问题