新建了一個kafka集群,在window下寫了一個簡單的producer做測試,結果遇到了消息發送失敗的問題,代碼如下:
Properties props = new Properties(); props.put("metadata.broker.list", "192.168.1.107:6667"); props.put("serializer.class", "kafka.serializer.StringEncoder"); // props.put("partitioner.class", "example.producer.SimplePartitioner"); props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props); Producer producer = new Producer<String, String>(config); String payload = String.format("ayuananybc%s", "test"); KeyedMessage<String, String> data = new KeyedMessage<String, String>("mytopic", payload); producer.send(data);
經過查看kafka broker的參數如下:
於是猜測是不是本地運行的時候不認識Slave1(192.168.1.107)? 然后我在hosts中加上:192.168.1.107 Slave1。
結果就可以了:
其實原因是讓kafka知道zookeeper的監聽地址,我們不修改hosts,加上如下配置也是可以的: