-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
34 lines (30 loc) · 871 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
package main
import (
"github.com/confluentinc/confluent-kafka-go/kafka"
"log"
)
const (
bootstrapServers = ConfluentServer
ccloudAPIKey = ConfluentApiKey
ccloudAPISecret = ConfluentSecret
)
// getConsumer returns a Kafka consumer for the given topic.
func getConsumer(topic string) *kafka.Consumer {
consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": bootstrapServers,
"sasl.mechanisms": "PLAIN",
"security.protocol": "SASL_SSL",
"sasl.username": ccloudAPIKey,
"sasl.password": ccloudAPISecret,
"group.id": "alert-consumer",
"auto.offset.reset": "earliest"})
if err != nil {
log.Fatalf("Failed to connect to Kafka %v", err)
}
// Read from Kafka
err = consumer.SubscribeTopics([]string{topic}, nil)
if err != nil {
log.Fatalf("failed to subscribe to topic: %v\n", err)
}
return consumer
}