第一节 云原生应用(Cloud Native Applications) (课件) [问答]
package com.ikadmin.sr.springcloudlesson1.demo;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
public class SpringApplicationDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DefaultApplicationListener.class);
// context.addApplicationListener(new DefaultApplicationListener());
// context.register(DefaultApplicationListener.class);
// context.refresh();
context.publishEvent(new DefaultApplicationEvent(new Double(1.0)));
context.publishEvent(new DefaultApplicationEvent("test"));
context.publishEvent(new DefaultApplicationEvent(1));
// context.publishEvent(new DefaultApplicationEvent(null));
}
@Component
private static class DefaultApplicationListener implements ApplicationListener {
@Override
public void onApplicationEvent(DefaultApplicationEvent event) {
System.err.printf("DefaultApplicationEvent source: %s \n", event.getSource());
}
}
private static class DefaultApplicationEvent extends ApplicationEvent{
/**
* Create a new ApplicationEvent.
*
* @param source
* the object on which the event initially occurred (never {@code null})
*/
public DefaultApplicationEvent(Object source) {
super(source);
}
}
}
查找springlistener的方式 ctrl+G
可以看到在spring cloud context 进行了auto confiure
# AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration,\
org.springframework.cloud.autoconfigure.RefreshAutoConfiguration,\
org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration,\
org.springframework.cloud.autoconfigure.WritableEnvironmentEndpointAutoConfiguration
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.bootstrap.BootstrapApplicationListener,\
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,\
org.springframework.cloud.context.restart.RestartListener
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\
org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\
org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
可以看到其加载顺序为第6个
public class BootstrapApplicationListener
implements ApplicationListener, Ordered {
/**
* Property source name for bootstrap.
*/
public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";
/**
* The default order for this listener.
*/
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;
SpringCloudLesson1Application被当做配置类传入SpringApplication构造器,注解的叠加效果
@SpringBootApplication
public class SpringCloudLesson1Application {
public static void main(String[] args) {
// SpringApplication.run(SpringCloudLesson1Application.class, args);
SpringApplication application = new SpringApplication(SpringCloudLesson1Application.class);
ConfigurableApplicationContext context = application.run(args);
}
}
app启动时加载顺序:
listeners.starting();
listener.onApplicationEvent(event);
BootstrapApplicationListener
//通过spring.factories加载 (D:\shen\java\.m2\repos\org\springframework\cloud\spring-cloud-context\2.1.5.RELEASE\spring-cloud-context-2.1.5.RELEASE.jar!\META-INF\spring.factories)
ApplicationEnvironmentPreparedEvent // publish ApplicationEnvironmentPreparedEvent
onApplicationEvent 监听到事件,执行相应动作(创建bootstrap上下文)
context = createApplicationContext(); //spring boot 上下文创建
所有spring cloud BootStrap上下文先于spring boot 上下文创建,并且是spring boot的双亲
有时是事件触发创建上下文,有时是上下文创建触发事件,涉及spring的设计哲学
org.springframework.web.servlet.FrameworkServlet#doGet
org.springframework.web.servlet.FrameworkServlet#doService
org.springframework.context.event.SimpleApplicationEventMulticaster#doInvokeListener
// 20200307154719
// http://localhost:7000/actuator/health
{
"status": "UP"
}
// 20200307155543
// http://localhost:7000/actuator/metrics/jvm.gc.max.data.size/
{
"name": "jvm.gc.max.data.size",
"description": "Max size of old generation memory pool",
"baseUnit": "bytes",
"measurements": [
{
"statistic": "VALUE",
"value": 1417674752
}
],
"availableTags": [
]
}
management.endpoints.web.exposure.include=*
management.endpoint.restart.enabled=true
server.port=7000
http://localhost:7000/actuator/restart
http://localhost:7000/actuator/resume
http://localhost:7000/actuator/pause
DD的书的把github的demo全部跑一遍
工作遇到文档,查不到源码,再去书里查,很多配置项配置得很到位。
第二节 配置客户端(Spring Cloud Config Client) (课件) [问答]
第三节 配置服务器(Spring Cloud Config Server) (课件) [问答]
第四节 服务发现/注册(Service Discovery/Registry)(课件) [问答]
第五节 高可用服务治理(HA Service Government)(课件) [问答]
第六节 负载均衡(Load Balance)(课件) [问答]
第七节 Netflix Ribbon 源码解读 (课件) [问答]
第八节 服务短路(Circuit Breakers)(课件) [问答]
第九节 Spring Cloud Hystrix 源码解读 (课件) [问答]
第十节 服务调用(Service Call)(课件) [问答]
第十一节 Spring Cloud 服务网关 (课件) [问答]
第十二节 消息驱动整合(课件) [问答]
第十三节 Spring Cloud Stream Binder 实现 (课件) [问答]
第十四节 消息总线
第十五节 分布式应用跟踪
第十六节 Spring Cloud 系列回顾