1. 生产者生产消息
func main() {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll // 发送完数据需要leader和follow都确认
config.Producer.Partitioner = sarama.NewRandomPartitioner // 新选出⼀个partition
config.Producer.Return.Successes = true // 成功交付的消息将在success channel返回
// sasl认证
config.Net.SASL.Enable = true
config.Net.SASL.User = "admin"
config.Net.SASL.Password = "admin"
// 连接kafka
client, err := sarama.NewSyncProducer([]string{"192.168.67.128:9092", "192.168.67.129:9092", "192.168.67.130:9092"}, config)
if err != nil {
fmt.Println("producer close, err:", err)
return
}
defer client.Close()
// 构造⼀个消息
msg := &sarama.ProducerMessage{}
msg.Topic = "ffcs"
msg.Value = sarama.StringEncoder("hello kafka")
// 发送消息
pid, offset, err := client.SendMessage(msg)
if err != nil {
fmt.Println("send message failed,", err)
return
}
fmt.Printf("pid:%v offset:%v\n", pid, offset)
}
2.异步发送
func main() {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Partitioner = sarama.NewRandomPartitio