从Java事件监听到Spring Cloud事件监听

从Java事件监听到Spring Cloud事件监听

Java 事件监听

可参考上一篇文章:
https://blog.csdn.net/shang_xs/article/details/86560994

Spring Cloud事件监听

1. BootstrapApplicationListener

org.springframework.cloud.bootstrap.BootstrapApplicationListener第六优先

负责加载bootstrap.properties或者bootstrap.ymal

Spring Cloud “/META-INF/spring.factories”;

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.bootstrap.BootstrapApplicationListener,\
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,\
org.springframework.cloud.context.restart.RestartListener

1.1 负责初始化Bootstrap Application_ID = "bootstrap"
ConfigurableApplicationContext context = builder.run();
1.2 Bootstrap 是一个根Spring上下文,parent = null
联想 ClassLoader
ExtClassLoader <- AppClassLoader <-System ClassLoader <-Bootstrap ClassLoader

2.优先级分析

BootstrapApplicationListener加载的优先级高于ConfigurableApplicationContext(第十一优先) ,所以在application.properties中定义的信息加载不到
ConfigurableApplicationContext -> AnnotationConfigApplicationContext

3.BootStrap 配置属性

BootStrap 配置文件路径
-spring.cloud.bootstrap.location
覆盖远程配置属性
-spring.cloud.config.allowOverride
自定义Bootstrap配置
-@BootstrapConfiguration
自定义Bootstrap配置属性源
-PropertySourceLocator

4.ENV端点

org.springframework.boot.actuate.env.EnvironmentEndpoint

Environment关联多个带名称的PropertySource

AbstractRefreshableWebApplicationContext

org.springframework.web.context.support.AbstractRefreshableWebApplicationContext

@Override
protected void initPropertySources() {
    ConfigurableEnvironment env = getEnvironment();
    if (env instanceof ConfigurableWebEnvironment) {
        ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
    }
}

普通类型 StandardEnvironment

web类型StandardServletEnvironment

  • Environment

    • AbstractEnviroment

      • StandardEnviroment

Environment关联着一个PropertySources PropertySources关联着多个PropertySource

比较常用的是:SystemEnvironmentPropertySource

Java System#getProperties实现:名称"SystemProperties",对应的内容System.getProperties()

Java System#getEnv实现:名称"SystemEnviroment",对应的内容System.getProperties()

实现自定义配置:
实现 org.springframework.cloud.bootstrap.config.PropertySourceLocator
PropertySourceLocator

  • 实现PropertySource
    @Configuration
    @Order(Ordered.HIGHEST_PRECEDENCE)

    public class MyPropertySourceLocator implements PropertySourceLocator{
        @Override
        public PropertySource<?> locate(Environment environment) {
            HashMap<String, Object> source = new HashMap<>();
            source.put("server.port","9090");
            MapPropertySource mapPropertySource = new MapPropertySource("my-property-source",source);
            return mapPropertySource;
        }
    }
    
  • 暴露该实现作为一个Bean 如:注解@Configuration

  • 定义并且配置/META-INF/spring.factories
    org.springframework.cloud.bootstrap.BootstrapConfiguration=com.learn.demo.configuration.MyPropertySourceLocator
    注意事项:Enviroment允许出现同名配置,优先级高的胜出
    MutablePropertySources

    5.具体源码加载实现:

    private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<>();
    //由于propertySourceList的FIFO是有顺序的 在如下的代码块中,使用API调用addFirst实现resource的第一优先级
    public void addFirst(PropertySource<?> propertySource) {
        removeIfPresent(propertySource);
        this.propertySourceList.add(0, propertySource);
    }
    /**
    	 * Add the given property source object with lowest precedence.
    	 */
    public void addLast(PropertySource<?> propertySource) {
        removeIfPresent(propertySource);
        this.propertySourceList.add(propertySource);
    }
    

如何控制顺序:

实现Ordered以及标记Order

在Spring中,数值越小越优先

具体实现参见:https://github.com/dwyanewede/project-learn

具体实现:com.learn.demo.configuration.MyPropertySourceLocator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值