Java 在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32997352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:08:43  来源:igfitidea点击:

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

javaspringspring-bootspring-cloud

提问by Rys

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.

在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别?在 logging.config 情况下,应用程序的工作方式不同。

采纳答案by Michael Isvy

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

我刚刚问过Spring Cloud这些人,并认为我应该分享我在这里的信息。

bootstrap.ymlis loaded before application.yml.

bootstrap.yml之前加载application.yml

It is typically used for the following:

它通常用于以下情况:

  • when using Spring Cloud Config Server, you should specify spring.application.nameand spring.cloud.config.server.git.uriinside bootstrap.yml
  • some encryption/decryptioninformation
  • 当使用 Spring Cloud Config Server 时,你应该指定spring.application.nameand spring.cloud.config.server.git.uriinsidebootstrap.yml
  • 一些encryption/decryption信息

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

从技术上讲,bootstrap.yml由父 Spring 加载ApplicationContext。该父级ApplicationContext在使用application.yml.

回答by dustin.schultz

bootstrap.ymlor bootstrap.properties

bootstrap.yml或者 bootstrap.properties

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

仅当您使用Spring Cloud并且应用程序的配置存储在远程配置服务器(例如 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.

Spring Cloud 应用程序通过创建“引导程序”上下文来运行,该上下文是主应用程序的父上下文。开箱即用,它负责从外部源加载配置属性,并解密本地外部配置文件中的属性。

Note that the bootstrap.ymlor bootstrap.propertiescancontain additional configuration (e.g. defaults) but generally you only need to put bootstrap config here.

请注意,bootstrap.ymlorbootstrap.properties可以包含其他配置(例如默认值),但通常您只需要在此处放置引导程序配置。

Typically it contains two properties:

通常它包含两个属性:

  • location of the configuration server (spring.cloud.config.uri)
  • name of the application (spring.application.name)
  • 配置服务器的位置 ( spring.cloud.config.uri)
  • 应用程序名称 ( 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.

启动时,Spring Cloud 使用应用程序的名称对配置服务器进行 HTTP 调用,并检索该应用程序的配置。

application.ymlor application.properties

application.yml或者 application.properties

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

包含标准应用程序配置 - 通常是默认配置,因为在引导过程中检索到的任何配置都将覆盖此处定义的配置。

回答by Sudip Bhandari

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:

Bootstrap.yml 用于从服务器获取配置。它可以用于 Spring 云应用程序或其他应用程序。通常它看起来像:

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

当我们启动应用程序时,它会尝试连接到给定的服务器并根据运行/调试配置中提到的 spring 配置文件读取配置。 bootstrap.yml 加载第一个

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.

为本地维护一个单独的配置文件,并使用不同的配置文件运行应用程序。

回答by dixit gangaiah

Just my 2 Cents here ..

只有我的 2 美分在这里..

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

Bootstrap.yml 或 Bootstrap.properties 用于从 Spring Cloud Server 获取配置。

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

例如,在我的 Bootstrap.properties 文件中,我有以下配置

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:8888and looks at Calculation-service.properties present in Spring Cloud Config server

在启动应用程序时,它尝试通过连接到http://localhost:8888来获取服务的配置,并查看 Spring Cloud Config 服务器中存在的 Calculation-service.properties

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

您可以在启动时从 Calcuation-Service 的日志中验证相同的内容

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

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

回答by Vaibhav Sharma

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.

这个答案已经在著作“非常漂亮解释微服务的面试问题,对于Java开发人员(春季启动,春季云,云本地应用程序)Munish Chandel,1.30版,2018年3月25日。

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

以下内容摘自本书,此答案的全部功劳归于本书的作者,即Munish Chandel

application.yml

应用程序.yml

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

application.yml/application.properties文件特定于 Spring Boot 应用程序。除非您更改应用程序的外部属性的位置,否则 Spring Boot 将始终从以下位置加载application.yml

/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.htmlYou can customize these properties as per your application needs. Sample file is shown below:

您可以在此文件中存储应用程序的所有外部属性。任何 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

引导程序.yml

bootstrap.ymlon the other hand is specific to spring-cloud-configand is loaded before the application.yml

bootstrap.yml另一方面特定于spring-cloud-config并且在application.yml之前加载

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

bootstrap.yml仅在您使用 Spring Cloud 并且您的微服务配置存储在远程 Spring Cloud 配置服务器上时才需要。

Important points about bootstrap.yml

关于 bootstrap.yml 的要点

  1. When used with Spring Cloud Config server, you shall specify the application-name and config git location using below properties.
  1. 与 Spring Cloud Config 服务器一起使用时,您应使用以下属性指定应用程序名称和配置 git 位置。
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
  1. 当与微服务(云配置服务器除外)一起使用时,我们需要使用以下属性指定配置服务器的应用程序名称和位置
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.
  1. 该属性文件可以包含与 Spring Cloud 环境相关的其他配置,例如 eureka 服务器位置、加密/解密相关属性。

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.

启动时,Spring Cloud 使用应用程序的名称对 Spring Cloud 配置服务器进行 HTTP(S) 调用,并检索该应用程序的配置。

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

application.yml 包含微服务的默认配置,并且在引导过程中检索到的任何配置(从云配置服务器)将覆盖 application.yml 中定义的配置

回答by Lebecca

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

好吧,我完全同意在这一点上已经存在的答案:

  • bootstrap.ymlis used to save parameters that point out where the remote configuration is and Bootstrap Application Contextis created with these remote configuration.
  • bootstrap.yml用于保存指出远程配置所在位置的参数,并使用这些远程配置创建Bootstrap 应用程序上下文

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

实际上,它也可以像application.ymldo一样存储普通属性。但是要注意这个棘手的事情:

  • 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.
  • 如果您确实将属性放在 中bootstrap.yml,它们的优先级将低于几乎任何其他属性源,包括 application.yml。如上所述这里

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

让我们说清楚,有两种与 相关的属性bootstrap.yml

  • Properties that are loaded during the bootstrap phase. We use bootstrap.ymlto 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.
  • 在引导阶段加载的属性。我们bootstrap.yml用来查找属性持有者(一个文件系统,git仓库或者其他什么),我们通过这种方式得到的属性具有很高的优先级,所以它们不能被本地配置覆盖。如上所述这里
  • 中的属性bootstrap.yml。如前所述,它们将获得较低的优先级。使用它们来设置默认值可能是个好主意。

So the differences between putting a property on application.ymlor bootstrap.ymlin spring boot are:

因此,在 Spring Boot上application.ymlbootstrap.ymlSpring Boot 中放置属性之间的区别是:

  • 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.ymlwill get higher precedence.
  • 在引导阶段加载配置文件的属性只能放在bootstrap.yml.
  • 对于所有其他类型的属性,将它们放入application.yml将获得更高的优先级。

回答by Hyman Beaken

Another use for bootstrap.ymlis to load configuration from kubernetes configmapand secretresources. The application must import the spring-cloud-starter-kubernetesdependency.

bootstrap.yml 的另一个用途是从 kubernetes configmap秘密资源加载配置。应用程序必须导入spring-cloud-starter-kubernetes依赖项。

As with the Spring Cloud Config, this has to take place during the bootstrap phrase.

与 Spring Cloud Config 一样,这必须在引导短语期间进行。

From the docs :

从文档:

spring:
  application:
    name: cloud-k8s-app
  cloud:
    kubernetes:
      config:
        name: default-name
        namespace: default-namespace
        sources:
         # Spring Cloud Kubernetes looks up a ConfigMap named c1 in namespace default-namespace
         - name: c1

So properties stored in the configmap resource with meta.name default-name can be referenced just the same as properties in application.yml

因此,可以像application.yml 中的属性一样引用存储在 configmap 资源中的带有 meta.name default-name 的属性

And the same process applies to secrets :

同样的过程适用于 secrets :

spring:
  application:
    name: cloud-k8s-app
  cloud:
    kubernetes:
      secrets:
        name: default-name
        namespace: default-namespace
        sources:
         # Spring Cloud Kubernetes looks up a Secret named s1 in namespace default-namespace
         - name: s1