无法从windows生成在wsl 2上运行的kafka主题

vsaztqbk  于 2021-06-04  发布在  Kafka
关注(0)|答案(2)|浏览(519)

我正在ubuntuwsl2上成功运行最新的kafka。我可以在wsl上运行的ubuntu中启动zookeeper、kafka服务器、创建主题、控制台生成和控制台消费。然而,当我进入windows上的intellij并创建一个简单的java生产者时,它似乎无法连接到代理
版本和主机名

Java version: 1.8
    Kafka Version: 2.6
    hostname (from Ubuntu): KDAAPPDEV04
    hostname (from Powershell): KDAAPPDEV04
    java.net.InetAddress.getLocalHost().getHostName() = KDAAPPDEV04
    java.net.InetAddress.getLocalHost().getCanonicalHostName() = KDAAPPDEV04
    netstat from CMD:
        TCP    [::1]:9092             [::]:0                 LISTENING

我在另一个so答案上找到了此设置,但这些设置对我不起作用。

advertised.listeners=PLAINTEXT://127.0.0.1:9092
listener.security.protocol.map=PLAINTEXT:PLAINTEXT
listeners=PLAINTEXT://0.0.0.0:9092

然后尝试(并重新启动zookeeper和Kafka)

advertised.listeners=PLAINTEXT://KDAAPPDEV04:9092
listener.security.protocol.map=PLAINTEXT:PLAINTEXT
listeners=PLAINTEXT://0.0.0.0:9092

制作人
我用三个不同的值运行这个producer:hostname、localhost和127.0.0.1,但它从未连接到代理

public class ProducerDemo{

    private static Logger logger = LoggerFactory.getLogger(ProducerDemo.class);

    public static void main(String[] args) throws UnknownHostException{

        System.out.println(InetAddress.getLocalHost().getHostName());
        System.out.println(InetAddress.getLocalHost().getCanonicalHostName());

        String bootstrapServers = "127.0.0.1:9092";
//        String bootstrapServers = "localhost:9092";
//        String bootstrapServers = "KDAAPPDEV04: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","hola mundo");

        //send data
        producer.send(record);

        //flush + close
        producer.flush();
        producer.close();
    }
}

错误

[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka version: 2.6.0
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka commitId: 62abe01bee039651
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka startTimeMs: 1601666175706
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Connection to node -1 (KDAAPPDEV04/my-ipconfig-address-here:9092) could not be established. Broker may not be available.
8tntrjer

8tntrjer1#

wsl2在hypervisor中运行,您需要打开防火墙规则才能从windows主机进行连接
kafka和zookeeper在windows中启动良好,所以不清楚您希望wsl2提供什么

13z8s7eq

13z8s7eq2#

这不是最佳的解决方案,但是如果您在ubuntu/wsl中运行producer,您将能够连接。这意味着如果您使用的是windows ide,编写代码,切换到ubuntu,使用命令行编译器并运行producer。在wsl2中通过ide连接到kafka服务器时看到这个post错误

相关问题