Last active
November 12, 2024 08:53
-
-
Save zengxs/79de368fa3dcdd46ffff97eba6a0bc98 to your computer and use it in GitHub Desktop.
Golang Kafka Consumer with SASL Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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