The role of Bootstrap.yml
load order
If application.yml and bootstrap.yml are in the same directory: bootstrap.yml is loaded first and then application.yml, and bootstrap.yml is used for the bootstrap phase of the application context. bootstrap.yml is loaded by the parent Spring ApplicationContext.
Configuration difference
Bootstrap is the configuration context loading of spring cloud. Loaded by the spring-cloud-context package. Introduce dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
Concrete loading class
key code
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
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;
String configName = environment
.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
for (ApplicationContextInitializer<?> initializer : event.getSpringApplication()
.getInitializers()) {
if (initializer instanceof ParentContextApplicationContextInitializer) {
context = findBootstrapContext(
(ParentContextApplicationContextInitializer) initializer,
configName);
}
}
if (context == null) {
context = bootstrapServiceContext(environment, event.getSpringApplication(),
configName);
}
apply(context, event.getSpringApplication(), environment);
}
Application is the configuration loading of spring boot. Introduce dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Cloud configuration center multi-environment configuration
One: spring boot multi-environment configuration file
We know that spring boot can distinguish configurations by file name, as follows:
application-dev.yml #Development environment
application-test.yml #Test environment
application-prod.yml #Formal environment
Two: spring cloud multi-environment configuration file
But spring cloud uses the configuration center, just a boostrap.yml, and does not support the file name to distinguish. The method of distinguishing the environment in the spring cloud configuration center bootstrap.yml is as follows:
spring:
profiles:
active: dev
application:
name: user-server
feign:
hystrix:
enabled: true
##上面是基础配置,不用上配置中心那种
##下面是环境区分,主要不同环境不同文件获取
---
#开发环境
spring:
profiles: dev
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: c97d4f46-deba-5588-b05f-c2a061ccc688
config:
server-addr: localhost:8848
file-extension: yaml
group: DEFAULT_GROUP
namespace: c97d4f46-deba-5588-b05f-c2a061ccc688
---
#正式环境
spring:
profiles: prod
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: aa3de4c8-61ad-5568-9887-ed731659edd4
config:
server-addr: localhost:8848
file-extension: yaml
group: DEFAULT_GROUP
namespace: aa3de4c8-61ad-5568-9887-ed731659edd4
---
#测试环境
spring:
profiles: test
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: 98adcdd5-f25c-7890-b8f3-97352adc83e8
config:
server-addr: localhost:8848
file-extension: yaml
group: DEFAULT_GROUP
namespace: 98adcdd5-f25c-7890-b8f3-97352adc83e8
Multiple configurations are separated by -, and then the specific environment configuration is represented by spring.profiles=environment.
Three: java startup method
At startup, we can specify the running reference configuration on our server such as:
java -jar -Dspring.profiles.active=test *****.jar
或者dockerfile启动方式
ENTRYPOINT ["java","-jar","-Dspring.profiles.active=test","*****.jar"]