/
kafka.go
48 lines (45 loc) · 991 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
39
40
41
42
43
44
45
46
47
48
package database
import (
"encoding/json"
"fmt"
"github.com/apex/log"
"github.com/segmentio/kafka-go"
"github.com/xulei324/spiderlab-go-sdk/entity"
"runtime/debug"
)
func GetKafkaConnection(ds entity.DataSource) (conn *kafka.Conn, err error) {
conn, err = kafka.Dial(
"tcp",
fmt.Sprintf("%s:%s", ds.Host, ds.Port),
)
if err != nil {
log.Errorf("dial kafka error: " + err.Error())
debug.PrintStack()
return conn, err
}
return conn, nil
}
func SendKafkaMsg(ds entity.DataSource, item entity.Item) (err error) {
conn, err := GetKafkaConnection(ds)
if err != nil {
return err
}
msgStr, err := json.Marshal(&item)
if err != nil {
log.Errorf("marshal json error: " + err.Error())
debug.PrintStack()
return err
}
if _, err := conn.WriteMessages(
kafka.Message{
Topic: ds.Database,
Partition: 0,
Value: msgStr,
},
); err != nil {
log.Errorf("send kafka message error: " + err.Error())
debug.PrintStack()
return err
}
return nil
}