1.拉取并安装zookeeper
docker run -d --name zookeeper -p 2181:2181 -v /etc/localtime:/etc/localtime wurstmeister/zookeeper
2.拉取并安装kafka
KAFKA_ZOOKEEPER_CONNECT=192.168.155.56:2181 这里的ip为第一步安装的zookeeper的ip,端口也是zookeeper端口,默认2181
KAFKA_ADVERTISED_LISTENERS =PLAINTEXT 是指kafka注册到zookeeper的地址,写本机IP。
KAFKA_LISTENERS 是指kafka监听的端口,写本机IP。如果不行就写localhost。
docker run -d --name kafka --net=host -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=localhost:2181/kafka -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 -e KAFKA_LISTENERS=PLAINTEXT://localhost:9092 -v /etc/localtime:/etc/localtime wurstmeister/kafka
查看docker容器进程,kafka和zookeeper已经启动起来了。
3.进入容器测试数据;
生产数据
docker exec -it kafka /bin/sh
cd opt/kafka_2.13-2.7.0/bin/
./kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
{"datas":[{"channel":"","metric":"temperature","producer":"ijinus","sn":"IJA0101-00002245","time":"1543207156000","value":"80"}],"ver":"1.0"}
消费数据
docker exec -it kafka /bin/sh
cd opt/kafka_2.13-2.7.0/bin/
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning
有问题欢迎留言讨论,不定时回复!
支持开源,拥抱开源,为开发者赋能!