事件驱动架构

事件驱动模型是基于观察者模式或者发布订阅模式实现的。

核心概念

事件驱动架构(Event-Driven Architecture,EDA)是一种基于事件和异步通信的架构模式。其核心概念包括以下几个方面:

事件(Event): 事件是系统中发生的事情或状态变化的表示。它可以是用户操作、传感器数据、消息等。事件通常包含有关事件发生的上下文信息。

事件发布者(Event Publisher)

事件订阅者(Event Subscriber)

事件通道(Event Channel): 事件通道是事件发布者和事件订阅者之间的通信媒介。它可以是消息队列、事件总线、消息中间件等,用于在系统中传递事件。

异步通信: 事件驱动架构中的通信是异步的,即事件发布者和订阅者之间不需要立即响应对方。这使得系统更加灵活、可伸缩,能够更好地处理分布式环境中的事件流。

状态管理: 一些事件可能导致系统中的状态发生变化。因此,事件驱动架构通常需要有效的状态管理机制,以确保系统能够正确地响应事件并保持一致性。

spring实现事件驱动架构的组件

ApplicationEventPublisher

ApplicationEventPublisherAware接口的实现类通常是由Spring容器自动注入ApplicationEventPublisher的。Spring容器在实例化Bean的过程中,如果发现某个Bean实现了ApplicationEventPublisherAware接口,就会自动调用其setApplicationEventPublisher方法,将ApplicationEventPublisher注入到该Bean中。

@Component
public class CustomEventPublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher eventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void publishEvent(String message) {
        CustomEvent customEvent = new CustomEvent(this, message);
        eventPublisher.publishEvent(customEvent);
    }
}

ApplicationEventPublisher的publishEvent方法是用于将事件发布给所有对该事件感兴趣的监听器。

Event

事件(Event): 事件是在系统中发生的某种事情,它是一个普通的POJO类,通常继承自ApplicationEvent。

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
    // 事件相关的信息
    public CustomEvent(Object source, String message) {
        super(source);
        // 初始化事件信息...
    }
}

Listener

当事件被发布时,所有实现了ApplicationListener接口的监听器都会接收到该事件,并且放入listener的onapplicationevent方法通过instanceof来判断是否是自己处理的event。

@Component
public class CustomEventListener implements ApplicationListener<CustomEvent> {

    @Override
    public void onApplicationEvent(CustomEvent event) {
        if (event instanceof CustomEvent) {
            // 处理自己感兴趣的事件...
        }
    }
}

其他的一些类:只使用上述三个类即可

如果你使用Spring框架进行事件驱动开发,并且只关心简单的事件发布和监听,通常情况下你不需要直接使用ApplicationEventMulticaster或其默认实现SimpleApplicationEventMulticaster。Spring框架会在后台自动处理这些细节,你只需关注事件的定义、发布和监听即可。

不使用spring,使用java原生类进行完整的逻辑开发会使用到的类

EventObject

public class EventObject implements java.io.Serializable {

    @java.io.Serial
    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object source;

    /**
     * Constructs a prototypical Event.
     *
     * @param source the object on which the Event initially occurred
     * @throws IllegalArgumentException if source is null
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");

        this.source = source;
    }

    /**
     * The object on which the Event initially occurred.
     *
     * @return the object on which the Event initially occurred
     */
    public Object getSource() {
        return source;
    }

    /**
     * Returns a String representation of this EventObject.
     *
     * @return a String representation of this EventObject
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }
}

EventListener

什么也没就是一个接口标识

相关推荐
canonical_entropy1 小时前
金蝶云苍穹的Extension与Nop平台的Delta的区别
后端·低代码·架构
沛沛老爹2 小时前
服务监控插件全览:提升微服务可观测性的利器
微服务·云原生·架构·datadog·influx·graphite
huaqianzkh3 小时前
了解华为云容器引擎(Cloud Container Engine)
云原生·架构·华为云
Kika写代码4 小时前
【基于轻量型架构的WEB开发】【章节作业】
前端·oracle·架构
刘某某.4 小时前
使用OpenFeign在不同微服务之间传递用户信息时失败
java·微服务·架构
迪捷软件4 小时前
知识|智能网联汽车多域电子电气架构会如何发展?
架构·汽车
zyhJhon4 小时前
软考架构-层次架构风格
架构
nbsaas-boot4 小时前
架构卡牌游戏:通过互动与挑战学习系统设计的创新玩法
学习·游戏·架构
Casual_Lei5 小时前
ClickHouse 的底层架构和原理
clickhouse·架构
cooldream20098 小时前
828华为云征文 | 在华为云X实例上部署微服务架构的文物大数据管理平台的实践
微服务·架构·华为云·文物大数据平台