Skip to content

Instantly share code, notes, and snippets.

@zengxs
Last active November 12, 2024 08:53
Show Gist options
  • Save zengxs/79de368fa3dcdd46ffff97eba6a0bc98 to your computer and use it in GitHub Desktop.
Save zengxs/79de368fa3dcdd46ffff97eba6a0bc98 to your computer and use it in GitHub Desktop.
Golang Kafka Consumer with SASL Auth
package main
import (
"context"
"fmt"
"log"
"strings"
// An EOF error always occurs when using the IBM/sarama, but segmentio/kafka-go works fine
"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl/scram"
)
var (
brokers = ""
topic = ""
groupId = ""
scramUsername = ""
scramPassword = ""
)
func main() {
saslMechanism, err := scram.Mechanism(scram.SHA512, scramUsername, scramPassword)
if err != nil {
log.Fatal(err)
}
reader := kafka.NewReader(kafka.ReaderConfig{
Brokers: strings.Split(brokers, ","),
GroupID: groupId,
Topic: topic,
MinBytes: 10e3, // 10KB
MaxBytes: 10e6, // 10MB
Dialer: &kafka.Dialer{
SASLMechanism: saslMechanism,
},
})
defer reader.Close()
fmt.Println("start consuming ... !!")
for {
m, err := reader.ReadMessage(context.Background())
if err != nil {
log.Fatalln(err)
}
fmt.Printf("message at topic:%v partition:%v offset:%v %s = %s\n", m.Topic, m.Partition, m.Offset, string(m.Key), string(m.Value))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment