Error using Kafka_2.11-0.9.0.0 producer API :
Exception in thread "main" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
Here is the producer code:
import java.util.Properties; /** * Kafka producer API */ public class KafkaProducer extends Thread { private String topic; private Producer<Integer, String> producer; public KafkaProducer(String topic) { this.topic = topic; Properties properties = new Properties(); properties.put("metadata.broker.list", KafkaProperties.BROKER_LIST); properties.put("serializer.class", "kafka.serializer.StringEncoder"); properties.put("request.required.acks", "1"); producer = new Producer<Integer, String>(new ProducerConfig(properties)); } @Override public void run() { int messageNum = 1; while (true) { String message = "message_" + messageNum; producer.send(new KeyedMessage<Integer, String>(topic, message)); System.out.println("send: " + message); messageNum++; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace (); } } } public static void main(String[] args) { new KafkaProducer(KafkaProperties.TOPIC).start(); } }
After some research, it was found that
# Hostname the broker will advertise to producers and consumers. If not set, it uses the # value for "host.name" if configured. Otherwise, it will use the value returned from # java.net.InetAddress.getCanonicalHostName(). advertised.host.name=hadoopIt must be configured here. The remote connection is based on this configuration to find the broker. The default is localhost.