https://www.jianshu.com/p/8eeffa9fdb59

今天我们一起学习一下 Bootstrap 配置的相关知识,在学习目标中我已经列出了今天需要学习的知识点,第一个知识点为复习知识点,属于 Spring Boot 中的知识,这里我们既然讲到了配置文件,就顺便把 Spring Boot 支持的两种配置文件类型的相关知识复习一下,有助于我们对 Bootstrap 配置的学习。
首先我们先创建项目:打开 https://start.spring.io/,填写相关信息,添加 Web、Actuator 以及 Cloud Bootstrap 依赖,点击 “Generate Project” 按钮生成项目,并导入到 idea 中。(注:此处使用的 Spring Boot 版本为 1.X 系列)

 
 

 

一、(复习)为什么 Spring Boot 可以支持两种配置文件类型

熟悉 Spring Boot 的小伙伴一定知道 Spring Boot 是支持两种配置文件类型的:

  • application.yaml / application.yml
  • application.properties

这里我们通过源代码查找的方式,看一下为什么 Spring Boot 会支持这两种配置文件类型。
首先,我们要找到加载上下文环境的类 **ConfigFileApplicationListener**,这个类是通过加载本地已知的配置文件中的属性来配置上下文环境。
然后在这个类中找到 **Loader#load()**** **方法,查看 **PropertySourcesLoader ** 源码,找到

private final List<PropertySourceLoader> loaders;

这里我们查看一下 **PropertySourceLoader ** 接口的实现类,可以发现,有两个类实现了该接口:

  • **PropertiesPropertySourceLoader**
  • **YamlPropertySourceLoader**

具体代码就不再展开讲解了,已经十分明了了。


二、Bootstrap 配置文件

我们都知道,Spring Boot 中使用 application.properties 配置文件,通过上篇文章对 Bootstrap 上下文的学习,我们应该也能猜到,在 Spring Cloud 中使用 bootstrap.properties 配置文件。也就是说,在 Spring Cloud 项目中,application.propertiesbootstrap.properties 配置文件是同时存在的。
**BootstrapApplicationListener#onApplicationEvent()**** **方法中,可以看出当 spring.cloud.bootstrap.name:bootstrap 存在时,使用该配置项,否则,使用 "bootstrap" 默认值。

String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");

所以我们在项目的 resources 目录下新建名为 bootstrap.properties 的配置文件,这个就是我们 Spring Cloud 中的配置文件。


三、调整 Bootstrap 配置文件名称

由于 Spring Cloud 的默认配置文件为 bootstrap.properties,那如果我们想要修改配置文件的名称应该如何做呢?这里我们使用调整程序启动参数的方式来进行修改。
首先我们在 Idea 中添加如下程序启动参数:

--spring.cloud.bootstrap.name=spring-cloud

 

 
 

bootstrap 配置文件名称发生了改变,改变成了 "spring-cloud",现有三个文件:(标黑的为我们的期望值

 

  • application.properties
    • spring.application.name=spring-cloud-client
  • bootstrap.properties
    • spring.application.name = spring-cloud-config-client-demo
  • spring-cloud.properties
    • spring.application.name = spring-cloud

其中 application.propertiesConfigFileApplicationListener 被加载了)和 spring-cloud.properties 读取了,这是我们所期望的。
重启项目,在 http://localhost:8080/env 中查看结果,结果也是和我们所期望的是相同的:

 
 

 


四、调整 Bootstrap 配置文件路径

既然配置文件名称是可以修改的,那么接下来我们尝试使用类似的方式修改一下 Bootstrap 配置文件路径,这里我们在 resources 目录下新建 config/spring-cloud2.properties 文件,添加如下程序启动参数:

--spring.cloud.bootstrap.name=spring-cloud2
--spring.cloud.bootstrap.location=config

 

 
 

现有四个文件:(标黑的为我们的期望值

 

  • application.properties
    • spring.application.name=spring-cloud-client
  • bootstrap.properties
    • spring.application.name = spring-cloud-config-client-demo
  • spring-cloud.properties
    • spring.application.name = spring-cloud
  • config/spring-cloud2.properties
    • spring.application.name = spring-cloud2

重启项目,在 http://localhost:8080/env 中查看结果,结果也是和我们所期望的是相同的:

 
 

 


五、自定义 Bootstrap 配置(实现 Spring 标准接口 )

上面说了这么多 Bootstrap 配置相关的东西,一直使用的都是别人的配置项,接下来我们来自定义配置,首先我们实现 Spring 的标准接口来完成自定义Bootstrap 配置 :
1、创建 META-INF/spring.factories 文件(类似于 Spring Boot 自定义 Starter)
2、创建自定义 Bootstrap 配置 Configuration

package top.alanshelby.springcloudchapter2.bootstrap;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * Bootstrap 配置 Bean
 *
 * @ClassName MyConfiguration
 * @Author AlanShelby
 * @Date 2019-04-22 14:29:14 14:29
 * @Version 1.0
 */
@Configuration
public class MyConfiguration implements ApplicationContextInitializer {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        // 获取 PropertySource
        MutablePropertySources propertySources = environment.getPropertySources();
        // 定义一个新的 PropertySource 放在首位
        propertySources.addFirst(createPropertySource());
    }

    private PropertySource createPropertySource() {
        Map<String, Object> source = new HashMap<>();
        source.put("name", "AlanShelby");

        PropertySource propertySource = new MapPropertySource("my-property-source", source);

        return propertySource;
    }
}

上面的代码是我们定义了一个新的 PropertySource 并将其放到了 MutablePropertySources 首位,这个配置项的 Key 值是 my-property-source,里面对应的属性信息为 source.put("name", "AlanShelby")
3、配置 META-INF/spring.factories 关联 Key org.springframework.cloud.bootstrap.BootstrapConfiguration

org.springframework.cloud.bootstrap.BootstrapConfiguration =top.alanshelby.springcloudchapter2.bootstrap.MyConfiguration

然后浏览器访问 http://localhost:8080/env,可以看到以下信息,表示我们自定义的配置成功了:

 
chapter-6.png

 


六、自定义 Bootstrap 配置(实现 SpringCloud 的接口)

上面我们使用了实现 Spring 标准接口的方式自定义了 Bootstrap 配置项,接下来我们来实现 Spring Cloud 接口来实现该功能。
1、实现 PropertySourceLocator

package top.alanshelby.springcloudchapter2;

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义 Bootstrap 配置属性源(实现 SpringCloud 的接口)
 *
 * @ClassName MyPropertySourceLocator
 * @Author AlanShelby
 * @Date 2019-04-22 14:45:15 14:45
 * @Version 1.0
 */
public class MyPropertySourceLocator implements PropertySourceLocator {
    @Override
    public PropertySource<?> locate(Environment environment) {
        if (environment instanceof ConfigurableEnvironment) {
            ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            propertySources.addFirst(createPropertySource());
        }
        return null;
    }

    private PropertySource createPropertySource() {
        Map<String, Object> source = new HashMap<>();
        source.put("spring.application.name", "AlanShelby-SpringCloud");
   
        PropertySource propertySource = new MapPropertySource("alan-property-source", source);

        return propertySource;
    }
}

2、配置 META-INF/spring.factories 关联 Key org.springframework.cloud.bootstrap.BootstrapConfiguration

org.springframework.cloud.bootstrap.BootstrapConfiguration = top.alanshelby.springcloudchapter2.MyPropertySourceLocator

然后浏览器访问 http://localhost:8080/env,可以看到以下信息,表示我们自定义的配置成功了:

 
 

 


至此,关于 Spring Cloud 中的 Bootstrap 配置就讲解完了,这是我的理解,各位看官如果有不同见解或文章中有错误,请不吝指正。
所用代码码云地址:https://gitee.com/AlanShelby/spring-cloud-chapter
知乎专栏地址:https://zhuanlan.zhihu.com/c_200981602
个人微信公众号:AlanShelby(多多关注,感谢~)



作者:AlanShelby
链接:https://www.jianshu.com/p/8eeffa9fdb59
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted on 2020-09-16 12:47  Sharpest  阅读(6161)  评论(0编辑  收藏  举报