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

前端 未结 8 779
慢半拍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

    0 讨论(0)
  • 2020-12-02 04:03

    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.

    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.

    0 讨论(0)
  • 2020-12-02 04:06

    Bootstrap.yml is the first file loaded when you start spring boot application and application.property is loaded when application starts. So, you keep, may be your config server's credentials etc., in bootstrap.yml which is required during loading application and then in application.properties you keep may be database URL etc.

    0 讨论(0)
  • 2020-12-02 04:08

    I have just asked the Spring Cloud guys and thought I should share the info I have here.

    bootstrap.yml is loaded before application.yml.

    It is typically used for the following:

    • when using Spring Cloud Config Server, you should specify spring.application.name and spring.cloud.config.server.git.uri inside bootstrap.yml
    • some encryption/decryption information

    Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that uses application.yml.

    0 讨论(0)
  • 2020-12-02 04:13

    bootstrap.yml or bootstrap.properties

    It's only used/needed if you're using Spring Cloud and your application's configuration is stored on a remote configuration server (e.g. Spring Cloud Config Server).

    From the documentation:

    A Spring Cloud application operates by creating a "bootstrap" context, which is a parent context for the main application. Out of the box it is responsible for loading configuration properties from the external sources, and also decrypting properties in the local external configuration files.

    Note that the bootstrap.yml or bootstrap.properties can contain additional configuration (e.g. defaults) but generally you only need to put bootstrap config here.

    Typically it contains two properties:

    • location of the configuration server (spring.cloud.config.uri)
    • name of the application (spring.application.name)

    Upon startup, Spring Cloud makes an HTTP call to the config server with the name of the application and retrieves back that application's configuration.

    application.yml or application.properties

    Contains standard application configuration - typically default configuration since any configuration retrieved during the bootstrap process will override configuration defined here.

    0 讨论(0)
  • 2020-12-02 04:13

    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

    0 讨论(0)
提交回复
热议问题