前言:要求安装docker-compose
kafka镜像版本:apache/kafka:3.8.0
可能存在镜像拉不下来的情况:
1、vim /etc/docker/daemon.json
{
"data-root":"/data/docker",
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://iju9kaj2.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://cr.console.aliyun.com",
"https://hub.docker.com",
"http://mirrors.ustc.edu.cn"
]
}
2、编辑好后执行如下两条命令使修改的文件生效
systemctl daemon-reload
systemctl restart docker
1、编辑docker-compose.yml
修改KAFKA_ADVERTISED_LISTENERS中的ip地址
version: '3'
services:
kafka:
image: apache/kafka:3.8.0
container_name: kafka
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xxx本机ipxxx:9092
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_NUM_PARTITIONS: 3
2、启动
#启动
docker-compose up -d
#停止
docker-compose down
docker-compose stop
3、验证
这边使用的是xshell,打开两个窗口A和B:
A窗口
- 进入kafka容器内
docker exec --workdir /opt/kafka/bin/ -it kafka sh
- 创建topic
./kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test-topic
- 进入生产者控制台,输入要发送的消息
这边写入 ni hao
./kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
B窗口
- 进入kafka容器内
docker exec --workdir /opt/kafka/bin/ -it kafka sh
- 打开消费者控制台:自动接收生产者的消息
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning