Kafka安装与启动
工作中需要用Kafka,就通过单机服务的形式学习下怎么使用,下面是安装的过程。
安装Java
跑Apache的东西,自然需要Java啦,直接上OpenJDK
1 | sudo apt install openjdk-11-jdk |
Zookeeper
Zookeeper是什么?
Zookeeper是一个分布式的协调服务框架,主要用来解决分布式集群中,应用系统需要面对的各种通用的一致性问题。Zookeeper本身可以部署为一个集群,集群的各个节点之间可以通过选举产生一个Leader,选举遵循半数以上的原则,所以一般集群需要部署奇数个节点。
Zookeeper最核心的功能是它提供了一个分布式的存储系统,数据的组织方式类似于Unix文件系统的树形结构,是一个可以保证一致性的存储系统。分布式系统中一些需要整个集群都可以访问的元数据,比如集群节点信息、公共配置信息等,都适合保存在Zookeeper中。
安装Zookeeper
发行版启动方式
Kafka的发行版中自带了Zookeeper,可以直接从脚本启动,不过这里我们单独下载Zookeeper进行安装。
下载解压Kafka后可直接使用发行版的shell脚本启动Zookeeper:
1 | bin/zookeeper-server-start.sh config/zookeeper.properties |
单独下载启动方式
访问官方站点,上面会建议我们从哪个镜像站进行下载: http://zookeeper.apache.org/releases.html
接下来进行基本的配置
1 | tar -xvf apache-zookeeper-3.5.5-bin.tar.gz |
将zoo.cfg
中的dataDir
为刚创建好的数据目录:
1 | dataDir=/home/top/KZ/apache-zookeeper-3.5.5-bin/data |
启动Zookeeper Server:
1 | bin/zkServer.sh start |
接下来可以通过telnet命令连接到Zookeeper端口,发送srvr
命令验证是否安装正确
1 | telnet localhost 2181 |
Kafka
什么是Kafka?
Apache Kafka是一款开源的消息引擎系统。主要功能是提供一套完备的消息发布与订阅解决方案。
安装Kafka
访问官方站点进行下载: http://kafka.apache.org/downloads
1 | tar -xvf kafka_2.12-2.3.0.tgz |
启动Kafka Server
1 | bin/kafka-server-start.sh config/server.properties |
如果想让其以守护进程模式运行,加上-daemon
参数即可:
1 | bin/kafka-server-start.sh -daemon config/server.properties |
现在我们创建一个Topic来验证Kafka是否能够正常工作
1 | bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test |
列出Topic
1 | bin/kafka-topics.sh --list --bootstrap-server localhost:9092 |
启动一个生产者并发送消息:
1 | bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test |
启动一个消费者接收信息:
1 | bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning |
查看Topic信息:
1 | bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test |
describe信息中部分字段的解释:
“leader” is the node responsible for all reads and writes for the given partition. Each node will be the leader for a randomly selected portion of the partitions.
“replicas” is the list of nodes that replicate the log for this partition regardless of whether they are the leader or even if they are currently alive.
“isr” is the set of “in-sync” replicas. This is the subset of the replicas list that is currently alive and caught-up to the leader.
删除Topic
1 | bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic test |
到这Kafka的单机安装和启动就算完成了吧,具体的术语、配置、multi-broker cluster等还要继续学习。
参考
1、http://kafka.apache.org/quickstart
2、http://zookeeper.apache.org/doc/r3.5.5/zookeeperStarted.html
3、Kafka权威指南
4、Kafka核心技术与实战