从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