前言
docker 如何快速搭建单机kafka,顺利做验证test,可以参考
拉取镜像
bash
# zookeeper
docker pull zookeeper:3.6
# kafka
docker pull wurstmeister/kafka:2.12-2.5.0
run起来
bash
# 先run zk
docker run -d --name zookeeper -p 2181:2181 -v /etc/localtime:/etc/localtime zookeeper:3.6
# run kafka
docker run -d --name kafka -p 9002:9002 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.0.1:2181/kafka -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.0.1:9002 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9002 -v /etc/localtime:/etc/localtime wurstmeister/kafka:2.12-2.5.0
再进入kafka测试
bash
# 按照docker name进入容器,也可以用id哈
docker exec -it kafka bash
# kafka验证测试
# 进入 /opt/kafka_2.12-2.5.0/bin/ 目录下
$ cd /opt/kafka_2.12-2.5.0/bin/
# 运行kafka生产者发送消息
$ ./kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
#发送消息
>哈哈哈
>hello ,kafka
>{ "name" : "Jones" }
>{ "Influencer" : { "name" : "Jaxon" , "age" : "42" , "city" , "New York" } }
>{ "Influencers" : [ { "name" : "Jaxon", "age" : 42, "Works At" : "Tech News" } { "name" : "Miller", "age" : 35 "Works At" : "IT Day" } ] }
# 运行kafka消费者接收消息
$ ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic testTopic --from-beginning
搞定手工
bash
# 停止容器
# 停止前先查一下
docker ps | grep zoookeeper
docker ps | grep kafka
# 注意下顺序,如果你下次还要用的话
docker stop kafka
docker stop zoookeeper
# 删除容器
docker rm kafka
docker rm zoookeeper