2

I'm unable to connect to my locally running Kafka instance via code - I can connect successfully using Kafka-Console-Producer and Kafka-Console-Consumer but when I use Kafka Java SDK and simply use the Java Producer to connect to & produce any message, it fails with the following error:

[Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected

Not sure if that matters but I'm using WSL2 on Windows and Kafka 2.12-2.5.0 to test this. How can I enable more verbose logging and debug this ?

Snippet of my Producer code

Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

ProducerRecord<String, String> record = new ProducerRecord<String, String>("first_topic","first_value");

KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);
kafkaProducer.send(record);
rahul sharma
  • 505
  • 4
  • 17
Agraj
  • 466
  • 5
  • 19
  • i am trying to do the same thing, connect from a kafka client in windows to a kafka instance running in WSL2. I get a similar error. But I do not have the option of running my client in wsl2 since it is a windows app. Does anyone know how to fix / configure this scenario so a windows kafka client can connect to a wsl2 kafka server? – Ed Ost Oct 31 '20 at 19:41
  • I had the same problem. I could only fix it by going back to wsl1, instead of wsl2. Seems like wsl2 is not able to connect to windows localhost. – Augusto Acioli Pinho Vanderley Nov 13 '20 at 06:20

1 Answers1

3

Sorry for the false alarm. My issue was somewhat linked to WSL2 - I was starting my ZK and Kafka Broker in WSL2 in the linux distro but was mistakenly trying to access the broker from a Java App running in Windows !

When I try to run that Java app from Linux itself. It works !

Agraj
  • 466
  • 5
  • 19