forked from anstoli/go-kafka-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_example_test.go
60 lines (52 loc) · 1.26 KB
/
consumer_example_test.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package kafkaavro_test
import (
"log"
"net/url"
"github.com/confluentinc/confluent-kafka-go/kafka"
kafkaavro "github.com/mycujoo/go-kafka-avro/v2"
)
func ExampleNewConsumer() {
srURL, err := url.Parse("http://localhost:8081")
if err != nil {
log.Fatal(err)
}
type val struct {
FieldName string `avro:"field_name"`
}
c, err := kafkaavro.NewConsumer(
[]string{"topic1"},
func(topic string) interface{} {
return val{}
},
kafkaavro.WithKafkaConfig(&kafka.ConfigMap{
"bootstrap.servers": "localhost:29092",
"security.protocol": "ssl",
"socket.keepalive.enable": true,
"enable.auto.commit": false,
"ssl.key.location": "/path/to/service.key",
"ssl.certificate.location": "/path/to/service.cert",
"ssl.ca.location": "/path/to/ca.pem",
"group.id": "some-group-id",
"session.timeout.ms": 6000,
"auto.offset.reset": "earliest",
}),
kafkaavro.WithSchemaRegistryURL(srURL),
kafkaavro.WithEventHandler(func(event kafka.Event) {
log.Println(event)
}),
)
for {
msg, err := c.ReadMessage(5000)
if err != nil {
log.Println("Error", err)
continue
}
if msg == nil {
continue
}
switch v := msg.Value.(type) {
case val:
log.Println(v)
}
}
}