version"3.5"
services
zookeeper
container_name zookeeper
image wurstmeister/zookeeper
networks
kafka-network
expose
2181
restart always
kafka1
depends_on
zookeeper
container_name kafka-1
image wurstmeister/kafka
networks
kafka-network
expose
9092
ports
"19092:9092"
environment
KAFKA_BROKER_ID0
KAFKA_NUM_PARTITIONS3
KAFKA_DEFAULT_REPLICATION_FACTOR2
KAFKA_ZOOKEEPER_CONNECT zookeeper2181
KAFKA_LISTENERS PLAINTEXT //0.0.0.09092
KAFKA_ADVERTISED_LISTENERS PLAINTEXT //192.168.56.119092
restart always
kafka2
depends_on
zookeeper
container_name kafka-2
image wurstmeister/kafka
networks
kafka-network
expose
9092
ports
"19093:9092"
environment
KAFKA_BROKER_ID1
KAFKA_NUM_PARTITIONS3
KAFKA_DEFAULT_REPLICATION_FACTOR2
KAFKA_ZOOKEEPER_CONNECT zookeeper2181
KAFKA_LISTENERS PLAINTEXT //0.0.0.09092
KAFKA_ADVERTISED_LISTENERS PLAINTEXT //192.168.56.119093
restart always
kafka3
depends_on
zookeeper
container_name kafka-3
image wurstmeister/kafka
networks
kafka-network
expose
9092
ports
"19094:9092"
environment
KAFKA_BROKER_ID2
KAFKA_NUM_PARTITIONS3
KAFKA_DEFAULT_REPLICATION_FACTOR2
KAFKA_ZOOKEEPER_CONNECT zookeeper2181
KAFKA_LISTENERS PLAINTEXT //0.0.0.09092
KAFKA_ADVERTISED_LISTENERS PLAINTEXT //192.168.56.119094
restart always
networks
kafka-network
driver bridge
name kafka-network
注意:
- 将
KAFKA_ADVERTISED_LISTENERS
中的 IP 地址更改成宿主机的 IP 地址
x
docker compose up
创建 topic
xxxxxxxxxx
docker exec -it kafka-1 kafka-topics.sh --create --bootstrap-server 127.0.0.1:9092 --topic test-topic
启动消费者
xxxxxxxxxx
docker exec -it kafka-2 kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic test-topic --group test-group
在另外一个窗口启动生产者
xxxxxxxxxx
docker exec -it kafka-3 kafka-console-producer.sh --bootstrap-server 127.0.0.1:9092 --topic test-topic
然后观察前一个窗口的输出