I'm working on building kafka producer based on golang right now. I cannot sent any message from my producer. The panic shown as follow:
producer close, err: kafka: client has run out of available brokers to talk to(Is your cluster reachable?)
I test the kafka-console-consumer and kafka-console-producer, they are all work well on terminal. So, is there anything I missed? Here's the code:
package main
import (
"github.com/Shopify/sarama"
"log"
"os"
"strings"
)
var (
logger = log.New(os.Stderr, "[srama]", log.LstdFlags)
)
func main() {
sarama.Logger = logger
config := sarama.NewConfig()
config.Producer.Return.Successes = true
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, world!")
producer, err := sarama.NewSyncProducer(strings.Split("localhost:9092", ","), config)//default port
if err != nil {
logger.Println("Failed to produce message: %s", err)
os.Exit(500)
}
defer producer.Close()
partition, offset, err := producer.SendMessage(msg)
if err != nil {
logger.Println("Failed to produce message: ", err)
}
logger.Printf("partition=%d, offset=%d
", partition, offset)
}