-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
40 lines (34 loc) · 864 Bytes
/
consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"context"
"fmt"
"log"
"strings"
kafka "github.com/segmentio/kafka-go"
)
func getKafkaReader(kafkaURL, topic, groupID string) *kafka.Reader {
brokers := strings.Split(kafkaURL, ",")
return kafka.NewReader(kafka.ReaderConfig{
Brokers: brokers,
GroupID: groupID,
Topic: topic,
MinBytes: 10, // 10B
MaxBytes: 10e6, // 10MB
})
}
func main() {
// get kafka reader using environment variables.
kafkaURL := "0.0.0.0:9093"
topic := "my-topic"
groupID := "testg1"
reader := getKafkaReader(kafkaURL, topic, groupID)
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))
}
}