kafka学习笔记

kafka学习笔记

Posted by kzdgt on Saturday, September 3, 2022

安装

使用docker-compose部署kafka和zookeeper

version: '3'
services:
  zoo1:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
    hostname: zoo1
    container_name: zookeeper
  kafka:
    image: wurstmeister/kafka
    depends_on:
      - zoo1
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 114.116.21.20
      KAFKA_CREATE_TOPICS: "sun:1:1"
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181
    container_name: kafka
                      

运行

docker-compose build  #(编译,打包)
docker-compose up -d  #(启动服务)

进入kafka

#1.进入kafka容器:
docker exec -it d4bd903e4530 bash
#2.创建topic
--dockerkafka_zookeeper_1 ( zook容器名)
$KAFKA_HOME/bin/kafka-topics.sh --create --topic moni --partitions 4 --zookeeper dockerkafka_zookeeper_1:2181 --replication-factor 1 
#3.查看主题信息
-- dockerkafka_zookeeper_1  (zook容器名)
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper dockerkafka_zookeeper_1:2181 --describe --topic moni
#4.测试发送消息
-- dockerkafka_kafka_1(kafka容器名)
$KAFKA_HOME/bin/kafka-console-producer.sh --topic=moni --broker-list dockerkafka_kafka_1:9092
#5.测试接收消息
-- dockerkafka_kafka_1(kafka容器名)
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server dockerkafka_kafka_1:9092 --from-beginning --topic moni
#6.从 latest 位移位置开始消费该主题的所有分区消息,即仅消费正在写入的消息。
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --topic topicName
#7.从指定主题中有效的起始位移位置开始消费所有分区的消息。
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --from-beginning --topic topicName

「真诚赞赏,手留余香」

kzdgt Blog

真诚赞赏,手留余香

使用微信扫描二维码完成支付