-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka-producer.go
46 lines (36 loc) · 944 Bytes
/
kafka-producer.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
package main
/*
import (
"context"
"os"
kafka "github.com/segmentio/kafka-go"
)
NOT REALLY USING ANY THING HERE
THIS IS JUST A MORE MANAGED CODE WAY OF DOING THIS
func getKafkaWriter(kafkaURL, topic string) *kafka.Writer {
return &kafka.Writer{
Addr: kafka.TCP(kafkaURL),
Topic: topic,
Balancer: &kafka.LeastBytes{},
}
}
*** save for TLS Implementation
w := kafka.Writer{
Addr: kafka.TCP("localhost:9092", "localhost:9093", "localhost:9094"),
Topic: "topic-A",
Balancer: &kafka.Hash{},
Transport: &kafka.Transport{
TLS: &tls.Config{},
},
}
func WriteEvent(topic string, payload string) (string, error) {
kafkaWriter := getKafkaWriter(os.Getenv("KAFKAURL"), topic)
defer kafkaWriter.Close()
msg := kafka.Message{
Key: []byte(topic),
Value: []byte(payload),
}
err := kafkaWriter.WriteMessages(context.Background(), msg)
return topic, err
}
*/