Spring Cloud 2:Bootstrap
Posted yang21
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud 2:Bootstrap相关的知识,希望对你有一定的参考价值。
Environment 端点
请求路径
/env
数据来源
EnvironmentEndpoint
Controller 来源
EnvironmentMvcEndpoint
@ActuatorGetMapping("/{name:.*}") @ResponseBody @HypermediaDisabled public Object value(@PathVariable String name) { if (!getDelegate().isEnabled()) { // Shouldn‘t happen - MVC endpoint shouldn‘t be registered when delegate‘s // disabled return getDisabledResponse(); } return new NamePatternEnvironmentFilter(this.environment).getResults(name); }
Bootstrap的配置
由SpringApplication.configurePropertySources
进行加载
/** * Add, remove or re-order any {@link PropertySource}s in this application‘s * environment. * @param environment this application‘s environment * @param args arguments passed to the {@code run} method * @see #configureEnvironment(ConfigurableEnvironment, String[]) */ protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) { MutablePropertySources sources = environment.getPropertySources(); if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { sources.addLast( new MapPropertySource("defaultProperties", this.defaultProperties)); } if (this.addCommandLineProperties && args.length > 0) { String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME; if (sources.contains(name)) { PropertySource<?> source = sources.get(name); CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(new SimpleCommandLinePropertySource( name + "-" + args.hashCode(), args)); composite.addPropertySource(source); sources.replace(name, composite); } else { sources.addFirst(new SimpleCommandLinePropertySource(args)); } } }
bootstrap 属性设置
application.properties
与bootstrap.properties
同一级目录下的时候,两者都设置了 spring.application.name
。
此时访问:http://localhost:8082/yang/env
获取应用程序配置属性:environment.getProperty("spring.application.name")
得到的实际是application.properties
所设置的信息:yang-cloud-example
也可以这样获取当前的应用程序名称:
访问:http://localhost:8082/yang/env/spring.application.name
项目加载了两种配置文件,但实际上的ApplicationName
为application.properties
加载的。
这是因为:Environment
: PropertySources
是1:1的关系,PropertySources
中的一个PropertySource
,
一旦加载了spring.application.name
,之后的PropertySource
就不会再去加载spring.application.name
了。
即:第一个读到的spring.application.name
会覆盖之后的。
如果注释掉application.properties
中的spring.application.name
,
启动程序后访问:http://localhost:8082/yang/env/spring.application.name
Bootstrap配置文件
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
可配置,默认bootstrap
注意:BootstrapApplicationListener
的加载早于ConfigFileApplicationListener
,
所以在application.properties
中设置spring.cloud.bootstrap.enabled=false
是没有效果的。
这是因为ConfigFileApplicationListener
的 Order = Ordered.HIGHEST_PRECEDENCE + 10
第十一位;
而 BootstrapApplicationListener
的Order=Ordered.HIGHEST_PRECEDENCE + 5
第六位。
所以,如果需要设置spring.cloud.bootstrap.enabled=false
,可以使用优先级更高的事务去做,例如设置在命令行参数中。
Changing the Location of Bootstrap Properties
参考 BootstrapApplicationListener.java
实现
public class BootstrapApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered { public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap"; public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 5; public static final String DEFAULT_PROPERTIES = "defaultProperties"; private int order = DEFAULT_ORDER; @Override 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); } // ignore others }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>sun.flower</groupId> <artifactId>yang-cloud-example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>yang-cloud-example</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.M1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
以上是关于Spring Cloud 2:Bootstrap的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud(02)——bootstrap文件介绍
为啥 spring-cloud-starter-config 忽略 bootstrap.properties?
Spring Cloud 2020 bootstrap 配置文件失效
spring cloud - 我的 jar 外的 bootstrap.properties
spring cloud导入一个新的spring boot项目作为spring cloud的一个子模块微服务,怎么做/或者 每次导入一个新的spring boot项目,IDEA不识别子module(代