其他分享
首页 > 其他分享> > kafka使用示例

kafka使用示例

作者:互联网

示例代码如下:

package main

import (
	"fmt"
	"github.com/Shopify/sarama"
)
func main() {
	config := sarama.NewConfig()
	config.Producer.RequiredAcks = sarama.WaitForAll
	config.Producer.Partitioner = sarama.NewRandomPartitioner
	config.Producer.Return.Successes = true

	msg := &sarama.ProducerMessage{}
	msg.Topic ="nginx_log"
	msg.Value = sarama.StringEncoder("this is a good test,my message is good")

	client,err := sarama.NewSyncProducer([]string{"192.168.1.19:9092"},config)
	if err != nil{
		fmt.Println("producer close,err:",err)
		return
	}

	defer client.Close()

	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)
}

  

标签:err,sarama,fmt,示例,pid,kafka,使用,msg,config
来源: https://www.cnblogs.com/marshhu/p/12072716.html