kafka环境依赖zookeeper,所以得先安装zookeeper
1
docker pull wurstmeister/zookeeper
2
docker pull wurstmeister/kafka
3
docker run -d --name zookeeper --publish 2181:2181 --volume /etc/localtime:/etc/localtime zookeeper:latest
4
docker run -d --name kafka --publish 9092:9092 --link zookeeper --env KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
--env KAFKA_ADVERTISED_HOST_NAME=kafka所在宿主机的IP --env KAFKA_ADVERTISED_PORT=9092 --volume /etc/localtime:/etc/localtime
wurstmeister/kafka:latest
5 执行Docker ps,找到kafka的Container ID,进入容器内部
docker exec -it ${CONTAINER ID} /bin/bash
6 创建一个topic:(kafka的安装路径: opt/kafka)
bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic mykafka
7 运行一个消息生产者,指定topic为刚刚创建的主题
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
8 运行一个消费者,指定同样的主题
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mykafka --from-beginning
=================kafka常用指令===================
查看所有的topic
./kafka-topics.sh --zookeeper 39.105.144.248 --list
查看指定的topic详情
./kafka-topics.sh --zookeeper 39.105.144.248 --topic mytopic --describe
网友评论