跳到主要内容

Events

An EventBus is a communication mechanism or pattern used in software development to enable the exchange of messages, events, or notifications between different parts of a software system or between loosely coupled components. It simplifies the decoupling of components and promotes a more scalable and maintainable architecture.

Local Eventbus

github: https://github.com/goxiaoy/go-eventbus

The Repository uses local eventbus to publish entities change events

Distributed Event

Producer

type Producer interface {
io.Closer
Send(ctx context.Context, msg Event) error
BatchSend(ctx context.Context, msg []Event) error
}
注意

Not all the underlying distributed event buses support atomic batch sending.

Consumer

type HandlerOf[T any] interface {
Process(context.Context, T) error
}
type ConsumerHandler HandlerOf[Event]

type Consumer interface {
io.Closer
// Process start process event with handler
Process(ctx context.Context, handler ConsumerHandler) error
}

Consistency

Unit of Work Producer

Combining with Aggregate Root allows for delayed event sending until the unit of work is committed.

event.ConsumerFactoryServer implements kratos transport.Server You can apply middleware pattern to ConsumerFactoryServer

	srv := event.NewConsumerFactoryServer(e, container)
// panic recovery,tracing,logging,unit of work
srv.Use(event.ConsumerRecover(event.WithLogger(logger)), trace.Receive(), event.Logging(logger), event.ConsumerUow(uowMgr))

Two-phase message Exposed Endpoint

You can embed eventserver.EventProviderSet to your di container to expose grpc endpoints for cases like Cache Consistency

eventserver "github.com/go-saas/kit/event/server"

service EventService{
rpc HandleEvent(HandleEventRequest)returns(google.protobuf.Empty){
}
rpc PublishEvent(PublishEventRequest)returns(google.protobuf.Empty){
}
}

Provider

Kafka

Pulsar