开发环境解决 kafka Failed to send messages after 3 tries

时间:2023-02-05 14:34:51

新建了一个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的参数如下:
开发环境解决 kafka Failed to send messages after 3 tries

于是猜测是不是本地运行的时候不认识Slave1(192.168.1.107)? 然后我在hosts中加上:192.168.1.107  Slave1。

结果就可以了:

开发环境解决 kafka Failed to send messages after 3 tries

其实原因是让kafka知道zookeeper的监听地址,我们不修改hosts,加上如下配置也是可以的:

开发环境解决 kafka Failed to send messages after 3 tries