【Java】 Kafka学习笔记

Kafka学习笔记

环境搭建

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
version: "2"

services:
zookeeper:
image: bitnami/zookeeper:3.8
ports:
- "2181:2181"
volumes:
- "zookeeper_data:/bitnami"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: bitnami/kafka:3.2
ports:
- "9092:9092"
volumes:
- "kafka_data:/bitnami"
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
depends_on:
- zookeeper

volumes:
zookeeper_data:
driver: local
kafka_data:
driver: local

启动

1
2
$ curl -sSL https://raw.githubusercontent.com/bitnami/containers/main/bitnami/kafka/docker-compose.yml > docker-compose.yml
$ docker-compose up -d

Kafka常用指令

1
2
3
4
5
6
# 查询所有topic
# 较低版本使用 ./kafka-topics.sh --list --zookeeper zookeeper:2181
./kafka-topics.sh --list --bootstrap-server localhost:9092

./kafka-topics.sh --create --bootstrap-server localhost:9092 --topic topic1 --replication-factor 1 --partitions 3