gateway/external/kafka/kafka.go
crusader e1cd1e7e19 ing
2018-04-10 14:29:17 +09:00

64 lines
999 B
Go

package kafka
import (
"context"
"fmt"
"git.loafle.net/commons/logging-go"
occek "git.loafle.net/overflow/commons-go/config/external/kafka"
"github.com/segmentio/kafka-go"
)
var (
kafkaWriter *kafka.Writer
)
func InitPackage(config *occek.Kafka) {
kafkaWriter = kafka.NewWriter(kafka.WriterConfig{
Brokers: []string{"192.168.1.50:9092"},
Topic: "overflow-metric-topic",
Balancer: &kafka.LeastBytes{},
})
}
func StartPackage(config *occek.Kafka) {
if nil == config {
return
}
}
func StopPackage(config *occek.Kafka) {
if nil == config {
return
}
}
func DestroyPackage(config *occek.Kafka) {
if nil == config {
return
}
if err := kafkaWriter.Close(); nil != err {
logging.Logger().Errorf("%v", err)
}
}
func Write(key []byte, value []byte) error {
if nil == kafkaWriter {
return fmt.Errorf("Kafka client is not valid")
}
err := kafkaWriter.WriteMessages(context.Background(),
kafka.Message{
Key: key,
Value: value,
},
)
return err
}