在 application.yml 上放置 属性 或在 spring 引导中放置 bootstrap.yml 有什么区别?
What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?
在 application.yml 上放置 属性 或在 spring 引导中放置 bootstrap.yml 有什么区别?
在 logging.config 情况下,应用程序的工作方式不同。
我刚问过 Spring Cloud
个人,我想我应该在这里分享我的信息。
bootstrap.yml
在 application.yml
之前加载。
通常用于以下情况:
- 使用Spring Cloud Config Server时,需要在
bootstrap.yml
中指定spring.application.name
和spring.cloud.config.server.git.uri
- 一些
encryption/decryption
信息
从技术上讲,bootstrap.yml
由父 Spring ApplicationContext
加载。那个父 ApplicationContext
在使用 application.yml
.
的那个之前加载
bootstrap.yml
或 bootstrap.properties
只有 used/needed 如果您使用 Spring Cloud 并且您的应用程序配置存储在远程配置服务器(例如 Spring 云配置服务器)。
来自文档:
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.
请注意 bootstrap.yml
或 bootstrap.properties
可以 包含其他配置(例如默认值),但通常您只需要将 bootstrap 配置这里。
通常它包含两个属性:
- 配置服务器的位置(
spring.cloud.config.uri
)
- 应用程序名称 (
spring.application.name
)
启动时,Spring云使用应用程序名称对配置服务器进行 HTTP 调用,并检索回该应用程序的配置。
application.yml
或 application.properties
包含标准应用程序配置 - 通常是默认配置,因为在 bootstrap 过程中检索到的任何配置都将覆盖此处定义的配置。
Bootstrap.yml 用于从服务器获取配置。它可以用于 Spring 云应用程序或其他应用程序。通常它看起来像:
spring:
application:
name: "app-name"
cloud:
config:
uri: ${config.server:http://some-server-where-config-resides}
当我们启动应用程序时,它会尝试连接到给定的服务器并根据 run/debug 配置中提到的 spring 配置文件读取配置。
如果服务器无法访问,应用程序甚至可能无法继续进行。但是,如果本地存在与配置文件匹配的配置,则服务器配置将被覆盖。
好的方法:
为本地和 运行 使用不同配置文件的应用维护单独的配置文件。
这里只是我的 2 美分..
Bootstrap.yml 或 Bootstrap.properties 用于从 Spring 云服务器获取配置。
例如,在我的 Bootstrap.properties 文件中,我有以下配置
spring.application.name=Calculation-service
spring.cloud.config.uri=http://localhost:8888
启动应用程序时,它会尝试通过连接到 http://localhost:8888 来获取服务的配置,并查看 Spring 云配置服务器 [=13] 中的计算 - service.properties =]
您可以在启动时从 Calcuation-Service 的日志中验证它
INFO 10988 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
这个答案在书“Microservices Interview Questions, For Java Developers (Spring Boot, Spring Cloud, Cloud
本机应用程序) Munish Chandel,版本 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 文件特定于 Spring 引导应用程序。除非您更改应用程序外部属性的位置,否则 spring 引导将始终从以下位置加载 application.yml:
/src/main/resources/application.yml
您可以在此文件中存储应用程序的所有外部属性。任何 Spring 引导项目中可用的通用属性可在以下位置找到: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.yml 另一方面特定于 spring-cloud-config 并在之前加载application.yml
bootstrap.yml 仅当您使用 Spring 云并且您的微服务配置存储在远程 Spring 云配置服务器时才需要.
关于bootstrap.yml
的要点
- 与 Spring 云配置服务器一起使用时,您应使用以下属性指定应用程序名称和配置 git 位置。
spring.application.name: "application-name"
spring.cloud.config.server.git.uri: "git-uri-config"
- 与微服务(云配置服务器除外)一起使用时,我们需要指定
使用以下属性的配置服务器的应用程序名称和位置
spring.application.name:
spring.cloud.config.uri:
- 此属性文件可以包含与 Spring 云环境相关的其他配置,例如eureka 服务器位置,encryption/decryption 相关属性。
启动后,Spring 云使用应用程序名称对 Spring 云配置服务器进行 HTTP(S) 调用,并检索回该应用程序的配置。
application.yml 包含微服务的默认配置,在 bootstrap 过程中(从云配置服务器)检索的任何配置将覆盖 application.yml 中定义的配置
好吧,我完全同意在这一点上已经存在的答案:
bootstrap.yml
用于保存指出远程配置位置的参数,Bootstrap应用上下文是用这些远程配置创建的。
实际上,它也可以像application.yml
一样存储普通属性。但是要注意这个棘手的事情:
- 如果您将属性放在
bootstrap.yml
中,它们的优先级将低于几乎任何其他 属性 来源,包括 application.yml。如所述 here.
说清楚,与bootstrap.yml
相关的属性有两种:
- 在 bootstrap 阶段加载的属性。我们使用
bootstrap.yml
来查找属性持有者(一个文件系统,git 存储库或其他东西),并且我们通过这种方式获得的属性具有高优先级,因此它们不能被本地配置覆盖。如所述 here.
bootstrap.yml
中的属性。如前所述,它们将获得较低的优先级。使用它们来设置默认值可能是个好主意。
因此,在 application.yml
上放置 属性 或在 spring 引导中放置 bootstrap.yml
之间的区别是:
- bootstrap阶段加载配置文件的属性只能放在
bootstrap.yml
.
- 至于所有其他类型的属性,将它们放在
application.yml
中将获得更高的优先级。
bootstrap.yml 的另一个用途是从 kubernetes configmap 和 secret[=27 加载配置=] 资源。应用程序必须导入 spring-cloud-starter-kubernetes 依赖项。
与 Spring 云配置一样,这必须在 bootstrap 短语期间进行。
来自文档:
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
因此可以引用存储在具有 meta.name 默认名称的 configmap 资源中的属性,就像 application.yml
中的属性一样
同样的过程也适用于机密:
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
Bootstrap.yml 是启动 spring 引导应用程序时加载的第一个文件,application.property 是应用程序启动时加载的文件。
所以,你保留,可能是你的配置服务器的凭据等,在加载应用程序期间需要 bootstrap.yml 然后在 application.properties 你保留可能是数据库 URL 等
bootstrap.yml 在您使用 Spring 云时使用,并且您的应用程序配置存储在远程配置服务器(例如 Spring 云配置服务器)上。 bootstrap.yml 在 application.yml
之前加载
在 application.yml 上放置 属性 或在 spring 引导中放置 bootstrap.yml 有什么区别? 在 logging.config 情况下,应用程序的工作方式不同。
我刚问过 Spring Cloud
个人,我想我应该在这里分享我的信息。
bootstrap.yml
在 application.yml
之前加载。
通常用于以下情况:
- 使用Spring Cloud Config Server时,需要在
bootstrap.yml
中指定 - 一些
encryption/decryption
信息
spring.application.name
和spring.cloud.config.server.git.uri
从技术上讲,bootstrap.yml
由父 Spring ApplicationContext
加载。那个父 ApplicationContext
在使用 application.yml
.
bootstrap.yml
或 bootstrap.properties
只有 used/needed 如果您使用 Spring Cloud 并且您的应用程序配置存储在远程配置服务器(例如 Spring 云配置服务器)。
来自文档:
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.
请注意 bootstrap.yml
或 bootstrap.properties
可以 包含其他配置(例如默认值),但通常您只需要将 bootstrap 配置这里。
通常它包含两个属性:
- 配置服务器的位置(
spring.cloud.config.uri
) - 应用程序名称 (
spring.application.name
)
启动时,Spring云使用应用程序名称对配置服务器进行 HTTP 调用,并检索回该应用程序的配置。
application.yml
或 application.properties
包含标准应用程序配置 - 通常是默认配置,因为在 bootstrap 过程中检索到的任何配置都将覆盖此处定义的配置。
Bootstrap.yml 用于从服务器获取配置。它可以用于 Spring 云应用程序或其他应用程序。通常它看起来像:
spring:
application:
name: "app-name"
cloud:
config:
uri: ${config.server:http://some-server-where-config-resides}
当我们启动应用程序时,它会尝试连接到给定的服务器并根据 run/debug 配置中提到的 spring 配置文件读取配置。
如果服务器无法访问,应用程序甚至可能无法继续进行。但是,如果本地存在与配置文件匹配的配置,则服务器配置将被覆盖。
好的方法:
为本地和 运行 使用不同配置文件的应用维护单独的配置文件。
这里只是我的 2 美分..
Bootstrap.yml 或 Bootstrap.properties 用于从 Spring 云服务器获取配置。
例如,在我的 Bootstrap.properties 文件中,我有以下配置
spring.application.name=Calculation-service
spring.cloud.config.uri=http://localhost:8888
启动应用程序时,它会尝试通过连接到 http://localhost:8888 来获取服务的配置,并查看 Spring 云配置服务器 [=13] 中的计算 - service.properties =]
您可以在启动时从 Calcuation-Service 的日志中验证它
INFO 10988 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
这个答案在书“Microservices Interview Questions, For Java Developers (Spring Boot, Spring Cloud, Cloud 本机应用程序) Munish Chandel,版本 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 文件特定于 Spring 引导应用程序。除非您更改应用程序外部属性的位置,否则 spring 引导将始终从以下位置加载 application.yml:
/src/main/resources/application.yml
您可以在此文件中存储应用程序的所有外部属性。任何 Spring 引导项目中可用的通用属性可在以下位置找到: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.yml 另一方面特定于 spring-cloud-config 并在之前加载application.yml
bootstrap.yml 仅当您使用 Spring 云并且您的微服务配置存储在远程 Spring 云配置服务器时才需要.
关于bootstrap.yml
的要点- 与 Spring 云配置服务器一起使用时,您应使用以下属性指定应用程序名称和配置 git 位置。
spring.application.name: "application-name" spring.cloud.config.server.git.uri: "git-uri-config"
- 与微服务(云配置服务器除外)一起使用时,我们需要指定 使用以下属性的配置服务器的应用程序名称和位置
spring.application.name: spring.cloud.config.uri:
- 此属性文件可以包含与 Spring 云环境相关的其他配置,例如eureka 服务器位置,encryption/decryption 相关属性。
启动后,Spring 云使用应用程序名称对 Spring 云配置服务器进行 HTTP(S) 调用,并检索回该应用程序的配置。
application.yml 包含微服务的默认配置,在 bootstrap 过程中(从云配置服务器)检索的任何配置将覆盖 application.yml 中定义的配置
好吧,我完全同意在这一点上已经存在的答案:
bootstrap.yml
用于保存指出远程配置位置的参数,Bootstrap应用上下文是用这些远程配置创建的。
实际上,它也可以像application.yml
一样存储普通属性。但是要注意这个棘手的事情:
- 如果您将属性放在
bootstrap.yml
中,它们的优先级将低于几乎任何其他 属性 来源,包括 application.yml。如所述 here.
说清楚,与bootstrap.yml
相关的属性有两种:
- 在 bootstrap 阶段加载的属性。我们使用
bootstrap.yml
来查找属性持有者(一个文件系统,git 存储库或其他东西),并且我们通过这种方式获得的属性具有高优先级,因此它们不能被本地配置覆盖。如所述 here. bootstrap.yml
中的属性。如前所述,它们将获得较低的优先级。使用它们来设置默认值可能是个好主意。
因此,在 application.yml
上放置 属性 或在 spring 引导中放置 bootstrap.yml
之间的区别是:
- bootstrap阶段加载配置文件的属性只能放在
bootstrap.yml
. - 至于所有其他类型的属性,将它们放在
application.yml
中将获得更高的优先级。
bootstrap.yml 的另一个用途是从 kubernetes configmap 和 secret[=27 加载配置=] 资源。应用程序必须导入 spring-cloud-starter-kubernetes 依赖项。
与 Spring 云配置一样,这必须在 bootstrap 短语期间进行。
来自文档:
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
因此可以引用存储在具有 meta.name 默认名称的 configmap 资源中的属性,就像 application.yml
中的属性一样同样的过程也适用于机密:
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
Bootstrap.yml 是启动 spring 引导应用程序时加载的第一个文件,application.property 是应用程序启动时加载的文件。 所以,你保留,可能是你的配置服务器的凭据等,在加载应用程序期间需要 bootstrap.yml 然后在 application.properties 你保留可能是数据库 URL 等
bootstrap.yml 在您使用 Spring 云时使用,并且您的应用程序配置存储在远程配置服务器(例如 Spring 云配置服务器)上。 bootstrap.yml 在 application.yml
之前加载