Apache Kafka-个人翻译(入门kafka资料)-Quickstart

来源:互联网 发布:情侣感人瞬间知乎 编辑:程序博客网 时间:2024/06/05 09:17

本教程假定您刚开始使用,并且没有现有的Kafka或ZooKeeper数据。

步骤1:下载代码

下载0.10.0.0 release并解压

<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> tar -xzf kafka_2.11-0.10.0.0.tgz  > cd kafka_2.11-0.10.0.0            </span></span>

步骤2:启动服务器

Kafka使用ZooKeeper,所以你需要首先启动一个ZooKeeper服务器,如果你还没有。 您可以使用与kafka一起提供的便利脚本来获取快速上手的单节点ZooKeeper实例。

<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">> bin/zookeeper-server-start.sh config/zookeeper.properties                            [2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties </span>
<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">(org.apache.zookeeper.server.quorum.QuorumPeerConfig)...                               </span>
然后启动Kafka服务器:
<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">> 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 </span>
<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">(kafka.utils.VerifiableProperties)...                                                     </span>
步骤3:创建一个topic
让我们使用单个分区和一个副本创建一个名为“test”的topic:
<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test</span>
如果我们运行list topic命令,我们现在可以看到这个topic:
<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">> bin/kafka-topics.sh --list --zookeeper localhost:2181 </span>
<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">test                                                    </span>
或者,您也可以将brokers配置为在发布不存在的topic时自动创建topic,而不是手动创建topic。

步骤4:发送一些消息

Kafka提供了一个命令行客户端,它将从文件或标准输入接收输入,并将其作为消息发送到Kafka集群。 默认情况下,每行都将作为单独的消息发送。
运行生产者,然后在控制台中键入一些消息发送到服务器。

<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testThis is a message                                                        This is another message                                                  </span>
步骤5:启动消费者

Kafka还有一个命令行消费者,将消息转储到标准输出。

<span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginningThis is a message                                                                       This is another message                                                                 </span>
如果您在不同的终端运行每个命令,那么您现在应该能够在生产者终端中键入消息,并看到它们出现在消费者终端中。
所有命令行工具都有其他选项; 运行没有参数的命令将更详细地显示记录它们的使用信息。

步骤6:设置多代理集群

到目前为止,我们已经对一个单一的broker运行,但这没有乐趣。 对于Kafka,单个broker只是一个大小为1的集群,因此除了启动几个代理实例之外没有什么变化。 但只是为了做个示范,让我们将集群扩展到三个节点(仍然在我们的本地机器上)。
首先,我们为每个代理制作一个配置文件:

<span style="background-color: rgb(0, 0, 0);"><span style="color: rgb(0, 153, 0);"><span style="font-size:14px;">> cp config/server.properties config/server-1.properties> cp config/server.properties config/server-2.properties</span></span></span>
现在编辑这些新文件并设置以下属性:

<span style="background-color: rgb(0, 0, 0);"><span style="color: rgb(0, 153, 0);"><span style="font-size:14px;">config/server-1.properties:        broker.id=1                    listeners=PLAINTEXT://:9093    log.dir=/tmp/kafka-logs-1  config/server-2.properties:        broker.id=2                    listeners=PLAINTEXT://:9094    log.dir=/tmp/kafka-logs-2  </span></span></span>
broker.id属性是集群中每个节点的唯一且永久的名称。 我们必须覆盖端口和日志目录,因为我们在同一台机器上运行这些,我们想保持所有broker尝试在同一端口注册或覆盖彼此的数据。
我们已经启动了Zookeeper和我们的单节点,所以我们只需要启动两个新的节点:


<span style="color: rgb(0, 153, 0); background-color: rgb(0, 0, 0);"><span style="font-size:14px;">> bin/kafka-server-start.sh config/server-1.properties & ...                                                      > bin/kafka-server-start.sh config/server-2.properties & ...  </span></span><span style="font-size:18px;color:#009900;background-color: rgb(0, 0, 0);">                                         </span>
现在创建一个topic,复制因子为3(replication factor
<span style="background-color: rgb(0, 0, 0);"><span style="color: rgb(0, 153, 0);"><span style="font-size:14px;">> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic</span></span></span>
好了,但现在我们有一个集群,我们怎么知道哪个代理正在做什么? 要看到,就运行“describe topics”命令:

<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic            Topic:my-replicated-topicPartitionCount:1ReplicationFactor:3Configs:           Topic: my-replicated-topicPartition: 0Leader: 1Replicas: 1,2,0Isr: 1,2,0 </span></span>

这里是输出的解释。 第一行给出所有分区的摘要,每个附加行给出关于一个分区的信息。 因为我们对于这个主题只有一个分区,所以只有一行。
“Leader”是负责给定分区的所有读取和写入的节点。 每个节点将是分区的随机选择部分的领导者。
“Replicas”是复制此分区的日志的节点的列表,无论它们是否为引导者,或者即使它们当前处于活动状态。
“Isr”是“in-sync”副本的集合。 这是副本列表的子集,其当前是活动的并赶上领导者。

注意,在我的示例中,节点1是topic的唯一分区的leader。

我们可以对我们创建的原始topic运行相同的命令,以查看它在哪里:

<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test        Topic:testPartitionCount:1ReplicationFactor:1Configs:        Topic: testPartition: 0Leader: 0Replicas: 0Isr: 0  </span></span>

所以没有什么惊喜 ,原来的topic没有副本,并在服务器0,我们创建了在集群中的唯一的服务器。
让我们向我们的新主题发布几条消息:

<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic ...                                                                                      my test message 1                                                                        my test message 2                                                                        ^C                                                                                       </span></span>
现在让我们接受这些消息:
<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic     ...                                                                                                         my test message 1                                                                                           my test message 2                                                                                           ^C                                                                                                          </span></span>
现在让我们测试容错。 Broker1担任领导者,所以让我们结束(kill)它:
<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> ps | grep server-1.properties                                                                   7564 ttys002    0:15.91 /System/Library/Frameworks/JavaVM.framework/Versions/1.8/Home/bin/java... > kill -9 7564                                                                                    </span></span>

领导已切换到其中一个从属节点,节点1不再处于同步副本集中:

<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic                    Topic:my-replicated-topicPartitionCount:1ReplicationFactor:3Configs:                   Topic: my-replicated-topicPartition: 0Leader: 2Replicas: 1,2,0Isr: 2,0           </span></span>

但是消息仍然可用于消费,即使采取写入的领导最初是下来(down):

<span style="background-color: rgb(0, 0, 0);"><span style="color:#009900;">> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic  ...                                                                                                      my test message 1                                                                                        my test message 2                                                                                        ^C                                                                                                       </span></span>



0 0
原创粉丝点击