kafka 创建&消费topic

创建topic

1
bash-4.4# ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic  topicname

2181是zookeeper端口

创建成功提示,kafka建议不要使用.或者_作topic名,但是不影响使用。

1
2

Created topic monitor_ping_topic

查看topic

查看topic使用–list 即可列出有哪些topic

1
2
3
4
5
6
bash-4.4# ./bin/kafka-topics.sh --list --zookeeper zookeeper:2181
__consumer_offsets
monitor_ping_topic
monitor_solve_topic
monitor_times_topic
bash-4.4#

生产消息

回车输入消息内容

1
2
3
bash-4.4# ./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic monitor_ping_topic
>{"Host":"192.168.1.1","Type":1,"Status":"success","PingCount":3,"AvgRtt":10756000,"PacketLoss":0,"CreateTime":"2020/07/08 13:58:16"}
>

消费消息

from-beginning 表示从头开始消费消息

1
2
3
4
5

bash-4.4# ./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic monitor_ping_topic --from-beginning

{"Host":"192.168.1.1","Type":1,"Status":"success","PingCount":3,"AvgRtt":119031000,"PacketLoss":0,"CreateTime":"2020/07/08 13:53:41"}
{"Host":"127.0.0.2","Type":1,"Status":"failed","PingCount":3,"AvgRtt":0,"PacketLoss":100,"CreateTime":"2020/07/08 13:53:41"}