Kafka Producer Not Able to connect via Code

Viewed 985

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);
1 Answers

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 !

Related