ing
This commit is contained in:
parent
c94c37d093
commit
1430aa098c
46
external/kafka/kafka.go
vendored
46
external/kafka/kafka.go
vendored
|
@ -10,17 +10,36 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
kafkaWriter *kafka.Writer
|
kafkaWriters map[string]*kafka.Writer
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitPackage(config *occe.Kafka) {
|
func InitPackage(config *occe.Kafka) {
|
||||||
|
if nil == config {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if nil == config.Producers || 0 == len(config.Producers) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
kafkaWriter = kafka.NewWriter(kafka.WriterConfig{
|
kafkaWriters = make(map[string]*kafka.Writer)
|
||||||
Brokers: []string{"192.168.1.50:9092"},
|
|
||||||
Topic: "overflow-metric-topic",
|
for n, c := range config.Producers {
|
||||||
|
wc := kafka.WriterConfig{
|
||||||
|
Brokers: c.Brokers,
|
||||||
|
Topic: c.Topic,
|
||||||
Balancer: &kafka.LeastBytes{},
|
Balancer: &kafka.LeastBytes{},
|
||||||
})
|
MaxAttempts: c.MaxAttempts,
|
||||||
|
QueueCapacity: c.QueueCapacity,
|
||||||
|
BatchSize: c.BatchSize,
|
||||||
|
BatchTimeout: c.BatchTimeout,
|
||||||
|
ReadTimeout: c.ReadTimeout,
|
||||||
|
WriteTimeout: c.WriteTimeout,
|
||||||
|
RebalanceInterval: c.RebalanceInterval,
|
||||||
|
RequiredAcks: c.RequiredAcks,
|
||||||
|
Async: c.Async,
|
||||||
|
}
|
||||||
|
kafkaWriters[n] = kafka.NewWriter(wc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartPackage(config *occe.Kafka) {
|
func StartPackage(config *occe.Kafka) {
|
||||||
|
@ -42,17 +61,24 @@ func DestroyPackage(config *occe.Kafka) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kafkaWriter.Close(); nil != err {
|
for _, w := range kafkaWriters {
|
||||||
|
if err := w.Close(); nil != err {
|
||||||
logging.Logger().Errorf("%v", err)
|
logging.Logger().Errorf("%v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Write(key []byte, value []byte) error {
|
func Write(name string, key []byte, value []byte) error {
|
||||||
if nil == kafkaWriter {
|
if nil == kafkaWriters {
|
||||||
return fmt.Errorf("Kafka client is not valid")
|
return fmt.Errorf("Kafka client is not valid")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := kafkaWriter.WriteMessages(context.Background(),
|
w, ok := kafkaWriters[name]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Kafka client is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := w.WriteMessages(context.Background(),
|
||||||
kafka.Message{
|
kafka.Message{
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: value,
|
Value: value,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user