盒子
盒子
文章目录
  1. 1. 编写docker-compose.yml
  2. 2. 启动kafka
  3. 3. 消息生产消费验证

Docker快速安装kafka

同样基于docker-compose安装,Docker快速部署nginx中有讲到,不在重述

1. 编写docker-compose.yml

个人习惯放在/opt/下,如/opt/kafka, /opt/nginx, docker-compose.yml如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181" # 端口不可省略,否则,docker会内部会随机分配端口,造成zk connected refused
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 192.168.153.152 # 宿主IP地址,此为我虚拟机地址
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock

2. 启动kafka

docker-compose up -d

Creating kafka_kafka_1 … done
Creating kafka_zookeeper_1 … done

3. 消息生产消费验证

1
2
3
4
5
6
7
8
9
10
11
12
docker exec -it kafka_kafka_1 /bin/bash # 进入kafka终端

# 创建test主题,副本1,分区1
kafka-topics.sh --create --topic test --zookeeper kafka_zookeeper_1:2181 --replication-factor 1 --partitions 1

kafka-topics.sh --list --zookeeper kafka_zookeeper_1:2181 # 查看刚才创建的test分区

# 发布消息,输入几条消息后,按^C退出发布
kafka-console-producer.sh --topic=test --broker-list kafka_kafka_1:9092

# 接收消息
kafka-console-consumer.sh --bootstrap-server kafka_kafka_1:9092 --from-beginning --topic test

如果正常的话,发布的消息能够接收到,开始愉悦的kafka之旅吧~~~

支持一下
扫一扫,支持沈健
  • 微信扫一扫
  • 支付宝扫一扫