提醒
在hub.docker.com网站上,Star最多的kafka镜像是wurstmeister/kafka,今天一起来实践这个镜像,使用此镜像搭建kafka环境,并且生产和消费消息;
https://developer.aliyun.com/article/988818
实战环境
操作系统:CentOS7
Docker:24.0.2
docker-compose:v2.18.1
编写docker-compose.yml
version: "3"
# 通用配置
x-common-config: &common-config
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_NUM_PARTITIONS: 3
KAFKA_DEFAULT_REPLICATION_FACTOR: 2
# kafka镜像通用配置
x-kafka: &kafka
image: wurstmeister/kafka
networks:
net:
depends_on:
- zookeeper
services:
zookeeper:
container_name: zookeeper
image: wurstmeister/zookeeper
ports:
- "2181:2181"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
networks:
- net
volumes:
- zookeeper_data:/wurstmeister/zookeeper
kafka-0:
container_name: kafka-0
<<: *kafka
ports:
- "9093:9093"
environment:
<<: *common-config
KAFKA_BROKER_ID: 0
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.166.169:9093
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093
volumes:
- kafka_0_data:/wurstmeister/kafka
kafka-1:
container_name: kafka-1
<<: *kafka
ports:
- "9094:9094"
environment:
<<: *common-config
KAFKA_BROKER_ID: 1
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.166.169:9094
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9094
volumes:
- kafka_1_data:/wurstmeister/kafka
kafka-2:
container_name: kafka-2
<<: *kafka
ports:
- "9095:9095"
environment:
<<: *common-config
KAFKA_BROKER_ID: 2
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.166.169:9095
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9095
volumes:
- kafka_2_data:/wurstmeister/kafka
volumes:
zookeeper_data:
kafka_0_data:
kafka_1_data:
kafka_2_data:
networks:
net:
启动server
在docker-compose.yml所在的文件夹下,执行命令docker compose up -d
,会先下载zookeeper和kafka的镜像,然后创建容器;
执行命令docker compose ps
,可见启动了一个zookeeper和三个kafka容器:
[root@hecs-334217 kafka-compose]# docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
kafka-0 wurstmeister/kafka "start-kafka.sh" kafka-0 28 minutes ago Up 28 minutes 0.0.0.0:9093->9093/tcp, :::9093->9093/tcp
kafka-1 wurstmeister/kafka "start-kafka.sh" kafka-1 28 minutes ago Up 28 minutes 0.0.0.0:9094->9094/tcp, :::9094->9094/tcp
kafka-2 wurstmeister/kafka "start-kafka.sh" kafka-2 28 minutes ago Up 28 minutes 0.0.0.0:9095->9095/tcp, :::9095->9095/tcp
zookeeper wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" zookeeper 28 minutes ago Up 28 minutes 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, :::2181->2181/tcp
查看版本号
执行以下命令查看容器中的kafka版本号:
[root@hecs-334217 kafka-compose]# docker exec kafka-0 find / -name \*kafka_\* | head -1
| grep -o '\kafka[^\n]*'
kafka_2.13-2.8.1
上述命令执行后,我这边控制台显示kafka_2.11-0.11.0.3,这表示Scala 的版本为2.11,kafka的版本是0.11.0.3;
执行以下命令查看zookeeper版本:
[root@hecs-334217 kafka-compose]# docker exec zookeeper pwd
/opt/zookeeper-3.4.13
创建topic
创建一个topic,名为topic001,3个partition,副本因子2,执行以下命令即可:
[root@hecs-334217 kafka-compose]# docker exec kafka-1 kafka-topics.sh --create --topic topic001 --partitions 3 --zookeeper zookeeper:2181 --replication-factor 2
Created topic topic001.
执行以下命令查看刚刚创建的topic,这次在容器kafka-2上执行命令试试:
[root@hecs-334217 kafka-compose]# docker exec kafka-2 kafka-topics.sh --list --zookeeper zookeeper:2181
test
topic001
可见刚刚创建的topic可以被查到;
查看刚刚创建的topic的情况,borker和副本情况一目了然,如下:
[root@hecs-334217 kafka-compose]# docker exec kafka-0 kafka-topics.sh --describe --topic topic001 --zookeeper zookeeper:2181
Topic: topic001 TopicId: umQtyLTtQQCeMG8W5bWcpA PartitionCount: 3 ReplicationFactor: 2 Configs:
Topic: topic001 Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: topic001 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: topic001 Partition: 2 Leader: 2 Replicas: 2,0 Isr: 2,0
消费消息
执行如下命令,即可进入等待topic为topic001消息的状态:
docker exec kafka-2 \
kafka-console-consumer.sh \
--topic topic001 \
--bootstrap-server kafka-0:9093,kafka-1:9094,kafka-2:9095
目前还没有生产消息,因此控制台不会有内容输出,接下来尝试生产消息;
生产消息
打开一个新的窗口,执行如下命令,进入生产消息的命令行模式,注意不要漏掉参数"-it"
docker exec -it kafka-1 \
kafka-console-producer.sh \
--topic topic001 \
--broker-list kafka-0:9093,kafka-1:9094,kafka-2:9095
现在已经进入了生产消息的命令行模式,输入一些字符串然后回车,再去消费消息的控制台窗口看看,已经有消息打印出来,说明消息的生产和消费都成功了。
文章来源:https://www.toymoban.com/news/detail-849327.html
docker compose 命令
# 启动:
$ docker compose up -d # docker-compose up -d 这种是旧版的,新版本已经抛弃了
# 查看compose容器
$ docker compose ps
# 删除容器:
$ docker compose down
Kafka 可视化工具(Kafka Tool)
https://www.cnblogs.com/miracle-luna/p/11299345.html文章来源地址https://www.toymoban.com/news/detail-849327.html
到了这里,关于kafka的Docker镜像使用说明(wurstmeister/kafka)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!