0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Install Kafka in WSL and access from Windows

0
Posted at
  1. install Kafka in WSL
    quickstart
  2. Access Kafka with Java from Windows
    dependence:org.apache.kafka-clients:3.1.2
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>("local.chat", "hallo world!!!!");

    //send data
    producer.send(record);

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

}

よくある問題

Error1

2022-12-13 17:41:10.884 WARN 23284 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Con
nection to node -1 (/172.21.223.255:9092) could not be established. Broker may not be available.
2022-12-13 17:41:10.884 WARN 23284 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Boo
tstrap broker 172.21.223.255:9092 (id: -1 rack: null) disconnected

解決方法:
(1)

WSL2 currently has a networking issue that prevents outside programs to connect to Kafka running on WSL2 (for example your Java programs, Conduktor, etc...);
Disable IPv6 on WSL2
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1

(2) cd {Kafka_Home}
vim config/server.properites
下記のものを追加する

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

(3) 再起動

Error2

bin/kafka-server-start.sh config/kraft/server.propertiesを実行する時

[2022-12-13 22:45:29,548] ERROR Exiting Kafka due to fatal exception (kafka.Kafka$)
org.apache.kafka.common.KafkaException: No meta.properties found in /tmp/kraft-combined-logs (have you run kafka-storage.sh to format the directory?)

解決方法

rand=`./bin/kafka-storage.sh random-uuid`
echo $rand
./bin/kafka-storage.sh format -t $rand  -c ./config/kraft/server.properties

再起動
bin/kafka-server-start.sh config/kraft/server.properties

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?