I’m running Kafka on WSL. I’m trying to make simple producer like this (I’m using intellj)
import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.serialization.StringSerializer; import java.util.Properties; public class ProducerDemo { public static void main(String[] args) { String bootstrapServers = "127.0.0.1:9092"; //create Producer properties Properties properties = new Properties(); properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServers); properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName()); //create the producer KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties); //create a producer record ProducerRecord<String,String> record = new ProducerRecord<String, String>("first_topic","hallo world"); //send data producer.send(record); //flush + close producer.flush(); producer.close(); } }
but there’s a problem, when I try to run the code it shows this error
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Error connecting to node AD17-2.localdomain:9092 (id: 0 rack: null) java.net.UnknownHostException: No such host is known (AD17-2.localdomain) at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932) at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1505) at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851) at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1495) at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1354) at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1288) at org.apache.kafka.clients.ClientUtils.resolve(ClientUtils.java:110) at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.currentAddress(ClusterConnectionStates.java:403) at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.access$200(ClusterConnectionStates.java:363) at org.apache.kafka.clients.ClusterConnectionStates.currentAddress(ClusterConnectionStates.java:151) at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:955) at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:293) at org.apache.kafka.clients.producer.internals.Sender.sendProducerData(Sender.java:350) at org.apache.kafka.clients.producer.internals.Sender.runOnce(Sender.java:323) at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:239) at java.base/java.lang.Thread.run(Thread.java:832) Process finished with exit code -1
i even checked the port on my Windows
TCP 0.0.0.0:9092 0.0.0.0:0 LISTENING
my question is, is it impossible to make producer on windows and while running kafka on WSL ?
Advertisement
Answer
Your Kafka broker is using the local hostname (AD17-2.localdomain
) in its advertised listener. When you client connects to it initially on 127.0.0.1:9092 the broker returns this address for it to connect to for producing messages. Your producer fails to resolve this address, and fails.
To fix it, in the broker’s server.properties
set
advertised.listeners=PLAINTEXT://127.0.0.1:9092 listener.security.protocol.map=PLAINTEXT:PLAINTEXT listeners=PLAINTEXT://0.0.0.0:9092
More info: https://rmoff.net/2018/08/02/kafka-listeners-explained/