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
2
3
4
5
tar -xvf apache-zookeeper-3.5.5-bin.tar.gz
cd apache-zookeeper-3.5.5-bin/
# 创建数据目录
mkdir data
cp conf/zoo_sample.cfg conf/zoo.cfg

zoo.cfg中的dataDir为刚创建好的数据目录:

1
dataDir=/home/top/KZ/apache-zookeeper-3.5.5-bin/data

启动Zookeeper Server:

1
2
3
4
5
$ bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/top/KZ/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

接下来可以通过telnet命令连接到Zookeeper端口,发送srvr命令验证是否安装正确

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ telnet localhost 2181
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
srvr
Zookeeper version: 3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 05/03/2019 12:07 GMT
Latency min/avg/max: 0/2/33
Received: 22
Sent: 21
Connections: 1
Outstanding: 0
Zxid: 0xa
Mode: standalone
Node count: 5
Connection closed by foreign host.

Kafka

什么是Kafka?

Apache Kafka是一款开源的消息引擎系统。主要功能是提供一套完备的消息发布与订阅解决方案。

安装Kafka

访问官方站点进行下载: http://kafka.apache.org/downloads

1
2
tar -xvf kafka_2.12-2.3.0.tgz
cd kafka_2.12-2.3.0/

启动Kafka Server

1
2
3
4
5
6
$ bin/kafka-server-start.sh config/server.properties
[2019-10-13 17:15:35,884] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2019-10-13 17:15:36,232] INFO Registered signal handlers for TERM, INT, HUP (org.apache.kafka.common.utils.LoggingSignalHandler)
[2019-10-13 17:15:36,233] INFO starting (kafka.server.KafkaServer)
[2019-10-13 17:15:36,234] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
...

如果想让其以守护进程模式运行,加上-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
2
$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092
test

启动一个生产者并发送消息:

1
2
3
4
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>This is a message
>This is another message
>

启动一个消费者接收信息:

1
2
3
4
$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
This is a message
This is another message
^CProcessed a total of 2 messages

查看Topic信息:

1
2
3
$ bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test
Topic:test PartitionCount:1 ReplicationFactor:1 Configs:segment.bytes=1073741824
Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0

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
2
3
$ bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic test
$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092
__consumer_offsets

到这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核心技术与实战

0%