java KafkaProducer 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37345551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:25:21  来源:igfitidea点击:

KafkaProducer Connection refused

javaapache-kafkakafka-producer-api

提问by Lukaszaq

I trying to send some data to kafka, but when i run my code i got

我试图向 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)

Code:

代码:

    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();

Does anyone know how to solve this?

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

I use kafka 0.9.1

我使用卡夫卡 0.9.1

回答by fhussonnois

Since Kafka 0.9 the producer API no longer uses Zookeeper.

从 Kafka 0.9 开始,生产者 API 不再使用 Zookeeper。

The property bootstrap.servers should contain a list of brokers for establishing the initial connection to the Kafka cluster.

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

2181 is the zookeeper port. The default port for a broker is 9092.

2181是zookeeper端口。代理的默认端口是 9092。

回答by Darwin.Lau

You have some mistake on your configuration, bootstrap.server is the Kafka broker's address, not zookeeper. Producer always use the broker's address to publish messages.

您的配置有误,bootstrap.server 是 Kafka 代理的地址,而不是 zookeeper。生产者总是使用代理的地址来发布消息。