• Apache Kafka


    在这篇文章中,我将要介绍如何搭建和使用Apache Kafka在windows环境。在开始之前,简要介绍一下Kafka,然后再进行实践。

    Apache Kafka

    Kafka是分布式的发布-订阅消息的解决方案。相比于传统的消息系统,Kafka快速,可扩展,耐用。想象一下传统的发布-订阅消息系统,producers产生/写消息到topic中,另一边,consumers从topic中消费/读消息。Kafka的topic可以在多个服务器之间分区(partition)和复制(replicate)。

    可以得到更多细节信息从Kafka官网

    我参考了这篇博客(http://blog.cloudera.com/blog/2014/09/apache-kafka-for-beginners/)。它简单并很好的解释了Kafka是什么。这两张图片也取自同一篇博客。

    "Messages are simply byte arrays and the developers can use them to store any object in any format – with String, JSON, and Avro the most common. It is possible to attach a key to each message, in which case the producer guarantees that all messages with the same key will arrive to the same partition. When consuming from a topic, it is possible to configure a consumer group with multiple consumers. Each consumer in a consumer group will read messages from a unique subset of partitions in each topic they subscribe to, so each message is delivered to one consumer in the group, and all messages with the same key arrive at the same consumer."

    “信息只不过是简单的字节数组,开发人员可以用它们来存储任何对象用任何格式--String,JSON,Avro是最常用的。可以给每个消息附上一个键,这样producer可以保证拥有相同键的消息到达相同的分区。当从一个topic消费信息时,可以配置一个消费组拥有多个消费者。在消费组里的每个消费者从订阅的topic的partition中读取唯一的一段消息,所以每个消息交付给组里的一个consumer,而且拥有相同键的所有消息到达同一个consumer。”

    "What makes Kafka unique is that Kafka treats each topic partition as a log (an ordered set of messages). Each message in a partition is assigned a unique offset. Kafka does not attempt to track which messages were read by each consumer and only retain unread messages; rather, Kafka retains all messages for a set amount of time, and consumers are responsible to track their location in each log. Consequently, Kafka can support a large number of consumers and retain large amounts of data with very little overhead."

     “使Kafka独特的是Kafka把每个topic分区当做一条日志来处理(一组有序的消息)。在一个分区当中的每一条消息被分配一个唯一的偏移量。Kafka并不试图追踪哪些消息被consumer读取,而是保留未被读取的消息;而且,Kafka保留了所有消息的时间设定量,consumer负责追踪在每一个log中他们的位置。因此,Kafka可以支持众多的消费者,保留大量的数据,只用了非常小的开销。”

    现在你要问了,“怎么在Windows上设置Kafka环境?”。不必着急,我通过简单的几步来引导你。

    下载并修改配置文件 
    1.   从这里下载Kafka,解压到你想要的路径。
    2.   到<kafka_dir>configserver.properties 文件,修改日志文件路径‘log.dirs'根据你自己的环境。
    log.dirs=<kafka_dir>kafka-logs
    3.   到<kafka_dir>configzookeeper.properties 文件,更改数据目录位置 'dataDir' 根据你自己的环境。
    dataDir=<kafka_dir>zookeeper-data
     
    启动Zookeeper和Kafka服务
    Kafka内部使用到了Zookeeper。如果你想了解更多Zookeeper,你可以访问这里
     
    首先,启动Zookeeper服务。运行以下命令:
     
    <kafka_dir>inwindowszookeeper-server-start.bat ....configzookeeper.properties
     
    然后,打开另一个cmd命令窗口,启动Kafka服务:
     
    <kafka_dir>inwindowskafka-server-start.bat ....configserver.properties
     
    创建Topic:
    现在你需要创建Topic来发布和订阅消息。创建Topic你只需运行以下命令。根据以下命令,创建了Topic ’mytopic‘拥有一个partition。
     
    <kafka_dir>inwindowskafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic mytopic
     
    运行完命令后,你会看到如下输出,说明成功创建了Topic。
    Created topic "mytopic".
     
     发布和消费消息
     
    打开一个命令行窗口,运行以下命令。这个命令行被当做producer。
     
    <kafka_dir>inwindowskafka-console-producer.bat --broker-list localhost:9092 --topic mytopic
     
    现在打开另一个命令行窗口运行以下命令。这个命令行被当做consumer。
     
    <kafka_dir>inwindowskafka-console-consumer.bat --zookeeper localhost:2181 --topic mytopic
    如果你输入一些消息在producer,然后按Enter。你会看到consumer消费了这些消息在consumer窗口。
     
    如果你到达了这一步,意味着你成功的在windows环境搭建了Kafka。
  • 相关阅读:
    易语言软件加VMProtect壳的正确方法
    ghost系统到硬盘完后,重启进入winxp安装的画面变成了蓝屏
    万象客户端设置服务端ip保存在注册表的位置
    php乱码解决
    远程桌面Default.rdp 中各个参数的含义
    关闭自动检测磁盘
    关于collapsed margin(外边距合并)
    position定位
    grunt-replace和grunt-include-replace问题
    关于动态生成dom绑定事件失效的原因
  • 原文地址:https://www.cnblogs.com/zdfjf/p/5645093.html
Copyright © 2020-2023  润新知