我在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
我的问题是,在WSL上运行Kafka的同时,在windows上制作producer是不可能的?
3条答案
按热度按时间yc0p9oo01#
您的Kafka代理在其通告的侦听器中使用本地主机名(
AD17-2.localdomain
)。当您的客户端最初在127.0.0.1:9092上连接到它时,代理将返回此地址供其连接以生成消息。您的生产者无法解析此地址,因此失败。要修复它,请在代理的
server.properties
集中更多信息:https://rmoff.net/2018/08/02/kafka-listeners-explained/
svgewumm2#
以下是我在Apache Kafka 3.4.0中使用的方法:
1.更改Apache Kafka的
server.properties
1.要求WSL 2优先选择IPV4而不是IPV6
第一步
在WSL 2
config/server.properties
中第二步
您还必须告诉WSL 2首选IPV4而不是IPV6[1],如下所示:
1.编辑/etc/gai.conf
$ sudo vi /etc/gai.conf
1.在行的末尾取消最后3行的注解,你应该有这样的东西:
第三步
在Windows 10 Java代码中:
1.首先使用以下命令获取WSL 2的IP:
hostname -I | cut -d' ' -f1
h9vpoimq3#
它适用于我的解决方案:Whit命令:“IP地址|grep eth0”在Ubuntu终端上我可以获取外部接口IP,我在advertised.listener上设置了这个IP,并使用命令:
在Windows cmd上,我可以转发端口。使用命令“netstat -ab”,我可以在Windows cmd上看到IP和端口TCP 0.0.0.0:9092。它工作正常!Running Kafka Confluent Platform on WSL 2 (Ubuntu Distribution) and Spring application on Windows (Broker may not be available)