This is the 13th day of my participation in the Gwen Challenge.More article challenges

Set a Flag write something every day and stick to it.

The role of the Bootstrap. Yml

Load order

If application. Yml and bootstrap.yml reside in the same directory: Bootstrap. yml loads application

Bootstrap. yml is used for the boot phase of the application context. Bootstrap. yml is loaded by the parent Spring ApplicationContext.

Configure the difference

Bootstrap is a configuration context load for Spring Cloud. Loaded by the Spring-Cloud-content package.

Introduction of depend on

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter</artifactId>
</dependency>
Copy the code

Application is the configuration load of Spring Boot.

Introduction of depend on

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

Concrete load class

Part of the code

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
 ConfigurableEnvironment environment = event.getEnvironment();
  / / # spring. Cloud. The bootstrap. Enabled guide switch
 if(! environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class,
   true)) {
  return;
 }
 // don't listen to events in a bootstrap context
 if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
  return;
 }
 ConfigurableApplicationContext context = null;
  // The default boot file name is bootstrap
 String configName = environment
   .resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
  // Load the configuration
 for(ApplicationContextInitializer<? > initializer : event.getSpringApplication() .getInitializers()) {// Load from ParentContent
  if (initializer instanceofParentContextApplicationContextInitializer) { context = findBootstrapContext( (ParentContextApplicationContextInitializer) initializer, configName); }}if (context == null) {
  context = bootstrapServiceContext(environment, event.getSpringApplication(),
    configName);
  event.getSpringApplication()
    .addListeners(new CloseContextOnFailureApplicationListener(context));
 }

 apply(context, event.getSpringApplication(), environment);
}
Copy the code

Both bootstrap.yml and application.yml can be used to configure parameters.

Bootstrap. yml is executed at boot time and should be used to read configuration information earlier. You can think of it as system-level configuration of parameters that generally do not change. Once bootstrap. yml is loaded, the content is not overwritten.

Application. Yml can be used to define application-level, application-specific configuration information, which can be used to configure public parameters to be used in subsequent modules, etc.

Attribute overlay problem

When the Context is started, Spring Cloud creates a Bootstrap Context as the parent Context of the Spring Application’s Application Context.

At initialization, the Bootstrap Context is responsible for loading configuration properties from an external source and parsing the configuration. The two contexts share an Environment fetched from the outside. Bootstrap properties have a high priority and are not overridden by local configuration by default.

In other words, if the loaded application.yml content tag is the same as that of bootstrap, The application will not overwrite bootstrap, and the content in application.yml can be replaced dynamically.

Bootstrap. yml Typical application scenario

Microservice registry, configuration center, project name Spring.application.name

Nacos, for example

spring:
  application:
    name: paw-alibaba-nacos
  # profile
  profiles:
    active: dev

  # nacos
  cloud:
    nacos:
      discovery:
        server-addr: http://localhost:8848
        username: nacos
        password: 123456

      config:
        server-addr: http://localhost:8848
        username: nacos
        password: 123456
        context-path: nacos
        file-extension: yaml
        prefix: ${spring.application.name}
Copy the code