零、为了方便开发调试,使用kafka部署一套kafka环境,进行功能调试,方便快捷
一、部署zookeeper
1、下载镜像
docker pull wurstmeister/zookeeper
2、运行zookeeper镜像
docker run -d --restart=always --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 --name zookeeper -p 2181:2181 -v /etc/localtime:/etc/localtime wurstmeister/zookeeper
备注:/etc/localtime:/etc/localtime 容器时间同步虚拟机的时间
启动后可以通过docker -ps 查看容器状态
二、搭建kafka
1、拉取镜像
docker pull wurstmeister/kafka
2、部署三个broker
2.1命名为:kafka0 端口:9092 brokerId: 0
docker run -d --name kafka0 -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.131.121:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.131.121:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -v /etc/localtime:/etc/localtime -t wurstmeister/kafka
2.2命名为:kafka1 端口:9093 brokerId: 1
docker run -d --name kafka1 -p 9093:9093 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=192.168.131.121:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.131.121:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -v /etc/localtime:/etc/localtime -t wurstmeister/kafka
2.3命名为:kafka2 端口:9094 brokerId: 2
docker run -d --name kafka2 -p 9094:9094 -e KAFKA_BROKER_ID=2 -e KAFKA_ZOOKEEPER_CONNECT=192.168.131.121:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.131.121:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -v /etc/localtime:/etc/localtime -t wurstmeister/kafka
参数说明:
-e KAFKA_BROKER_ID=0 在kafka集群中,每个kafka都有一个BROKER_ID来区分自己
-e KAFKA_ZOOKEEPER_CONNECT=192.168.131.121:2181/kafka 配置zookeeper管理kafka的路径192.168.131.121:2181/kafka
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.131.121:9092 把kafka的地址端口注册给zookeeper,如果是远程访问要改成外网IP,类如Java程序访问出现无法连接。
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 配置kafka的监听端口
-v /etc/localtime:/etc/localtime 容器时间同步虚拟机的时间
三、验证kafka可用
1、进入容器
docker exec -it kafka0 bash 和 docker exec -it kafka1 bash
2、进入 /opt/kafka_2.13-2.8.1/bin/ 目录下
cd /opt/kafka_2.13-2.8.1/bin/
3、运行kafka生产者发送消息
./kafka-console-producer.sh --broker-list 192.168.131.121:9092,192.168.131.121:9093,192.168.131.121:9094 --topic sun
发送消息
> {"datas":[{"channel":"","metric":"temperature","producer":"ijinus","sn":"IJA0101-00002245","time":"1543207156000","value":"80"}],"ver":"1.0"}
>111
>222
4、运行kafka消费者接收消息
./kafka-console-consumer.sh --bootstrap-server 192.168.131.121:9092,192.168.131.121:9093,192.168.131.121:9094 --topic sun --from-beginning
{"datas":[{"channel":"","metric":"temperature","producer":"ijinus","sn":"IJA0101-00002245","time":"1543207156000","value":"80"}],"ver":"1.0"}
111
222
四、docker部署kafka管理平台
1、下载镜像
docker pull hlebalbau/kafka-manager
2、创建文件夹 \kafka\kafka-manager\docker-compose.yml
version: '2'
services:
kafka-manager:
image: sheepkiller/kafka-manager
environment:
ZK_HOSTS: 192.168.131.121:2181
APPLICATION_SECRET: "letmein"
ports:
- "39000:9000"
expose:
- "9000"
3、启动docker命令
docker-compose up -d
4、启动界面如下
5、注意,第一次进入页面不会自动同步cluster,需要手动输入zookeeper地址
进入cluster就可以查看到topic和broker了