go-kafka

news/2024/11/17 7:34:36/

go kafka包

本文使用的是kafka-go 6.5k 这个包 其他包参考:

我们在细分市场中非常依赖GO和Kafka。不幸的是,在撰写本文时,Kafka的GO客户库的状态并不理想。可用选项是:

萨拉玛(Sarama) 10k,这是迄今为止最受欢迎的,但很难与之合作。它的记录不足,API暴露了Kafka协议的低级概念,并且不支持诸如上下文之类的GO。它还将所有值传递给引起大量动态内存分配,更频繁的垃圾收集和更高的内存使用情况的指针。

Confluent-kafka-Go 4.4k是围绕librdkafka的基于CGO的包装器,这意味着它将使用该软件包的所有GO代码引入了C库的依赖关系。它的文档比Sarama要好得多,但仍然缺乏对GO上下文的支持。

Goka2.2k 是GO的最新Kafka客户端,专注于特定的用法模式。它提供了将KAFKA用作服务之间的消息传递的消息,而不是事件的有序日志,但这并不是我们在细分市场的典型用例。该包还取决于与Kafka的所有互动的Sarama。

这就是Kafka-Go发挥作用的地方。它提供了低水平和高级API,可与Kafka进行交互,反映概念并实现GO标准库的接口,以使其易于使用并与现有软件集成。

go操作kafka

 go get github.com/segmentio/kafka-go

简单的生产者:

package mainimport ("context""github.com/segmentio/kafka-go""log""time"
)func main() {// to produce messagestopic := "my-topic"partition := 0//默认没有主题会创建conn, err := kafka.DialLeader(context.Background(), "tcp", "192.168.59.131:9092", topic, partition)if err != nil {log.Fatal("failed to dial leader:", err)}conn.SetWriteDeadline(time.Now().Add(10 * time.Second))_, err = conn.WriteMessages(kafka.Message{Value: []byte("one!")},kafka.Message{Value: []byte("two!")},kafka.Message{Value: []byte("three!")},)if err != nil {log.Fatal("failed to write messages:", err)}if err := conn.Close(); err != nil {log.Fatal("failed to close writer:", err)}
}

简单的消费者:

package mainimport ("context""fmt""github.com/segmentio/kafka-go""log""time"
)func main() {// to consume messagestopic := "my-topic"partition := 0conn, err := kafka.DialLeader(context.Background(), "tcp", "192.168.59.131:9092", topic, partition)if err != nil {log.Fatal("failed to dial leader:", err)}conn.SetReadDeadline(time.Now().Add(10 * time.Second))batch := conn.ReadBatch(10e3, 1e6) // fetch 10KB min, 1MB maxb := make([]byte, 10e3) // 10KB max per messagefor {n, err := batch.Read(b)if err != nil {break}fmt.Println(n, string(b[:n]))}if err := batch.Close(); err != nil {log.Fatal("failed to close batch:", err)}if err := conn.Close(); err != nil {log.Fatal("failed to close connection:", err)}
}

输出

4 one!
4 two!
6 three!
2023/08/22 16:48:02 failed to close batch:[7] Request Timed Out: the request exceeded the user-specified time limit in the request
exit status 1

连接

会自动创建主题:

// to create topics when auto.create.topics.enable='true' 
conn, err := kafka.DialLeader(context.Background(), "tcp", "localhost:9092", "my-topic", 0)
if err != nil {panic(err.Error())
}

连接 手动创建主题:

package mainimport ("github.com/segmentio/kafka-go""net""strconv"
)// kafka
func main() {// to create topics when auto.create.topics.enable='false'topic := "my-topic"conn, err := kafka.Dial("tcp", "192.168.59.131:9092")if err != nil {panic(err.Error())}defer conn.Close()controller, err := conn.Controller()if err != nil {panic(err.Error())}var controllerConn *kafka.ConncontrollerConn, err = kafka.Dial("tcp", net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port)))if err != nil {panic(err.Error())}defer controllerConn.Close()topicConfigs := []kafka.TopicConfig{{Topic:             topic,NumPartitions:     1,ReplicationFactor: 1,},}err = controllerConn.CreateTopics(topicConfigs...)if err != nil {panic(err.Error())}//---------------读取主题------------------pl, err := conn.ReadPartitions()if err != nil {panic(err.Error())}for _, p := range pl {fmt.Println(p.Topic)}
}

Reader 消费者

官网介绍:
A Reader is another concept exposed by the kafka-go package, which intends to make it simpler to implement the typical use case of consuming from a single topic-partition pair. A Reader also automatically handles reconnections and offset management, and exposes an API that supports asynchronous cancellations and timeouts using Go contexts.

Note that it is important to call Close() on a Reader when a process exits. The kafka server needs a graceful disconnect to stop it from continuing to attempt to send messages to the connected clients. The given example will not call Close() if the process is terminated with SIGINT (ctrl-c at the shell) or SIGTERM (as docker stop or a kubernetes restart does). This can result in a delay when a new reader on the same topic connects (e.g. new process started or new container running). Use a signal.Notify handler to close the reader on process shutdown.
阅读器(Reader)是 kafka-go 软件包暴露的另一个概念,它旨在简化从单个主题-分区对中消费的典型用例的实现。阅读器还会自动处理重新连接和偏移管理,并提供一个 API,使用 Go 上下文支持异步取消和超时。

需要注意的是,在进程退出时调用 Reader 上的 Close() 非常重要。kafka 服务器需要优雅地断开连接,以阻止它继续尝试向已连接的客户端发送消息。如果进程被 SIGINT(在 shell 中按 ctrl-c)或 SIGTERM(docker stop 或 kubernetes 重启)终止,给出的示例将不会调用 Close()。当同一主题上有新的阅读器连接时(例如,新进程启动或新容器运行),这可能会导致延迟。使用 signal.Notify 处理程序在进程关闭时关闭阅读器。

package mainimport ("context""fmt""github.com/segmentio/kafka-go""log"
)// kafka
func main() {// make a new reader that consumes from topic-A, partition 0, at offset 42r := kafka.NewReader(kafka.ReaderConfig{Brokers:   []string{"192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"},Topic:     "topic-A",Partition: 0,MaxBytes:  10e6, // 10MB})r.SetOffset(4)fmt.Println("start")for {m, err := r.ReadMessage(context.Background())if err != nil {break}fmt.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))}if err := r.Close(); err != nil {log.Fatal("failed to close reader:", err)}
}

Consumer Groups 消费者组

kafka-go 还支持 Kafka 消费者组,包括代理管理的偏移量。要启用消费者组,只需在 ReaderConfig 中指定 GroupID。
使用消费者组时,ReadMessage 会自动提交偏移量

package mainimport ("context""fmt""github.com/segmentio/kafka-go""log"
)// kafka
func main() {// make a new reader that consumes from topic-A, partition 0, at offset 42r := kafka.NewReader(kafka.ReaderConfig{Brokers:   []string{"192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"},Topic:     "topic-A",Partition: 0,MaxBytes:  10e6, // 10MBGroupID:   "AAA",})fmt.Println("start")for {m, err := r.ReadMessage(context.Background())if err != nil {break}fmt.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))}if err := r.Close(); err != nil {log.Fatal("failed to close reader:", err)}
}

使用消费者组时有一些限制:
(*Reader).SetOffset 在设置 GroupID 时会返回错误信息
(*Reader).Offset在设置GroupID时将始终返回-1
(*Reader).Lag在GroupID被设置时总是返回-1
(*Reader).ReadLag 将在设置 GroupID 时返回错误信息
(*Reader).Stats在GroupID被设置时将返回-1的分区。

Explicit Commits 显式提交

没 CommitMessages 提交偏移量没变下次还会读到

package mainimport ("context""fmt""github.com/segmentio/kafka-go""log"
)// kafka
func main() {r := kafka.NewReader(kafka.ReaderConfig{Brokers:   []string{"192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"},Topic:     "topic-A",Partition: 0,MaxBytes:  10e6, // 10MBGroupID:   "AAA",})fmt.Println("start")ctx := context.Background()for {m, err := r.FetchMessage(ctx)if err != nil {break}fmt.Printf("message at topic/partition/offset %v/%v/%v: %s = %s\n", m.Topic, m.Partition, m.Offset, string(m.Key), string(m.Value))if err := r.CommitMessages(ctx, m); err != nil {log.Fatal("failed to commit messages:", err)}}if err := r.Close(); err != nil {log.Fatal("failed to close reader:", err)}
}

Managing Commits 管理提交 周期性提交

通过在 ReaderConfig 上设置 CommitInterval 来周期性地向 Kafka 提交偏移量。

r := kafka.NewReader(kafka.ReaderConfig{Brokers: []string{"192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"},Topic:   "topic-A",Partition:      0,MaxBytes:       10e6, // 10MBGroupID:        "AAA",CommitInterval: time.Second,// flushes commits to Kafka every second})

Writer 生产者

To produce messages to Kafka, a program may use the low-level Conn API, but the package also provides a higher level Writer type which is more appropriate to use in most cases as it provides additional features:

  • Automatic retries and reconnections on errors.
  • Configurable distribution of messages across available partitions.
  • Synchronous or asynchronous writes of messages to Kafka.
  • Asynchronous cancellation using contexts.
  • Flushing of pending messages on close to support graceful shutdowns.
  • Creation of a missing topic before publishing a message. Note! it was the default behaviour up to the version v0.4.30

要向 Kafka 发送消息,程序可以使用底层的 Conn API,但软件包也提供了更高级的 Writer 类型,在大多数情况下更适合使用,因为它提供了更多的功能:

  • 出错时自动重试和重新连接。
  • 可配置的可用分区信息分配。
  • 将消息同步或异步写入 Kafka。
  • 使用上下文进行异步取消。
  • 在关闭时刷新待处理消息,以支持优雅关机。
  • 在发布消息前创建缺失的主题。注意!这是 v0.4.30 之前的默认行为。
package mainimport ("context""github.com/segmentio/kafka-go""log"
)func main() {// make a writer that produces to topic-A, using the least-bytes distribution// 使用最少字节分布,制作向主题-A 发送信息的写入器w := &kafka.Writer{Addr:     kafka.TCP("192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"),Topic:    "topic-A",Balancer: &kafka.LeastBytes{},}err := w.WriteMessages(context.Background(),kafka.Message{Key:   []byte("Key-A"),Value: []byte("Hello World!One!"),},kafka.Message{Key:   []byte("Key-B"),Value: []byte("Hello World!Two!"),},kafka.Message{Key:   []byte("Key-C"),Value: []byte("Hello World!Three!"),},)if err != nil {log.Fatal("failed to write messages:", err)}if err := w.Close(); err != nil {log.Fatal("failed to close writer:", err)}
}

自动创建主题

package mainimport ("context""errors""github.com/segmentio/kafka-go""log""time"
)func main() {// Make a writer that publishes messages to topic-A.// The topic will be created if it is missing.w := &kafka.Writer{Addr:                   kafka.TCP("192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"),Topic:                  "topic-A",AllowAutoTopicCreation: true,}messages := []kafka.Message{{Key:   []byte("Key-A"),Value: []byte("Hello One!"),},{Key:   []byte("Key-B"),Value: []byte("Hello Two!"),},{Key:   []byte("Key-C"),Value: []byte("Hello Three!"),},}var err errorconst retries = 3for i := 0; i < retries; i++ {ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)defer cancel()// 尝试在发布消息前创建主题err = w.WriteMessages(ctx, messages...)if errors.Is(err, kafka.LeaderNotAvailable) || errors.Is(err, context.DeadlineExceeded) {time.Sleep(time.Millisecond * 250)continue}if err != nil {log.Fatalf("unexpected error %v", err)}break}if err := w.Close(); err != nil {log.Fatal("failed to close writer:", err)}
}

多个主题

通常,WriterConfig.Topic 用于初始化单主题写入器。通过排除该特定配置,您可以通过设置 Message.Topic.WriterConfig.Topic 来按消息定义主题。

package mainimport ("context""github.com/segmentio/kafka-go""log"
)func main() {w := &kafka.Writer{Addr: kafka.TCP("192.168.59.131:9092", "192.168.59.131:9093", "192.168.59.131:9094"),// NOTE: When Topic is not defined here, each Message must define it instead.// 注意:如果这里没有定义主题,则每条信息都必须定义主题Balancer: &kafka.LeastBytes{},}err := w.WriteMessages(context.Background(),// NOTE: Each Message has Topic defined, otherwise an error is returned.// 注意:每条信息都定义了主题,否则将返回错误信息。kafka.Message{Topic: "topic-A",Key:   []byte("Key-A"),Value: []byte("Hello World!"),},kafka.Message{Topic: "topic-B",Key:   []byte("Key-B"),Value: []byte("One!"),},kafka.Message{Topic: "topic-C",Key:   []byte("Key-C"),Value: []byte("Two!"),},)if err != nil {log.Fatal("failed to write messages:", err)}if err := w.Close(); err != nil {log.Fatal("failed to close writer:", err)}
}

http://www.ppmy.cn/news/1055699.html

相关文章

精准高效农业作业,植保无人机显身手

中国作为农业大国&#xff0c;拥有约18亿亩的农田&#xff0c;每年都需要进行种子喷洒和农药施用等农业作业&#xff0c;对于普通农户来说&#xff0c;这是一项耗时耗力的工程&#xff0c;同时&#xff0c;人工喷洒农药极易造成农药慢性中毒&#xff0c;对农民的身体健康产生极…

FairyGUI编辑器的弹窗操作【插件】

之前在FairyGUI编辑器菜单扩展中&#xff0c;我使用了App.Alert("复制失败")来提示操作是否成功。这篇则会说一下我们可以使用的弹窗提示&#xff0c;以及做到类似资源发布成功时的“发布成功”飘窗。 打开APP的API脚本&#xff0c;可以看到有很多公开方法&#xff…

设计模式(11)观察者模式

一、概述&#xff1a; 1、定义&#xff1a;观察者模式定义了一种一对多的依赖关系&#xff0c;让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象&#xff0c;使它们能够自动更新自己。 2、结构图&#xff1a; public interface S…

设计模式二十二:策略模式(Strategy Pattern)

定义一系列算法&#xff0c;将每个算法封装成独立的对象&#xff0c;并使这些对象可互相替换。这使得在运行时可以动态地选择算法&#xff0c;而不必改变使用算法的客户端代码。策略模式的主要目标是将算法的定义与使用分离&#xff0c;使得客户端可以根据需要灵活地选择和切换…

多维时序 | MATLAB实现BiTCN-BiGRU-Attention多变量时间序列预测

多维时序 | MATLAB实现SABO-CNN-GRU-Attention多变量时间序列预测 目录 多维时序 | MATLAB实现SABO-CNN-GRU-Attention多变量时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 多维时序 | MATLAB实现BiTCN-BiGRU-Attention多变量时间序列预测。 模型描…

使用proxman对iOS真机进行抓包

1 打开手机的safari 输入地址 http://proxy.man/ssl 2 下载证书代开设置页面&#xff0c;安装证书 设置信任证书 打开手机设置 &#xff0c;点击通用 点击关于本机、 点击证书信任设置 打开信任设置开关 4 设置手机代理 查看需要设置的代理地址 打开界面 在手机中按…

Postgresql+Postgis安装教程

Windows 下载地址 Postgresql&#xff1a;https://www.enterprisedb.com/downloads/postgres-postgresql-downloads Postgis&#xff1a;https://winnie.postgis.net/download/windows/ 我这里安装Postgresql13&#xff0c;所以对应Postgis也选择pg13版本 首先安装Postgresql…

一生一芯8——在github上添加ssh key

为在github上下载代码框架&#xff0c;这里在github上使用ssh key进行远程连接&#xff0c;方便代码拉取 参照博客https://blog.csdn.net/losthief/article/details/131502734 本机 系统ubuntu22.04 git 版本2.34.1 本人是第一次配置&#xff0c;没有遇到奇奇怪怪的错误&…