kafka producer生產數據到kafka異常:Got error produce response with correlation id 16 on topic-partition...Error: NETWORK_EXCEPTION
1.問題描述
2017-09-13 15:11:30.656 o.a.k.c.p.i.Sender [WARN] Got error produce response with correlation id 25 on topic-partition test2-rtb-camp-pc-hz-5, retrying (299 attempts left). Error: NETWORK_EXCEPTION
2017-09-13 15:11:30.656 o.a.k.c.p.i.Sender [WARN] Got error produce response with correlation id 25 on topic-partition test2-rtb-camp-pc-hz-23, retrying (299 attempts left). Error: NETWORK_EXCEPTION
2017-09-13 15:11:30.657 o.a.k.c.p.i.Sender [WARN] Got error produce response with correlation id 25 on topic-partition test2-rtb-camp-pc-hz-14, retrying (299 attempts left). Error: NETWORK_EXCEPTION
2017-09-13 15:11:30.657 o.a.k.c.p.i.Sender [WARN] Got error produce response with correlation id 16 on topic-partition test2-rtb-camp-pc-hz-5, retrying (299 attempts left). Error: NETWORK_EXCEPTION
2.問題原因
$KAFKA_HOME/config/server.properties中有如下注釋:

大意是:kafka中每台broker會將hostname和port通知生產者和消費者,如果配置了listeners他就返回listeners的值,否則,就會調用java.net.InetAddress.getCanonicalHostName(),而這個方法返回的是linux這台主機的hostname,而不是IP。所以,在搭建kafka的時候需要配置listeners這個參數,最好也配上host.name=ip。