1 创建topic

  • 进到kafkabash

    docker exec -it kafka bash cd /opt/kafka/bin

  • 创建topic名为demo,分区个数为2, 每个分区的副本数为1

    kafka-topics.sh —create —zookeeper 192.168.1.9:2181 \ —partitions 2 —replication-factor 1 —topic demo

kafka-topics.sh —create —zookeeper 192.168.31.75:2181 \ —partitions 2 —replication-factor 1 —topic demo

kafka-topics.sh —create —zookeeper 172.19.223.211:2181 \ —partitions 2 —replication-factor 1 —topic demo

partitions指定分区个数
replication-factor指定创建topic时每个分区的副本数
image.png

  • 查看当前所有topic

    kafka-topics.sh —zookeeper 192.168.1.9:2181 —list

kafka-topics.sh —zookeeper 192.168.31.75:2181 —list

kafka-topics.sh —zookeeper 172.19.223.211:2181 —list

image.png

2 测试kafka生产者和消费者

  • 进入容器

    docker exec -it kafka bash

  • 进到bin目录

    cd /opt/kafka_2.13-2.7.1/bin/

image.png

  • 运行kafka生产者发送消息

    ./kafka-console-producer.sh —broker-list localhost:9092 —topic test

image.png

  • 运行kafka消费者接收消息

    ./kafka-console-consumer.sh —bootstrap-server localhost:9092 —topic test

image.png