1、下载镜像
这里使用了wurstmeister/kafka和wurstmeister/zookeeper这两个版本的镜像
- docker pull wurstmeister/zookeeper
- docker pull wurstmeister/kafka
2、启动
1、启动zookeeper
- docker run -d --name zookeeper -p 2181:2181 -t wurstmeister/zookeeper
- docker run -d --name kafka -e HOST_IP=localhost -e KAFKA_ADVERTISED_PORT=9092 -e KAFKA_BROKER_ID=1 -e ZK=zk -p 9092:9092 --link zookeeper:zk -t wurstmeister/kafka
3、测试发送消息
执行Docker ps,找到kafka的Container ID,进入容器内部:
- docker exec -it ${CONTAINER ID} /bin/bash
进入kafka默认目录
- cd /opt/kafka_2.12-0.10.2.0/
下面就是跟一般的kafka没什么区别了
创建一个主题:
- bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic mykafka
运行一个消息生产者,指定topic为刚刚创建的主题
运行一个消费者,指定同样的主题
bash-4.3# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
larry testing information here!!!
hahaha kafka MQ testing here!!!
produce message here
- bin/kafka-console-consumer.sh --zookeeper zookeeper:2181 --topic mykafka --from-beginning
这时在生产者输入测试消息,在消费者就可以接收消息了
bash-4.3# bin/kafka-console-consumer.sh --zookeeper zookeeper:2181 --topic mykafka --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
larry testing information here!!!
hahaha kafka MQ testing here!!!
produce message here