Event streaming and event sourcing are two related but different concepts in event-driven architecture.
Event streaming is the process of continuously capturing and recording events that occur in the system. These events can be processed and analyzed immediately or stored for later analysis. Event streaming is typically used in systems that need to process large amounts of real-time data, such as financial transactions or social media platforms.
The following is a simple example of event streaming in Go using the popular Kafka messaging system:
package main import ( "context" "fmt" "github.com/segmentio/kafka-go" ) func main() { // 设置Kafka生产者以将事件发送到主题 writer := kafka.NewWriter(kafka.WriterConfig{ Brokers: []string{"localhost:9092"}, Topic: "my-topic", }) // 发送一些事件到主题 writer.WriteMessages(context.Background(), kafka.Message{ Key: []byte("key1"), Value: []byte("value1"), }, kafka.Message{ Key: []byte("key2"), Value: []byte("value2"), }, ) // 设置Kafka消费者以从主题读取事件 reader := kafka.NewReader(kafka.ReaderConfig{ Brokers: []string{"localhost:9092"}, Topic: "my-topic", }) // 从主题读取事件 for { msg, err := reader.ReadMessage(context.Background()) if err != nil { break } fmt.Printf("Received message: key=%s, value=%sn", string(msg.Key), string(msg.Value)) } }
Event sourcing is a pattern for building systems that stores all changes in application state as a sequence of events. These events can then be used to reconstruct the state of the application at any point in time. Event sourcing is often used in systems that require auditability, traceability, or compliance, such as financial systems or healthcare systems.
The following is a simple example of using in-memory event storage for event sourcing in Go:
package main import ( "fmt" ) type Event struct { Type string Data interface{} } type EventStore struct { events []Event } func (store *EventStore) Append(event Event) { store.events = append(store.events, event) } func (store *EventStore) GetEvents() []Event { return store.events } type Account struct { idstring balance int store *EventStore } func NewAccount(id string, store *EventStore) *Account { return &Account{ id:id, balance: 0, store: store, } } func (account *Account) Deposit(amount int) { event := Event{ Type: "deposit", Data: amount, } account.store.Append(event) account.balance += amount } func (account *Account) Withdraw(amount int) { if account.balance >= amount { event := Event{ Type: "withdraw", Data: amount, } account.store.Append(event) account.balance -= amount } } func (account *Account) GetBalance() int { return account.balance } func main() { store := &EventStore{} account := NewAccount("123", store) account.Deposit(100) account.Withdraw(50) account.Deposit(25) events := store.GetEvents() for _, event := range events { switch event.Type { case "deposit": amount := event.Data.(int) fmt.Printf("Deposited %dn", amount) case "withdraw": amount := event.Data.(int) fmt.Printf("Withdrew %dn", amount) } } fmt.Printf("Final balance: %dn", account.GetBalance()) }
Event sourcing is a method by recording each modification to an aggregate as an event and appending it to a continuous stream. To reconstruct the final state of the aggregate, these events need to be read in sequence and then applied to the aggregate. This is in contrast to the on-the-fly modifications performed in a create, read, update, and delete (CRUD) system. In a CRUD system, any changes to a record's state are stored in the database, essentially overwriting the same
A previous version of an aggregate.After price changes have been saved to the Products table, only the price will be updated, while the other parts will remain unchanged. However, this approach can result in losing previous prices and the context behind changes, as shown in Figure 5.1.
In order to retain information including the new price and key metadata (such as the reason for the adjustment), the change record will be stored as an event in the Events table. The previous price will remain unchanged to ensure that it can be retrieved if necessary.
In order to achieve effective event sourcing, it is recommended to use an event store that provides strong consistency guarantees and uses optimistic concurrency control. In practice, this means that when multiple modifications occur simultaneously, only the initial modification can be appended to the stream. Subsequent modifications may need to be retried or may fail.
The above is the detailed content of Event flow and event sourcing. For more information, please follow other related articles on the PHP Chinese website!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

本篇文章带大家了解一下golang 的几种常用的基本数据类型,如整型,浮点型,字符,字符串,布尔型等,并介绍了一些常用的类型转换操作。

闭包(closure)是一个函数以及其捆绑的周边环境状态(lexical environment,词法环境)的引用的组合。 换而言之,闭包让开发者可以从内部函数访问外部函数的作用域。 闭包会随着函数的创建而被同时创建。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

在写 Go 的过程中经常对比这两种语言的特性,踩了不少坑,也发现了不少有意思的地方,下面本篇就来聊聊 Go 自带的 HttpClient 的超时机制,希望对大家有所帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
