需要
- 搭建一个 3 节点 kafka 集群,测试性能和性能
- 实现 Spring kafka 下对 kafka 集群的操作
一、搭建精简版 kafka 集群
参考排名第一的 kafka 镜像 wurstmeister/kafka 的官网文档,进行如下操作:
- 创立 docker-compose.yml 文件,内容如下:
version: '1'
services:
zookeeper:
image: zookeeper
container_name: bitkylin-kafka-zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092"
environment:
kafka_ADVERTISED_HOST_NAME: 172.17.0.1
kafka_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
关键点:未指定 kafka 容器名、未裸露 kafka 相干端口,便于 docker-compose 命令自伸缩扩大容器。
- 集群操作
在 docker-compose.yml 文件所在目录下,可执行如下操作:
# 启动单 kafka 节点的集群
docker-compose up -d
# 启动三个 kafka 节点的集群
docker-compose up --scale kafka=3
# 销毁集群
docker-compose stop
二、完整版 kafka 集群
精简版集群创立比较简单,然而并不实用,以下创立可用版的集群。
wurstmeister/kafka
镜像搞了半天,搞出的集群无奈被容器内部拜访,这为调试、开发带来不便,现更换镜像重搞一次。
更换为镜像:bitnami-docker-kafka
- 创立单节点 kafka 集群:
version: '1'
services:
zookeeper:
image: zookeeper
container_name: bitkylin-kafka-zookeeper
ports:
- "2181:2181"
kafka:
image: 'bitnami/kafka'
ports:
- '9092:9092'
environment:
- kafka_BROKER_ID=1
- kafka_LISTENERS=PLAINTEXT://:9092
- kafka_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
- kafka_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
depends_on:
- zookeeper
- 创立 3 节点 kafka 集群:
version: '2'
services:
zookeeper:
image: zookeeper
container_name: bitkylin-kafka-zookeeper
ports:
- "2181:2181"
kafka-1:
image: 'bitnami/kafka'
container_name: bitkylin-kafka-1
ports:
- '9081:9081'
environment:
- kafka_BROKER_ID=11
- kafka_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- kafka_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT
- kafka_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9081
- kafka_CFG_ADVERTISED_LISTENERS=CLIENT://:9092,EXTERNAL://localhost:9081
- kafka_INTER_BROKER_LISTENER_NAME=CLIENT
depends_on:
- zookeeper
kafka-2:
image: 'bitnami/kafka'
container_name: bitkylin-kafka-2
ports:
- '9082:9082'
environment:
- kafka_BROKER_ID=12
- kafka_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- kafka_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT
- kafka_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9082
- kafka_CFG_ADVERTISED_LISTENERS=CLIENT://:9092,EXTERNAL://localhost:9082
- kafka_INTER_BROKER_LISTENER_NAME=CLIENT
depends_on:
- zookeeper
kafka-3:
image: 'bitnami/kafka'
container_name: bitkylin-kafka-3
ports:
- '9083:9083'
environment:
- kafka_BROKER_ID=13
- kafka_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- kafka_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT
- kafka_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9083
- kafka_CFG_ADVERTISED_LISTENERS=CLIENT://:9092,EXTERNAL://localhost:9083
- kafka_INTER_BROKER_LISTENER_NAME=CLIENT
depends_on:
- zookeeper
- 启动三节点 kafka 集群
docker-compose up -d
- 操作
# --- 容器集群内 - 基于节点 1 发送音讯 ---
# 进入其中一个 kafka 容器
docker exec -it bitkylin-kafka-1 /bin/bash
# 从节点 1 发送音讯
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-bitkylin
# --- 容器集群外 - 基于节点 3 接管音讯 ---
# 进入 kafka 装置目录
cd /opt/kafka_2.13-2.7.0/bin
# 从节点 3 监听指定 topic
./kafka-console-consumer.bat --bootstrap-server localhost:9083 --topic test-bitkylin --from-beginning
通过以上操作,能够验证发送音讯、接管音讯均失常。尤其验证了容器外拜访 kafka 容器是失常的,这是难点!!!
通过 Spring boot + Spring kafka 对 kafka 集群音讯发送、接管的示例,见如下代码:
bitkylin-kafka
启动 Spring boot 利用后,依然应用 bitkylin-kafka-1 容器中的音讯生产者发送音讯,能够看到 Spring boot 利用和连贯 bitkylin-kafka-3 的内部消费者均全量生产了音讯。
备忘
- kafka 需配置残缺的代理
IP:PORT
,容器集群外要拜访 kafka 容器时,必须通过裸露的IP:PORT
进行拜访,不能进行任何变更。此处设计据说是为了避免中间人攻打。 - 容器外拜访 kafka 容器的确很麻烦,钻研了很久,bitnami-docker-kafka 镜像的官网文档写的很好,能够研读。
参考链接
- kafka 的 Docker 镜像应用阐明 (wurstmeister-kafka)
- docker image:wurstmeister/kafka
- docker image:bitnami-docker-kafka
- 示例代码:bitkylin-kafka