第1步:下载代码
下载 2.0.0版本并解压缩它。
1 2 | > tar -xzf kafka_2.11-2.0.0.tgz > cd kafka_2.11-2.0.0 |
第2步:启动服务器
Kafka使用ZooKeeper,因此如果您还没有ZooKeeper服务器,则需要先启动它。您可以使用与kafka一起打包的便捷脚本来获取快速且脏的单节点ZooKeeper实例。
1 2 3 | > bin /zookeeper-server-start .sh config /zookeeper .properties [2013-04-22 15:01:37,495] INFO Reading configuration from: config /zookeeper .properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig) ... |
现在启动Kafka服务器:
1 2 3 4 | > bin /kafka-server-start .sh config /server .properties [2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties) [2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties) ... |
第3步:创建主题
让我们创建一个名为“test”的主题,它只包含一个分区,只有一个副本:
1 | > bin /kafka-topics .sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test |
如果我们运行list topic命令,我们现在可以看到该主题:
1 2 | > bin /kafka-topics .sh --list --zookeeper localhost:2181 test |
或者,您也可以将代理配置为在发布不存在的主题时自动创建主题,而不是手动创建主题。
第4步:发送一些消息
Kafka附带一个命令行客户端,它将从文件或标准输入中获取输入,并将其作为消息发送到Kafka集群。默认情况下,每行将作为单独的消息发送。
运行生产者,然后在控制台中键入一些消息以发送到服务器。
1 2 3 | > bin /kafka-console-producer .sh --broker-list localhost:9092 --topic test This is a message This is another message |
第5步:启动消费者
Kafka还有一个命令行使用者,它会将消息转储到标准输出。
1 2 3 | > bin /kafka-console-consumer .sh --bootstrap-server localhost:9092 --topic test --from-beginning This is a message This is another message |
如果您在不同的终端中运行上述每个命令,那么您现在应该能够在生产者终端中键入消息并看到它们出现在消费者终端中。
所有命令行工具都有其他选项; 运行不带参数的命令将显示更详细地记录它们的使用信息。
步骤6:设置多代理群集
到目前为止,我们一直在与一个经纪人竞争,但这并不好玩。对于Kafka来说,单个代理只是一个大小为1的集群,因此除了启动更多代理实例之外没有太多变化。但是为了感受它,让我们将我们的集群扩展到三个节点(仍然在我们的本地机器上)。
首先,我们为每个代理程序创建一个配置文件(在Windows上使用copy
命令代替):
1 2 | > cp config /server .properties config /server-1 .properties > cp config /server .properties config /server-2 .properties |
现在编辑这些新文件并设置以下属性:
1 2 3 4 五 6 7 8 9 | config/server-1.properties: broker.id=1 listeners=PLAINTEXT://:9093 log.dirs=/tmp/kafka-logs-1 config/server-2.properties: broker.id=2 listeners=PLAINTEXT://:9094 log.dirs=/tmp/kafka-logs-2 |
该broker.id
属性是群集中每个节点的唯一且永久的名称。我们必须覆盖端口和日志目录,因为我们在同一台机器上运行这些,并且我们希望让所有代理尝试在同一端口上注册或覆盖彼此的数据。
我们已经启动了Zookeeper并启动了我们的单个节点,因此我们只需要启动两个新节点:
1 2 3 4 | > bin /kafka-server-start .sh config /server-1 .properties & ... > bin /kafka-server-start .sh config /server-2 .properties & ... |
现在创建一个复制因子为3的新主题:
1 | > bin /kafka-topics .sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic |
好的,但现在我们有一个集群,我们怎么知道哪个经纪人正在做什么?要查看运行“describe topics”命令:
1 2 3 | > bin /kafka-topics .sh --describe --zookeeper localhost:2181 --topic my-replicated-topic Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs: Topic: my-replicated-topic Partition: 0 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0 |
这是输出的解释。第一行给出了所有分区的摘要,每个附加行提供有关一个分区的信息。由于我们只有一个分区用于此主题,因此只有一行。
- “leader”是负责给定分区的所有读取和写入的节点。每个节点将成为随机选择的分区部分的领导者。
- “replicas”是复制此分区日志的节点列表,无论它们是否为领导者,或者即使它们当前处于活动状态。
- “isr”是“同步”复制品的集合。这是副本列表的子集,该列表当前处于活跃状态并且已经被领导者捕获。
请注意,在我的示例中,节点1是该主题的唯一分区的领导者。
我们可以在我们创建的原始主题上运行相同的命令,以查看它的位置:
1 2 3 | > bin /kafka-topics .sh --describe --zookeeper localhost:2181 --topic test Topic: test PartitionCount:1 ReplicationFactor:1 Configs: Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0 |
所以毫无疑问 - 原始主题没有副本,位于服务器0上,是我们创建它时群集中唯一的服务器。
让我们向我们的新主题发布一些消息:
1 2 3 4 五 | > bin /kafka-console-producer .sh --broker-list localhost:9092 --topic my-replicated-topic ... my test message 1 my test message 2 ^C |
现在让我们使用这些消息:
1 2 3 4 五 | > bin /kafka-console-consumer .sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic ... my test message 1 my test message 2 ^C |
现在让我们测试一下容错性。经纪人1充当领导者所以让我们杀了它:
1 2 3 | > ps aux | grep server-1.properties 7564 ttys002 0:15.91 /System/Library/Frameworks/JavaVM .framework /Versions/1 .8 /Home/bin/java ... > kill -9 7564 |
在Windows上使用:
1 2 3 4 | > wmic process where "caption = 'java.exe' and commandline like '%server-1.properties%'" get processid ProcessId 6016 > taskkill /pid 6016 /f |
领导已切换到其中一个从属节点,节点1不再位于同步副本集中:
1 2 3 | > bin /kafka-topics .sh --describe --zookeeper localhost:2181 --topic my-replicated-topic Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs: Topic: my-replicated-topic Partition: 0 Leader: 2 Replicas: 1,2,0 Isr: 2,0 |
但即使最初接受写入的领导者已经失败,这些消息仍可供消费:
1 2 3 4 五 | > bin /kafka-console-consumer .sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic ... my test message 1 my test message 2 ^C |