Soundarya Karthikeyan
Soundarya Karthikeyan

Reputation: 11

Connect to Kafka running in WSL2 Ubuntu

The below is my Kafka broker configuration

broker.id=1
port=9092
host.name=127.0.0.1
advertised.listeners=PLAINTEXT://127.0.0.1:9092
listeners=PLAINTEXT://127.0.0.1:9092

Console producer and consumer are working perfectly but when I try to connect through Java, it is throwing broker not available error. The Kafka broker is running and able to produce and consume messages through console.

Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
props.put(ProducerConfig.ACKS_CONFIG, "all");
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<String,String>(props);

producer.send(new ProducerRecord<String, String>("Sample","Hey","From java program"));
producer.close();
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Connection to node -1 (/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 127.0.0.1:9092 (id: -1 rack: null) disconnected

I'm using STS IDE to write Java program and using Java 8, Kafka 2.8.1 Windows OS and using Ubuntu 20.04.4 LTS to execute console producer and consumer.

Upvotes: 1

Views: 3399

Answers (1)

Tim
Tim

Reputation: 1471

Too late for this response, but hope this helps someone.

  • Identify the ip address of the WSL2 using ifconfig or ip addr or hostname -I

  • From the windows command prompt (as admin) run below command

> netsh interface portproxy add v4tov4 listenport=9092 listenaddress=0.0.0.0 connectport=9092 connectaddress=172.X.X.X

172.X.X.X is the Ip of the WSL2

With that change I was able to access the Kafka cluster.

Reference link

Upvotes: 4

Related Questions