欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kafka命令行生产者消费者测试

程序员文章站 2022-07-12 17:41:37
...

【README】

基于命令行开启生产者,消费者线程,测试kafka的消费转发功能; 

【1】生产者与消费者

生产者201

[[email protected] logs]# kafka-console-producer.sh --topic first --broker-list centos201:9092
>hello-world
>sichuan-changdu
>

消费者202

[[email protected] logs]# kafka-console-consumer.sh --topic first --zookeeper centos201:2181
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
hello-world
sichuan-changdu

注意: 消费者202在生产者201 发送消息前就已经启动了; 所以能够收到所有生产者数据; 
此后203再启动消费者线程,如下:

[[email protected] logs]#  kafka-console-consumer.sh --topic first --zookeeper centos201:2181
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].

执行结果说明:显然201机器开启生产者线程,写入了数据到 kafka, hello-world等; 202机器开启了消费者线程,可以收到201机器写入的数据; 但201写完之后, 203 再开启了消费者线程,203收不到之前201写入的数据
但当我们在开启消费者线程时,加上参数 --from-beginning 时可以消费或收到201之前写的数据;只不过消息无序了

[[email protected] logs]#  kafka-console-consumer.sh --topic first --zookeeper centos201:2181 --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
sichuan-changdu
hello-world
sichuan-deyang

补充:因为 提示命令过期,所以使用 bootstrap-server 来启动消费者 

[email protected] logs]# kafka-console-consumer.sh --topic first --bootstrap-server centos201:9092
sichuan-nanchong

-- 从头开始消费
[[email protected] logs]# kafka-console-consumer.sh --topic first --bootstrap-server centos201:9092 --from-beginning
sichuan-deyang
sichuan-nanchong
sichuan-changdu
hello-world
sichuan-mianyang

 

相关标签: kafka