从零开始学习SpringCloud
今天记录下Kafka的使用,截取下官网上的一段话 “Apache Kafka® is a distributed streaming platform”,kafka是一个分布式流媒体平台。下图是来自kafka官方的一些介绍和说明。
![](http://211.159.189.135:8081/wp-content/uploads/2020/03/15850308021.png)
流媒体平台有三大主要功能:
- 发布和订阅的记录,类似于一个消息队列或企业邮件系统流。
- 存储流的容错耐用的方式记录。
- 记录过程流,因为它们发生。
Kafka通常用于两大类的应用:
- 建立实时流数据管道不仅能够可靠地获得系统或应用程序之间的数据
- 构建实时流式变换或反应数据流应用
随着物联网的大力发展,接下来会有越来越多的事物需要接入到互联网上,因此对于数据的接入处理和数据的实时处理也是一个比较大的挑战,同时也迸发出了很多的消息队列,现在比较主流和常用的如Kafka、RabbitMq、RocketMq等。但是不同的消息队列应用的场景有一定的差异,有兴趣的话可以去深入的研究下。这里我们这介绍下kafka的搭建和kafka的一些用法。
在zookeeper官网、kafka官网分别下载zk和kafka的安装包我这里已经下载好了对应的tar包,然后解压对应的tar包。
一.首先搭建kafka需要先搭建好zookeeper的服务。
首先下载zookeeper,因为kafka的数据种的offset等需要存储在zookeeper种,并且zookeeper也会用来管理好kafka集群之间的状态和leader之间的选举。
首先需要先来修改zookeeper的配置并且启动zookeeper。 在服务器上切换到zookeeper的根目录下,然后进入conf包,然后拷贝zoo_sample.cfg文件并将文件的名称修改为 zoo.cf,然后打开zoo.cfg文件如图配置。
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataLogDir=/data/zookeeper/log
dataDir=/data/zookeeper/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
只需要配置好zookeeper的数据的存放路径和日志的存放路径,便于我们查找分析,然后配置zookeeper的端口即可,然后我们切到bin目录下执行zkServer.sh start即可看到我们的zookeeper已经起来。
这里我们只是简单的介绍了单实例的安装,对于集群的安装方式,类似只是配置文件稍微修改一下,有兴趣的可以了解下,建议了解下zookeper集群的选举模式(半数选举)等。
二.接下来需要搭建kafka的环境
同样的进入到kafka安装包的解压的路径下,然后进入conf包种然后先去修改server.properties文件:
- broker.id=0
- log.dirs=/data/kafka/log
- zookeeper.connect=127.0.0.1:2181
到此,单机实例下的kafka的配置就已经完成了,接下来我们需要启动kafka:
bin/kafka-server-start.sh config/server.properties
到此,kafka在单机上的实例的搭建过程就全部完成了,接下来我们一起看下kafka的使用方法。
三.kafka的基本操作
接下来分别讲解下kafka如何创建一个topic 、 查看一个topic的信息 、生产一个消息、消费一个消息和删除一个topic等。
- 1.创建一个主题topic
- ./kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic kk-test
- Created topic "kk-test".
- 2.查看主题:
- $ ./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic kk-test
- Topic:kk-test PartitionCount:1 ReplicationFactor:1 Configs:
- Topic: kk-test Partition: 0 Leader: 0 Replicas: 0 Isr: 0
- 3.生产消息:
- ./kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic kk-test
- 4.消费消息:
- kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic kk-test -- from-beginning
- 5.删除topic
- kafka-topics.bat --delete --zookeeper 127.0.0.1:2181 --topic kk-test
- Topic kk-test is marked for deletion.
- Note: This will have no impact if delete.topic.enable is not set to true.
到此,kafka的搭建就全部完成了,但是没有说出为何日志收集选择kafka ,并且kafka的topic、group和partition等关系。等到下一章记录springboot整合Kafka的时候在具体的说明。