-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathconsumer.go
106 lines (89 loc) · 2.52 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"crypto/tls"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/Shopify/sarama"
)
func main() {
brokerList := []string{os.Getenv("KAFKA_EVENTHUB_ENDPOINT")}
fmt.Println("Event Hubs broker", brokerList)
consumerGroupID := getEnv(consumerGroupIDEnvVar)
fmt.Println("Sarama client consumer group ID", consumerGroupID)
consumer, err := sarama.NewConsumerGroup(brokerList, consumerGroupID, getConfig())
if err != nil {
fmt.Println("error creating new consumer group", err)
os.Exit(1)
}
fmt.Println("new consumer group created")
eventHubsTopic := os.Getenv("EVENTHUBS_TOPIC")
fmt.Println("Event Hubs topic", eventHubsTopic)
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
err = consumer.Consume(ctx, []string{getEnv(eventHubsTopicEnvVar)}, messageHandler{})
if err != nil {
fmt.Println("error consuming from group", err)
os.Exit(1)
}
if ctx.Err() != nil {
//exit for loop
return
}
}
}()
close := make(chan os.Signal)
signal.Notify(close, syscall.SIGTERM, syscall.SIGINT)
fmt.Println("Waiting for program to exit")
<-close
cancel()
fmt.Println("closing consumer group....")
if err := consumer.Close(); err != nil {
fmt.Println("error trying to close consumer", err)
os.Exit(1)
}
fmt.Println("consumer group closed")
}
type messageHandler struct{}
func (h messageHandler) Setup(s sarama.ConsumerGroupSession) error {
fmt.Println("Partition allocation -", s.Claims())
return nil
}
func (h messageHandler) Cleanup(s sarama.ConsumerGroupSession) error {
fmt.Println("Consumer group clean up initiated")
return nil
}
func (h messageHandler) ConsumeClaim(s sarama.ConsumerGroupSession, c sarama.ConsumerGroupClaim) error {
for msg := range c.Messages() {
fmt.Printf("Message topic:%q partition:%d offset:%d\n", msg.Topic, msg.Partition, msg.Offset)
fmt.Println("Message content", string(msg.Value))
s.MarkMessage(msg, "")
}
return nil
}
func getConfig() *sarama.Config {
config := sarama.NewConfig()
config.Net.DialTimeout = 10 * time.Second
config.Net.SASL.Enable = true
config.Net.SASL.Mechanism = sarama.SASLTypeOAuth
config.Net.SASL.TokenProvider = NewTokenProvider()
config.Net.TLS.Enable = true
config.Version = sarama.V1_0_0_0
config.Net.TLS.Config = &tls.Config{
InsecureSkipVerify: true,
ClientAuth: 0,
}
return config
}
func getEnv(envName string) string {
value := os.Getenv(envName)
if value == "" {
fmt.Println("Environment variable " + envName + " is missing")
os.Exit(1)
}
return value
}