前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊go.cqrs的EventHandler

聊聊go.cqrs的EventHandler

原创
作者头像
code4it
修改于 2021-04-12 02:30:47
修改于 2021-04-12 02:30:47
47700
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下go.cqrs的EventHandler

EventHandler

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
type EventHandler interface {
    Handle(EventMessage)
}

EventHandler定义了Handle方法

EventMessage

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// EventMessage is the interface that a command must implement.
type EventMessage interface {

    // AggregateID returns the ID of the Aggregate that the event relates to
    AggregateID() string

    // GetHeaders returns the key value collection of headers for the event.
    //
    // Headers are metadata about the event that do not form part of the
    // actual event but are still required to be persisted alongside the event.
    GetHeaders() map[string]interface{}

    // SetHeader sets the value of the header specified by the key
    SetHeader(string, interface{})

    // Returns the actual event which is the payload of the event message.
    Event() interface{}

    // EventType returns a string descriptor of the command name
    EventType() string

    // Version returns the version of the event
    Version() *int
}

EventMessage接口定义了AggregateID、GetHeaders、SetHeader、Event、EventType、Version方法

EventDescriptor

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// EventDescriptor is an implementation of the event message interface.
type EventDescriptor struct {
    id      string
    event   interface{}
    headers map[string]interface{}
    version *int
}

// NewEventMessage returns a new event descriptor
func NewEventMessage(aggregateID string, event interface{}, version *int) *EventDescriptor {
    return &EventDescriptor{
        id:      aggregateID,
        event:   event,
        headers: make(map[string]interface{}),
        version: version,
    }
}

// EventType returns the name of the event type as a string.
func (c *EventDescriptor) EventType() string {
    return typeOf(c.event)
}

// AggregateID returns the ID of the Aggregate that the event relates to.
func (c *EventDescriptor) AggregateID() string {
    return c.id
}

// GetHeaders returns the headers for the event.
func (c *EventDescriptor) GetHeaders() map[string]interface{} {
    return c.headers
}

// SetHeader sets the value of the header specified by the key
func (c *EventDescriptor) SetHeader(key string, value interface{}) {
    c.headers[key] = value
}

// Event the event payload of the event message
func (c *EventDescriptor) Event() interface{} {
    return c.event
}

// Version returns the version of the event
func (c *EventDescriptor) Version() *int {
    return c.version
}

EventDescriptor定义了id、event、headers、version属性,它实现了EventMessage接口

PublishEvent

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// PublishEvent publishes events to all registered event handlers
func (b *InternalEventBus) PublishEvent(event EventMessage) {
    if handlers, ok := b.eventHandlers[event.EventType()]; ok {
        for handler := range handlers {
            handler.Handle(event)
        }
    }
}

InternalEventBus的PublishEvent方法会遍历指定event.EventType()的handlers,挨个执行handler.Handle(event)方法

小结

go.cqrs的EventHandler定义了Handle方法;InternalEventBus的PublishEvent方法会遍历指定event.EventType()的handlers,挨个执行handler.Handle(event)方法。

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
聊聊go.cqrs的Dispatcher
go.cqrs的Dispatcher接口定义了Dispatch、RegisterHandler方法;InMemoryDispatcher定义了map[string]CommandHandler属性,其Dispatch方法根据command.CommandType()获取handler,然后执行handler.Handle(command);其RegisterHandler方法遍历commands,然后获取command的type,挨个注册到map[string]CommandHandler中。
code4it
2021/04/09
2730
聊聊go.cqrs的Dispatcher
聊聊go.cqrs的AggregateRoot
go.cqrs的AggregateRoot接口定义了AggregateID、OriginalVersion、CurrentVersion、IncrementVersion、Apply、TrackChange、GetChanges、ClearChanges方法。
code4it
2021/04/07
3310
聊聊go.cqrs的AggregateRoot
聊聊go.cqrs的DomainRepository
go.cqrs的DomainRepository定义了Load、Save方法;GetEventStoreCommonDomainRepo实现了DomainRepository接口,其Load方法主要是读取event,然后挨个执行aggregate.Apply;其Save方法主要是将aggregate.GetChanges()转换为event,然后通过streamWriter.Append写入,然后执行aggregate.ClearChanges(),最后执行r.eventBus.PublishEvent。
code4it
2021/04/08
3290
聊聊go.cqrs的DomainRepository
浅谈命令查询职责分离(CQRS)模式
在常用的三层架构中,通常都是通过数据访问层来修改或者查询数据,一般修改和查询使用的是相同的实体。在一些业务逻辑简单的系统中可能没有什么问题,但是随着系统逻辑变得复杂,用户增多,这种设计就会出现一些性能问题。虽然在DB上可以做一些读写分离的设计,但在业务上如果在读写方面混合在一起的话,仍然会出现一些问题。 本文介绍了命令查询职责分离模式(Command Query Responsibility Segregation,CQRS),该模式从业务上分离修改 (Command,增,删,改,会对系统状态进行修改)和查
逸鹏
2018/04/11
2.2K0
浅谈命令查询职责分离(CQRS)模式
聊聊eventhorizon的EventBus
eventhorizon的EventBus接口内嵌了EventHandler接口,定义了AddHandler、Errors、Wait方法。
code4it
2021/04/01
5440
聊聊eventhorizon的EventBus
聊聊event-sourcing-cqrs的model
event-sourcing-cqrs-examples的model定义了Event、Aggregate、ValueObject抽象类以及EventStore、Specification接口。
code4it
2021/04/15
5590
聊聊event-sourcing-cqrs的model
istio 庖丁解牛(三) galley
今天我们来解析istio控制面组件Galley. Galley Pod是一个单容器单进程组件, 没有sidecar, 结构独立, 职责明确.
钟华
2019/04/01
4.6K1
聊聊eventhorizon的Aggregate
eventhorizon/aggregatestore/model/aggregatestore.go
code4it
2021/03/31
4290
聊聊eventhorizon的Aggregate
利用 Watermill 实现 Golang CQRS
CQRS 的意思是“命令-查询责任隔离”。我们分离了命令(写请求)和查询(读请求)之间的责任。写请求和读请求由不同的对象处理。
为少
2021/05/27
9510
利用 Watermill 实现 Golang CQRS
聊聊cheddar的events
Cheddar/cheddar/cheddar-events/src/main/java/com/clicktravel/cheddar/event/Event.java
code4it
2021/03/24
3990
聊聊cheddar的events
分析kubernetes中的事件机制
我们通过 kubectl describe [资源] 命令,可以在看到Event输出,并且经常依赖event进行问题定位,从event中可以分析整个POD的运行轨迹,为服务的客观测性提供数据来源,由此可见,event在Kubernetes中起着举足轻重的作用。
silenceper
2020/03/08
1.7K0
分析kubernetes中的事件机制
Hadoop-Yarn源码-服务库与事件库
本文分析Hadoop2.6源码,生命周期长的对象,Yarn采用基于服务的对象管理模型对其进行管理。
Dlimeng
2023/06/30
1680
手撸一套纯粹的CQRS实现
关于CQRS,在实现上有很多差异,这是因为CQRS本身很简单,但是它犹如潘多拉魔盒的钥匙,有了它,读写分离、事件溯源、消息传递、最终一致性等都被引入了框架,从而导致CQRS背负了太多的混淆。本文旨在提供一套简单的CQRS实现,不依赖于ES、Messaging等概念,只关注CQRS本身。
拓荒者IT
2019/09/24
6780
Yarn 状态机以及事件机制
AsyncDispatcher 实现了接口Dispatcher,Dispatcher中定义了事件Dispatcher的接口。主要提供两个功能:
zeekling
2023/11/29
3760
Yarn 状态机以及事件机制
Web框架的设计方案和Go源码实现
那么为何要用web框架,或者说现在的主流web后端开发都要选定一个框架,然后再开发,就是为了提高效率,共通的业务以外的逻辑都由框架实现了,有了框架,开发只需要专注业务逻辑。
后端云
2022/11/25
3740
Web框架的设计方案和Go源码实现
CQRS框架:AxonFramework 之 Hello World
Command Query Responsibility Segregation,CQRS 这个架构好象最近博客园里讨论得比较多,有几篇园友的文章很有深度,推荐阅读: CQRS架构简介 浅谈命令查询职责分离(CQRS)模式 DDD CQRS架构和传统架构的优缺点比较 比较有趣的是,以往一断谈及架构思路、OO这些,往往都是java大佬们的专长,而CQRS这个话题,好象.NET占了上风。园友汤雪华的ENODE开源大作,在github上人气也很旺。 于是,我逆向思路搜索了下java的类似项目,果然有一个Axon
菩提树下的杨过
2018/01/18
1.8K0
CQRS框架:AxonFramework 之 Hello World
Go 模式
这是一种很常见的模式,但是在 golang 中,这种模式能够提供更多有用/高级的选项。比如 我们可以定义三种消费者:第一种,生产者生产的消息会阻塞,等所有消费者都消费完,第二种,生产者不等消费者,生产完消息就返回,消费者异步消费;第三种,消费者并行消费,生产者等所有消费者都消费完再返回。
王磊-字节跳动
2021/08/21
1.3K0
聊聊cheddar的events
Cheddar/cheddar/cheddar-events/src/main/java/com/clicktravel/cheddar/event/Event.java
code4it
2021/03/27
3950
CQRS+ES项目解析-Diary.CQRS
在《当我们在讨论CQRS时,我们在讨论些神马》中,我们讨论了当使用CQRS的过程中,需要关心的一些问题。其中与CQRS关联最为紧密的模式莫过于Event Sourcing了,CQRS与ES的结合,为我们构造高性能、可扩展系统提供了基本思路。本文将介绍 Kanasz Robert在《Introduction to CQRS》中的示例项目Diary.CQRS。
拓荒者IT
2019/09/23
7910
CQRS+ES项目解析-Diary.CQRS
针对事件驱动架构的Spring Cloud Stream
今天我们要分享一个比较有意思的内容。就是如何通过spring cloud 的stream来改造一个微服务下事件驱动的框架。 为什么要改造?我们都知道事件驱动的微服务开发框架,一个非常重要的点就是每次的
ImportSource
2018/04/03
1.7K0
针对事件驱动架构的Spring Cloud Stream
相关推荐
聊聊go.cqrs的Dispatcher
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档