KafkaProducer Connection拒绝了

时间:2016-05-20 11:29:41

标签: java apache-kafka kafka-producer-api

我试图将一些数据发送到kafka,但是当我运行我的代码时,我得到了

13:20:17.688 [kafka-producer-network-thread | producer-1] 
DEBUG org.apache.kafka.clients.NetworkClient - Node -1 disconnected.
13:20:17.784 [kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initialize connection to node -1 for sending metadata request
13:20:17.784 [kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initiating connection to node -1 at kafkaAdress:2181.
13:20:18.781 [kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.network.Selector - Connection with kafkaAdress/addressId disconnected
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
at org.apache.kafka.common.network.PlaintextTransportLayer.finishConnect(PlaintextTransportLayer.java:54)
at org.apache.kafka.common.network.KafkaChannel.finishConnect(KafkaChannel.java:72)
at org.apache.kafka.common.network.Selector.poll(Selector.java:274)
at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:256)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:216)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:128)
at java.lang.Thread.run(Unknown Source)

代码:

    String topic = "TST";
    Properties props = new Properties();
    props.put("bootstrap.servers", "kafkaAdress:2181");
    props.put("key.serializer",      "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    KafkaProducer<String, String> producer = new KafkaProducer<>(props);
    for(int i = 0; i < 100; i++)
        producer.send(new ProducerRecord<String, String>(topic, "TestMessage"));    
    producer.close();

有谁知道如何解决这个问题?

我使用kafka 0.9.1

2 个答案:

答案 0 :(得分:5)

由于Kafka 0.9,生产者API不再使用Zookeeper。

属性bootstrap.servers应包含用于建立与Kafka集群的初始连接的代理列表。

2181是动物园管理员的港口。代理的默认端口是9092。

答案 1 :(得分:1)

你的配置有误,bootstrap.server是Kafka经纪人的地址,而不是zookeeper。制作人总是使用经纪人的地址来发布消息。