在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.官网上下载kafka安装包:http://kafka.apache.org/downloads.html 2.执行命令运行zookeeper 实例(单点): bin/zookeeper-server-start.sh config/zookeeper.properties package main import ( "github.com/Shopify/sarama" "log" "os" "strings" ) var logger = log.New(os.Stderr, "[TEST]", log.LstdFlags) func main(){ sarama.Logger = logger config := sarama.NewConfig() config.ClientID = "newsDataSource" config.Producer.RequiredAcks = sarama.WaitForAll config.Producer.Partitioner = sarama.NewRandomPartitioner msg := &sarama.ProducerMessage{} msg.Topic = "hello" msg.Partition = int32(-1) msg.Key = sarama.StringEncoder("key") msg.Value = sarama.ByteEncoder("hello") producer, err := sarama.NewSyncProducer(strings.Split("localhost:9092", ","), config) if err != nil { logger.Printf("Failed to produce message :%s", err ) os.Exit(500) } defer producer.Close() partition, offset, err := producer.SendMessage(msg) if err != nil { logger.Printf("Failed to produce message :%s", err ) } logger.Printf("partition:%d, offset: %d\n", partition, offset ) }
|
请发表评论