简单记录一下本地搭建 Kafka 用到的命令。
利用 Docker 在本地搭建单机版 Kafka。
拉取镜像
1
2
| docker pull wurstmeister/zookeeper
docker pull wurstmeister/kafka
|
Zookeeper
1
2
3
| mkdir -p ~/.zookeeper/data
mkdir -p ~/.zookeeper/datalog
docker run -d --privileged=true --restart=always --name=zookeeper -p 2181:2181 -v /etc/localtime:/etc/localtime -v ~/.zookeeper/data:/data -v ~/.zookeeper/datalog:/datalog -t wurstmeister/zookeeper
|
Kafka
1
2
| mkdir -p ~/.kafka/data
docker run -d --privileged=true --restart=always --name=kafka -p 9092:9092 -v ~/.kafka/data:/kafka -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 -e KAFKA_AUTO_CREATE_TOPICS_ENABLE=true -e KAFKA_LOG_RETENTION_HOURS=72 -t wurstmeister/kafka
|
关于 KAFKA_ADVERTISED_LISTENERS
- 如果有固定的公网 IP,把 localhost 改成公网 IP 可以开启公网访问
- 如果有固定的局域网 IP,把 localhost 改成局域网 IP,可以保证局域网内互通
完成上述配置后,重启一下 Kafka
docker-compose
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
32
33
34
35
| version: "3"
services:
zookeeper:
image: "wurstmeister/zookeeper"
container_name: zookeeper
privileged: true
restart: always
ports:
- "2181:2181"
volumes:
- ~/.zookeeper/data:/data
- ~/.zookeeper/datalog:/datalog
tty: true
kafka:
image: "wurstmeister/kafka"
container_name: kafka
privileged: true
restart: always
ports:
- "9092:9092"
volumes:
- ~/.kafka/data:/kafka
tty: true
environment:
KAFKA_BROKER_ID: 0
KAFKA_AUTO_CREATE_TOPICS_ENABLE: true
KAFKA_LOG_RETENTION_HOURS: 72
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
# 如果有固定的公网 IP,把 localhost 改成公网 IP 可以开启公网访问
# 如果有固定的局域网 IP,把 localhost 改成局域网 IP,可以保证局域网内互通
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
depends_on:
- zookeeper
|