You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
799 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package KafkaUtil
import (
"dsSzxy/Utils/ConfigUtil"
"fmt"
"github.com/Shopify/sarama"
)
func SendKafkaMsg(topic string, content string) error {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Successes = true
config.Producer.Return.Errors = true
producer, err := sarama.NewAsyncProducer([]string{ConfigUtil.KafkaAddrs}, config)
if err != nil {
return err
}
defer producer.AsyncClose()
msg := &sarama.ProducerMessage{
Topic: topic,
}
msg.Value = sarama.ByteEncoder(content)
producer.Input() <- msg
select {
case suc := <-producer.Successes():
fmt.Printf(suc.Topic + "Topic发送成功")
case fail := <-producer.Errors():
return fail.Err
}
return nil
}