由于kafka依赖zookeeper,所以使用到了docker-compose,由于我使用的是Docker Desktop,默认安装了docker-compose,否则需要自己安装,可以用下面命令检查是否安装了docker-compose
docker-compose -v
这里就不介绍安装,直接上docker-compose.yml代码
version: '2'
services:
zookeeper:
image: "zookeeper"
hostname: "zookeeper.local"
container_name: "zookeeper"
#设置网络别名
networks:
local:
aliases:
- "zookeeper.local"
kafka:
image: "wurstmeister/kafka"
hostname: "kafka.local"
container_name: "kafka"
ports:
- "9092:9092"
networks:
local:
aliases:
- "kafka.local"
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka.local
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
#设置网络,名为local
networks:
local:
driver: bridge
执行启动命令
docker-compose up -d
然后docker ps查看启动的容器
进入kafka容器
docker exec -it kafka /bin/bash
cd 到kafka的bin目录下,创建topic,发送消息,接收消息
cd /opt/kafka_2.12-2.4.0/bin/
#创建topic
kafka-console-producer.sh --broker-list localhost:9092 --topic test
#生产者生产消息
kafka-console-producer.sh --broker-list localhost:9092 --topic test
#消费topic
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test--from-beginning
来源:CSDN
作者:sayok_why
链接:https://blog.csdn.net/sayoko06/article/details/104020621