1.拉取镜像
docker pull zookeeper
docker pull wurstmeister/kafka
2.启动zookeeper
docker run -d --name zookeeper -p 2181:2181 zookeeper
3.设置zookeeper容器对外服务的ip
Zookeeper_Server_IP=$(docker inspect zookeeper --format='{{ .NetworkSettings.IPAddress }}')
4.启动kafka
docker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=${Zookeeper_Server_IP}:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -e TZ="Asia/Shanghai" wurstmeister/kafka
测试生产者消费者功能
-
终端进入kafka
docker exec -it kafka bash
-
cd /opt/kafka_version/bin/
-
创建topic,运行生产者命令
./kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic first-topic
-
发送消息
-
启动终端进入kafka,进入相同目录,运行消费者命令
./kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic first-topic --from-beginning
看到消息成功接收。