1. Overview
1.概述
Spring Boot is an opinionated framework. Despite this, we usually end up overriding autoconfigured properties in an application configuration file such as application.properties.
Spring Boot是一个有主见的框架。尽管如此,我们通常最终会在应用程序配置文件(如application.properties)中覆盖自动配置的属性。
However, in a Spring Cloud application, we often use another configuration file called bootstrap.properties.
然而,在Spring Cloud应用程序中,我们经常使用另一个名为bootstrap.properties的配置文件。
In this quick tutorial, we’ll explain the differences between bootstrap.properties and application.properties.
在这个快速教程中,我们将解释bootstrap.properties和application.properties之间的区别。
2. When Is the Application Configuration File Used?
2.什么时候使用应用程序配置文件?
We use application.yml or application.properties for configuring the application context.
我们使用application.yml或application.properties 用于配置应用环境。
When a Spring Boot application starts, it creates an application context that doesn’t need to be explicitly configured – it’s already autoconfigured. However, Spring Boot offers different ways to override these properties.
当Spring Boot应用程序启动时,它会创建一个不需要显式配置的应用程序上下文,它已经是自动配置的。然而,Spring Boot提供了不同的方式来覆盖这些属性。
We can override these in code, command-line arguments, ServletConfig init parameters, ServletContext init parameters, Java system properties, operating system variables, and application properties file.
我们可以在代码、命令行参数、ServletConfig初始参数、ServletContext初始参数、Java系统属性、操作系统变量和应用程序属性文件中覆盖这些。
An important thing to keep in mind is that these application properties files have the lowest precedence compared to other forms of overriding application context properties.
需要记住的一件事是,与其他形式的覆盖应用程序上下文属性相比,这些应用程序属性文件具有最低的优先权。
We tend to group properties that we can override in the application context:
我们倾向于将我们可以在应用环境中覆盖的属性分组。
- Core properties (logging properties, thread properties)
- Integration properties (RabbitMQ properties, ActiveMQ properties)
- Web properties (HTTP properties, MVC properties)
- Security properties (LDAP properties, OAuth2 properties)
3. When Is the Bootstrap Configuration File Used?
3.什么时候使用Bootstrap配置文件?
We use bootstrap.yml or bootstrap.properties for configuring the bootstrap context. This way we keep the external configuration for bootstrap and main context nicely separated.
我们使用bootstrap.yml或bootstrap.properties 用于配置bootstrap上下文。这样一来,我们就可以将bootstrap和main context的外部配置很好地分开。
The bootstrap context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files.
bootstrap上下文负责从外部资源加载配置属性,并负责解密本地外部配置文件中的属性。
When the Spring Cloud application starts, it creates a bootstrap context. The first thing to remember is that the bootstrap context is the parent context for the main application.
当Spring Cloud应用程序启动时,它会创建一个bootstrap context。首先要记住的是,bootstrap上下文是主应用程序的父级上下文。
Another key point to remember is that these two contexts share the Environment, which is the source of external properties for any Spring application. In contrast with the application context, the bootstrap context uses a different convention for locating the external configuration.
另一个需要记住的关键点是,这两个上下文共享Environment,它是任何Spring应用程序的外部属性的来源。与应用程序上下文不同的是,bootstrap上下文使用不同的约定来定位外部配置。
The source of configuration files can be a filesystem or even a git repository, for example. The services use their spring-cloud-config-client dependency to access the configuration server.
配置文件的来源可以是一个文件系统,甚至是一个git库,例如。服务使用其spring-cloud-config-client依赖性来访问配置服务器。
To put it in simple words, the configuration server is the point through which we access the application context configuration files.
用简单的话来说,配置服务器是我们访问应用程序上下文配置文件的地方。
4. Quick Example
4.快速实例
In this example, the bootstrap context configuration file configures the spring-cloud-config-client dependency to load the right application properties files.
在这个例子中,bootstrap上下文配置文件配置了spring-cloud-config-client依赖关系以加载正确的应用程序属性文件。
Let’s see an example of a bootstrap.properties file:
让我们看看一个bootstrap.properties文件的例子。
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=s3cr3t
spring.cloud.config.fail-fast=true
management.security.enabled=false
5. Conclusion
5.总结
In contrast to a Spring Boot application, a Spring Cloud application features a bootstrap context that is the parent of the application context. Although both of them share the same Environment, they have different conventions for locating the external configuration files.
与Spring Boot应用程序不同的是,Spring Cloud应用程序具有一个引导上下文,它是应用程序上下文的父级。虽然它们都共享相同的Environment,但它们在定位外部配置文件方面有不同的约定。
The bootstrap context is searching for a bootstrap.properties or a bootstrap.yaml file, whereas the application context is searching for an application.properties or an application.yaml file.
bootstrap上下文搜索的是bootstrap.properties或bootstrap.yaml文件,而application上下文搜索的是application.properties或application.yaml文件。
And, of course, the configuration properties of the bootstrap context load before the configuration properties of the application context.
当然,bootstrap上下文的配置属性也会在应用上下文的配置属性之前加载。