事件驱动架构是实现技术组件解耦的基础收手段 事件驱动架构核心价值:解耦(不再直接调用目标函数) 学习目标
事件架构驱动基本概念和组成
spring对事件驱动架构的抽象和实现原理
目录
事件驱动架构的组成
spring事件驱动的应用
spring事件驱动的实现原理
事件驱动架构的组成 事件:业务状态变更动作抽象成一个对象 事件驱动架构: 发布者,事件中心,消费者
spring事件驱动的应用 自定义事件 发布事件 1 2 3 4 5 6 7 8 @Autowired private ApplicationEventPublisher publisher; public void testCustomerEvent() { CustomerEvent customerEvent = new CustomerEvent(); publisher.publishEvent(customerEvent); }
监听事件 1 2 3 4 5 6 @Override @EventListener @Async //异步执行,和发布线程不同 public void consumEvent(CustomerEvent event) { System.out.println("CustomerEvent: " + event.getEventContent()); }
内置事件监听 扩展spring事件处理 针对spring容器启动、停止以及关闭等生命周期各阶段事件进行订阅,实现对spring的有效监听和处理
1 2 3 4 5 6 7 @component public class TestApplicationListener implements ApplicationListener<ContextStoppedEvent>{ @Override public void onApplicationEvent(ContextStoppedEvent contextStoppedEvent){ System.out.println(contextStoppedEvent); } }
如果是监听 Spring 应用上下文(ApplicationContext)创建之后的事件,可以直接在监听器上使用 @Component 注解即可,否则需要在配置文件中声明配置(如下),因为 ApplicationContext 并未创建,这时的 Bean 是不能被加载的。 在资源目录中的 META-INF/spring.factories 文件中自动注册:
1 2 org.springframework.context.ApplicationListener=\ cn.javastack.springboot.features.listener.JavastackListener
dubbo集成 Dubbo框架基于ContextRefreshedEvent事件完成服务发布
1 2 3 4 public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean,DisposableBean,ApplicationContextAware, ApplicationListener<ContextRefreshedEvent>, BeanNameAware { ... }
注:看代码发现ServiceBean没有实现ApplicationListener??????
nacos集成 Nacos框架基于WebServerlnitializedEvent事件完成服务绑定
1 2 3 4 public abstract class AbstractAutoServiceRegistration<R extends Registration> ApplicationListener<WebServerInitializedEvent>{ ... }
spring事件驱动的实现原理 事件分发器:SimpleApplicationEventMulticaster (实现ApplicationEventMulticaster接口);事件分发器维护了消费者列表 初始化消费者时机:1.AbstractApplicationContext.registListners();2.ApplicationListenerDetector(实现了BeanPostprocessor) 触发事件:生命周期不同阶段
ApplicationEventPublisher 1 2 3 4 5 6 7 public abstract class AbstractApplicationcontext { public void publishEvent(ApplicationEvent event, Nullable ResolvableType eventType) { getApplicationEventMulticaster().multicastEvent(event, eventType); if(this.parent != hull){ this.parent.publishEvent(event); } }
ApplicationEventMulticaster(发布器dispatcher) ApplicationEventMulticaster相当于观察者模式中的Subject,维护着ApplicationListener列表,并能实现对这些ApplicationListener发送事件 (类似disppatcher,不过dispatcher事件和消费者一一对应)
1 2 3 4 5 6 7 8 public interface ApplicationEventMulticaster{ void addApplicationListener(ApplicationListener listener); void addApplicationListenerBean(String listenerBeanName); void removeApplicationListener(ApplicationListener listener); void removeApplicationListenerBean(String listenerBeanName); void removeAllListeners(); void multicastEvent(ApplicationEvent event); }
SimpleApplicationEventMulticaster 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class SimpleApplicationEventMulticaster { public void multicastEvent(final ApplicationEvent event){ for (final ApplicationListener listener : getApplicationlisteners(event)){ Executor executor = getTaskExecutor(); if(executor != null){ executor.execute(new Runnable(){ public void run(){ listener.onApplicationEvent(event); } }); } } } }
初始化发布器中的消费者 AbstractApplicationContext初始化ApplicationListner(消费者) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public abstract class AbstractApplicationContext { public void refresh() throws BeansException, IllegalstateException { synchronized(this.startupShutdownMonitor){ try{ /注册用来拦截Bean创建的BeanPostProcessorregisterBeanPostProcessors(beanFactory); // 初始化自定义事件广播器initApplicationEventMulticaster(); //执行剧新 onRefresh(); // 注册监听器 registerListeners(); } } } }
ApplicationListenerDetector初始化ApplicationListner(消费者) 1 2 3 4 5 6 class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor public Object postProcessAfterInitialization(object bean, String beanName){ ... applicationContext,addApplicationListener((ApplicationListener<?>) bean); ... }
总结 轻量级,非分布式事件驱动框架
思考题 开发过程中,使用Spring事件实现事件驱动架构的具体业务场景有哪些?