理解BootStrap上下文
BootStrap
上下文是Spring Cloud新引入的,与传统Spring上下文相同,是ConfigurableApplicationContext实例,由BootstrapApplication在监听ApplicationEnvironmentPreparedEvent时创建。
prepareEnvironment()[方法会激发]->ApplicationEnvironmentPreparedEvent[事件]->被BootStrapApplictionListener【在监听器类中此类被加载排序为第六】监听到。
Spring Boot的应用上下文在Bootstrap后面创建:context=creatApplicationContext();
结论:Spring Cloud BootStrap上下文优先于Spring Boot应用上下文;
并且BootStrap上下文是Spring Boot应用上下文的parent
jar包被加载排序是由orederd接口中的oreder()方法实现。
理解Actuator Endpoints
**Actuator**
[中文直译“传动装置”,在Spring Boot场景中表示为“生产而准备的特性”Production-ready features,这些特性是通过HTTP端口的形式帮助相关人员管理和监控应用。
大致分为:监控类:“端点信息”、“应用信息”、“外部化配置信息”、“指标信息”、“健康检查”、“Bean管理”、“WebURL映射管理”、“WebURL跟踪”
管理类:”外部化配置“、”日志配置“、”线程dump“、”堆dump“、”关闭应用“
注意:Spring Boot1.5开始Actuator增强了安全能力
Spring Cloud扩展Actuator Endpoints
上下文重启:/restart 暂停:/pause 恢复:/resume
测试类MyEventListener
package com.huilong.springcloudone.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyEventListener {
public static void main(String[] args){
AnnotationConfigApplicationContext annotactionConfigApplicationContext = new AnnotationConfigApplicationContext();
//增加监听器
annotactionConfigApplicationContext.addApplicationListener(new MyApplicationListener());
//上下文启动
annotactionConfigApplicationContext.refresh();
//发布事件
annotactionConfigApplicationContext.publishEvent(new MyApplicationEvent("hello world"));
}
private static class MyApplicationListener implements ApplicationListener<MyApplicationEvent>{
@Override
public void onApplicationEvent(MyApplicationEvent event) {
//%s 会调用toString()
System.out.printf("MyApplicationListener : %s \n",event.getSource());
}
}
private static class MyApplicationEvent extends ApplicationEvent{
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
private MyApplicationEvent(Object source) {
super(source);
}
}
}