我在WSL上运行Kafka。我正在尝试使像这样的简单生产者(我正在使用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();
}
}
但是有一个问题,当我尝试运行代码时,它显示此错误
[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
我什至在Windows上检查了端口
TCP 0.0.0.0:9092 0.0.0.0:0 LISTENING
我的问题是,是否不可能在Windows上以及在WSL上运行kafka时制作生产者?
最佳答案
您的Kafka经纪人在其通告的侦听器中使用本地主机名(AD17-2.localdomain
)。客户端最初在127.0.0.1:9092上与其连接时,代理将返回此地址以供其连接以生成消息。您的生产者无法解析此地址,并且失败。
要解决此问题,请在经纪人的server.properties
集中
advertised.listeners=PLAINTEXT://127.0.0.1:9092
listener.security.protocol.map=PLAINTEXT:PLAINTEXT
listeners=PLAINTEXT://0.0.0.0:9092
更多信息:https://rmoff.net/2018/08/02/kafka-listeners-explained/
关于java - 在WSL上运行Kafka,并在Windows上制作制作器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62256604/