kafka笔记

docker运行Kafka

version: '3'
services:
  zookeeper:
    container_name: zookeeper
    image: wurstmeister/zookeeper
    restart: always
    ports:
      - "2181:2181"
  kafka:
    container_name: kafka
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    restart: always
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
      #KAFKA_CREATE_TOPICS: "test" #默认创建一个topic
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LOG_RETENTION_MINUTES: 10
    volumes:
      - /Users/ly/data/kafka/docker.sock:/var/run/docker.sock

kafka操作命令

进入容器

docker exec -it kafka bash

创建topic

kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic test

查看topic

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

删除topic

kafka-topics.sh --delete --zookeeper zookeeper:2181 --topic test

启动生产者

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

消息带有key的生产者

kafka-console-producer.sh  --broker-list localhost:9092 --topic test   --property parse.key=true    --property key.separator=,

消费者,输出消息

kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic test --from-beginning

消费者,输出key和消息

kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic test  --from-beginning --property print.key=true  --property key.separator=,

推荐阅读更多精彩内容