码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Cloud 2:Bootstrap

时间:2018-11-12 22:18:46      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:put   project   command   code   new   pac   pre   lower   maven   

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.propertiesbootstrap.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

技术分享图片

项目加载了两种配置文件,但实际上的ApplicationNameapplication.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>
View Code

 

Spring Cloud 2:Bootstrap

标签:put   project   command   code   new   pac   pre   lower   maven   

原文地址:https://www.cnblogs.com/yang21/p/9949332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!