分别搭建参考Docker搭建Kafka
docker pull wurstmeister/kafka
docker pull zookeeper
docker run -d --name zookeeper -p 2181:2181 -t zookeeper:latest
docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_ZOOKEEPER_CONNECT=192.168.8.208:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.8.208:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 wurstmeister/kafka
#进入容器
docker exec -it ${CONTAINER ID} /bin/bash
cd opt/bin
#单机方式:创建一个主题
# opt/kafka/bin/
# 172.17.0.2 通过docker inspect zookeeper获取zookeeper的ip地址
./kafka-topics.sh --create --zookeeper 172.17.0.2:2181 --replication-factor 1 --partitions 1 --topic mykafka
#运行一个生产者
./kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
#运行一个消费者
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mykafka --from-beginning
同时搭建参考docker部署kafka
docker pull wurstmeister/kafka
docker pull zookeeper
version: '2.2'
services:
zookeeper:
image: zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
depends_on: [ zookeeper ]
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 192.168.8.208
KAFKA_CREATE_TOPICS: "test:1:1"
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
# 需要在本地找到目录
- ${HOME}/docker/kafka/docker.sock:/var/run/docker.sock
zzw@zhaozhenwei-Mac-mini kafka % docker-compose build
zookeeper uses an image, skipping
kafka uses an image, skipping
zzw@zhaozhenwei-Mac-mini kafka % docker-compose up -d
Starting kafka_zookeeper_1 ... done
Starting kafka_kafka_1 ... done
docker stop $(docker ps -q)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7fe021070118 wurstmeister/kafka "start-kafka.sh" 17 minutes ago Up 23 seconds 0.0.0.0:9092->9092/tcp kafka_kafka_1
6c1c80b36b95 zookeeper "/docker-entrypoint.…" 17 minutes ago Up About a minute 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp kafka_zookeeper_1
# 新建topic
$KAFKA_HOME/bin/kafka-topics.sh --create --topic mykafka --partitions 4 --zookeeper kafka_zookeeper_1:2181 --replication-factor 1
# 查看topic
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper kafka_zookeeper_1:2181 --describe --topic mykafka
Topic: mykafka PartitionCount: 4 ReplicationFactor: 1 Configs:
Topic: mykafka Partition: 0 Leader: 1001 Replicas: 1001 Isr: 1001
Topic: mykafka Partition: 1 Leader: 1001 Replicas: 1001 Isr: 1001
Topic: mykafka Partition: 2 Leader: 1001 Replicas: 1001 Isr: 1001
Topic: mykafka Partition: 3 Leader: 1001 Replicas: 1001 Isr: 1001
# 创建生产者
$KAFKA_HOME/bin/kafka-console-producer.sh --topic=mykafka --broker-list kafka_kafka_1:9092
# 创建消费者
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server kafka_kafka_1:9092 --from-beginning --topic mykafka