-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkafka.go
38 lines (31 loc) · 949 Bytes
/
kafka.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
package main
import (
"context"
"github.com/Shopify/sarama"
"github.com/feiin/go-binlog-kafka/logger"
)
var (
producer sarama.SyncProducer
)
func InitKafka(kafkaAddrs []string) (err error) {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.MaxMessageBytes = 104857600
config.Producer.Partitioner = sarama.NewHashPartitioner
config.Producer.Flush.Messages = 1
config.Producer.Flush.MaxMessages = 1
config.Net.MaxOpenRequests = 1
config.Producer.Return.Successes = true
producer, err = sarama.NewSyncProducer(kafkaAddrs, config)
return
}
func sendKafkaMsg(ctx context.Context, Msg []byte, topicName string) (err error) {
msg := &sarama.ProducerMessage{}
msg.Topic = topicName
msg.Value = sarama.ByteEncoder(Msg)
if _, _, err := producer.SendMessage(msg); err != nil {
logger.ErrorWith(ctx, err).Interface("msg", msg).Msg("send kafka message error")
return err
}
return nil
}