初期化のメインコードは次のとおりです。
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll // Wait for all in-sync replicas to ack the message
config.Producer.Retry.Max = 10 // Retry up to 10 times to produce the message
config.Producer.Return.Successes = true
config.Producer.Compression = sarama.CompressionSnappy // Compress messages
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Errors = true
producer, err := sarama.NewSyncProducer(brokerList, config)
if err != nil {
log.Printf("sarama.NewSyncProducer fail:%+v\n", err)
return err
}
プロダクションメッセージのメインコードは次のとおりです。
msg := sarama.ProducerMessage{
Topic: monitorInstance.topic,
Value: sarama.StringEncoder(resportString),
//Key: sarama.StringEncoder("monitor_data"),
}
partition, offset, err := (*producer).SendMessage(&msg)
if err != nil {
log.Printf("kafka SendMessage fail:%+v\n", err)
return
}
log.Printf("Your data is stored with unique identifier important/%d/%d\n", partition, offset)
次の方法を試しても機能しません:
1.プロデューサーを非同期から同期に変更します
2.メッセージのキーを増やします
3.トピックを置き換えてみます
最後にこの問題が見つかりました:
config.Producer.Compression = sarama.CompressionSnappy
は、この行が正常にコメント化されています。
圧縮できませんか?Tencent Cloudのエンジニアに尋ねて解決策を得た:
config.Version = sarama.V2_1_0_0
config.Producer.Compression = sarama.CompressionSnappy