java 在WSL上运行Kafka,在Windows上运行make producer

qlfbtfca  于 2023-05-27  发布在  Java
关注(0)|答案(3)|浏览(204)

我在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是不可能的?

yc0p9oo0

yc0p9oo01#

您的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/

svgewumm

svgewumm2#

以下是我在Apache Kafka 3.4.0中使用的方法:
1.更改Apache Kafka的server.properties
1.要求WSL 2优先选择IPV4而不是IPV6

  1. Java代码与生产者使用WSL 2 IP

第一步

在WSL 2 config/server.properties

listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://[::1]:9092
listener.security.protocol.map=PLAINTEXT:PLAINTEXT

第二步

您还必须告诉WSL 2首选IPV4而不是IPV6[1],如下所示:
1.编辑/etc/gai.conf
$ sudo vi /etc/gai.conf
1.在行的末尾取消最后3行的注解,你应该有这样的东西:

#
# scopev4  <mask>  <value>
#    Add another rule to the RFC 6724 scope table for IPv4 addresses.
#    By default the scope IDs described in section 3.2 in RFC 6724 are
#    used.  Changing these defaults should hardly ever be necessary.
#    The defaults are equivalent to:
#
scopev4 ::ffff:169.254.0.0/112  2
scopev4 ::ffff:127.0.0.0/104    2
scopev4 ::ffff:0.0.0.0/96       14

第三步

在Windows 10 Java代码中:
1.首先使用以下命令获取WSL 2的IP:hostname -I | cut -d' ' -f1

Properties props = new Properties();
        // WSL2 IP = 172.xx.xxx.xx:9092
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "172.xx.xxx.xx:9092");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
            ProducerRecord<String, String> message = new ProducerRecord<>("test-topic", "Hello, World!");
            producer.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
h9vpoimq

h9vpoimq3#

它适用于我的解决方案:Whit命令:“IP地址|grep eth0”在Ubuntu终端上我可以获取外部接口IP,我在advertised.listener上设置了这个IP,并使用命令:

netsh interface portproxy add v4tov4 listenport=9092 listenaddress=0.0.0.0 connectport=9092 connectaddress=XXX.XX.XX.XX

在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)

相关问题