本地 Kafka 部署教程
工作目录
mkdir onlineinternship
cd onlineinternship
筹备 docker-compose
KAFKA_ADVERTISED_LISTENERS 为客户端调用 kafka 时应用的端口
能够是 localhost(也就是 127.0.0.1),也能够是本机局域网 IP 等
docker-compose.yml
“””
version: ‘2’
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- “2181:2181”
kafka:
image: wurstmeister/kafka
ports:
- “9092:9092”
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_LISTENERS: PLAINTEXT://:9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
“””
执行
“””
docker-compose up -d
docker ps
“””
如果看到两个容器 onlineinternship_kafka_1, onlineinternship_zookeeper_1 阐明胜利了
create topic
“””
docker exec onlineinternship_kafka_1
kafka-topics.sh
–create –topic test
–partitions 1
–zookeeper zookeeper:2181
–replication-factor 1
“””
topic list
“””
docker exec onlineinternship_kafka_1
kafka-topics.sh –list
–zookeeper zookeeper:2181
“””
如果看到 test 阐明胜利了
consume
“””
docker exec -it onlineinternship_kafka_1
kafka-console-consumer.sh
–topic test
–bootstrap-server localhost:9092
“””
关上一个 consumer 期待音讯
produce
“””
docker exec -it onlineinternship_kafka_1
kafka-console-producer.sh
–topic test
–broker-list localhost:9092
“””
试着发送一条音讯
没谬误就是胜利了
看看 consumer 是不是有后果
在 Go 中调用
应用 localhost:9092
“””
kafka.NewProducer(&kafka.ConfigMap{“bootstrap.servers”: “localhost:9092”})
“””