1.用docker-compose搭建kafka集群
docker-compose.yaml
version: '3'
services:
zoo1:
image: zookeeper:3.4.14
restart: unless-stopped
hostname: zoo1
ports:
- "2181:2181"
container_name: zookeeper
kafka1:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: xx.xx.xx.xx ## 修改:宿主机IP
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9092 ## 修改:宿主机IP
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
KAFKA_ADVERTISED_PORT: 9092
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zoo1
container_name: kafka1
kafka2:
image: wurstmeister/kafka
ports:
- "9093:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: xx.xx.xx.xx ## 修改:宿主机IP
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9093 ## 修改:宿主机IP
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
KAFKA_ADVERTISED_PORT: 9093
KAFKA_BROKER_ID: 2
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zoo1
container_name: kafka2
kafka-manager:
image: sheepkiller/kafka-manager ## 镜像:开源的web管理kafka集群的界面
environment:
ZK_HOSTS: xx.xx.xx.xx ## 修改:宿主机IP
ports:
- "9000:9000" ## 暴露端口
执行命令
docker-compose up -d
2.进行测试
我们进入到kafka1容器内部中:
docker exec -it 容器ID /bin/sh
然后我们创建一个topic
kafka-topics.sh --create --zookeeper zoo1:2181 --replication-factor 1 --partitions 2 --topic testKafka
创建成功,然后我们给这个topic发送消息:
kafka-console-producer.sh --broker-list kafka1:9092,kafka2:9093 --topic testKafka
然后我们在进入kafka2容器中进行消费:
kafka-console-consumer.sh --bootstrap-server kafka1:9092,kafka2:9093 --topic testKafka --from-beginning
消息已经可以正常接收了
3.遗留问题
1.每次开启生产者或者消费者时,会报以下错误:
但是消息还是可以正常生产与消费,没有搞明白错误在哪里