docker-compose文件
注意替换ip
version: '3'
networks:
docker_network:
external: true
services:
zookeeper:
image: wurstmeister/zookeeper
networks:
- docker_network
container_name: zookeeper
ports:
- 2181:2181
environment:
- TZ=Asia/Shanghai
kafka:
image: wurstmeister/kafka
networks:
- docker_network
container_name: kafka00
ports:
- 9090:9090
environment:
- TZ=Asia/Shanghai
- KAFKA_BROKER_ID=0
- KAFKA_ZOOKEEPER_CONNECT=zookeeper容器ip:2181
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://服务器ip:9090
- KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9090
kafka1:
image: wurstmeister/kafka
networks:
- docker_network
container_name: kafka01
ports:
- 9091:9091
environment:
- TZ=Asia/Shanghai
- KAFKA_BROKER_ID=1
- KAFKA_ZOOKEEPER_CONNECT=zookeeper容器ip:2181
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://服务器ip:9091
- KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9091
kafka2:
image: wurstmeister/kafka
networks:
- docker_network
container_name: kafka02
ports:
- 9092:9092
environment:
- TZ=Asia/Shanghai
- KAFKA_BROKER_ID=2
- KAFKA_ZOOKEEPER_CONNECT=zookeeper容器ip:2181
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://服务器ip:9092
- KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092
启动
zk:docker-compose up -d zookeeper
kafka00:docker-compose up -d kafka
kafka01:docker-compose up -d kafka1
kafka02:docker-compose up -d kafka2
创建一个测试主题
进入kafka00容器 docker exec -it kafka00 bash
创建一个主题
/opt/kafka/bin/kafka-topics.sh --create --zookeeper zk容器ip:2181 --replication-factor 3 --partitions 5 --topic TestTopic
查看主题情况
建议在打开两个ssh连接的窗口 (自行进入)
查看主题
/opt/kafka/bin/kafka-topics.sh --describe --zookeeper zk容器ip:2181 --topic TestTopic
其他的几个容器也应该是一样的。。。省略
发送消息测试
记得放开9090、9091、9092端口
接下来,分别在kafka00上运行一个生产者,kafka01、02上分别运行一个消费者:
生产者:
/opt/kafka/bin/kafka-console-producer.sh --broker-list 服务器ip:当前kafka端口(9090) --topic TestTopic
消费者 (分别在kafka01,kafka02)
kafka01:
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 服务器ip:当前kafka端口(9091) --topic TestTopic --from-beginning
kafka02:
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 服务器ip:当前kafka端口(9092) --topic TestTopic --from-beginning
然后在生产者发消息:
看两个消费者是否都能接收
至此,结束