今天在寫kafka生產者生成數據的程序並運行時,報以下錯誤:java
log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties). log4j:WARN Please initialize the log4j system properly. kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries. at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90) at kafka.producer.Producer.send(Producer.scala:76) at kafka.javaapi.producer.Producer.send(Producer.scala:33) at ProducerTest.main(TestProducer.java:21)
問題解決方案:api
1.檢查目錄:C:\Windows\System32\drivers\etc下的hosts文件,看IP地址與主機名是否配置正確。常常,發現沒有正確配置,修改後再運行,正常。app
192.168.2.212 hadoop01
192.168.2.224 hadoop02
192.168.2.226 hadoop03dom
2.把config文件夾下的server.properties文件裏#host.name=localhost的註釋去掉,而後運行async
3.把config文件夾下的server.properties中的如下兩個屬性oop
zookeeper.connect=localhost:
2181
改爲zookeeper.connect=192.168.2.212
:
2181
ui
以及默認註釋掉的
#host.name=localhost改爲host.name=192.168.2.212
import java.util.*; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; public class ProducerDemo3 { public static void main(String[] args) { long events = 400; Random rnd = new Random(); Properties props = new Properties(); props.put("metadata.broker.list", "192.168.2.212:9092,192.168.224:9092,192.168.226:9092"); props.put("serializer.class", "kafka.serializer.StringEncoder"); props.put("partitioner.class", "com.vrv.kafka.SimplePartitioner"); props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props); Producer<String, String> producer = new Producer<String, String>(config); long start=System.currentTimeMillis(); for (long nEvents = 0; nEvents < events; nEvents++) { String ip1 = "192.168.2." + rnd.nextInt(255); String ip2 = "192.168.2." + rnd.nextInt(255); String ip3 = "192.168.2." + rnd.nextInt(255); String msg = ip1 + " " + ip2 + " " + ip3 ; KeyedMessage<String, String> data = new KeyedMessage<String, String>("test", ip1, msg); producer.send(data); } producer.close(); System.out.println("耗時:" + (System.currentTimeMillis() - start)/1000); } }