apache kafka_Apache Kafka简介

apache kafka

什么是Apache Kafka?

Apache Kafka是一个分布式流系统,具有发布和订阅记录流的功能。 在另一方面,它是企业消息传递系统。 它是一个快速,水平可扩展和容错的系统。 Kafka有四个核心API,

生产者API:

该API允许客户端连接到集群中运行的Kafka服务器,并将记录流发布到一个或多个Kafka主题。

使用者API:

该API允许客户端连接到集群中运行的Kafka服务器,并使用来自一个或多个Kafka主题的记录流。 卡夫卡的消费者是直接从卡夫卡主题的消息。

流API:

通过使用来自一个或多个主题的流并将这些流生成到其他输出主题,此API允许客户端充当流处理器。 这允许转换输入和输出流。

连接器API:

该API允许编写可重用的生产者和使用者代码。 例如,如果我们要从任何RDBMS读取数据以将数据发布到主题,并使用主题中的数据并将其写入RDBMS。 使用连接器API,我们可以为各种数据源创建可重用的源连接器和接收器连接器组件。

Kafka用于什么用例?

Kafka用于以下用例,

讯息系统:

Kafka用作企业消息传递系统,用于分离源系统和目标系统以交换数据。 与JMS相比,Kafka具有分区的高吞吐量和具有复制功能的容错能力。

网络活动跟踪:

跟踪网站上的用户旅程事件,以进行分析和脱机数据处理。

日志汇总:

处理来自各种系统的日志。 尤其是在具有微服务架构的分布式环境中,该系统将系统部署在各种主机上。 我们需要汇总来自各种系统的日志,并在中央位置提供日志以进行分析。 浏览有关使用Kafka的分布式日志记录体系结构的文章https://smarttechie.org/2017/07/31/distributed-logging-architecture-for-micro-services/

指标收集器:

Kafka用于从各种系统和网络收集指标以进行操作监视。 Kafka指标报告器可用于监控工具,例如GangliaGraphite等…

关于此https://github.com/stealthly/metrics-kafka的一些参考

什么是经纪人?

Kafka群集中的一个实例称为代理。 在Kafka群集中,如果您连接到任何一个代理,您将可以访问整个群集。 我们连接到访问群集的代理实例也称为引导服务器。 每个代理由集群中的数字ID标识。 首先从Kafka集群开始,三个经纪人是一个不错的选择。 但是有些集群中有数百个经纪人。

什么是主题?

主题是将记录发布到的逻辑名称。 在内部,该主题分为多个发布数据的分区。 这些分区分布在群集中的代理之间。 例如,如果一个主题有三个分区,群集中有3个代理,则每个代理都有一个分区。 要发布的分区数据仅附加偏移量增量。

以下是在使用分区时需要记住的几点。

  • 主题由其名称标识。 一个集群中可以有很多主题。
  • 消息的顺序保持在分区级别,而不是跨主题。
  • 一旦写入分区的数据不被覆盖。 这称为不变性。
  • 分区中的消息与键,值和时间戳一起存储。 Kafka确保将给定密钥的消息发布到同一分区。
  • 在Kafka群集中,每个分区都有一个领导者,该领导者将对该分区进行读/写操作。

在上面的示例中,我创建了一个主题,其中包含三个具有复制因子3的分区。在这种情况下,因为集群具有3个代理,所以这三个分区是均匀分布的,每个分区的副本都复制到另外2个代理中。 由于复制因子为3,因此即使有2个代理发生故障,也不会丢失数据。 始终保持复制因子大于1且小于或等于集群中的代理数目。 创建主题的复制因子不能超过集群中代理的数量。

在上图中,每个分区都有一个领导者(发光分区),其他同步副本(灰色分区)是跟随者。 对于分区0,broker-1是领导者,broker-2,broker-3是跟随者。 对分区0的所有读/写都将转到Broker-1,并且相同的内容将被复制到Broker-2和Broker-3。

现在,让我们按照以下步骤创建具有3个代理的Kafka集群。

第1步:

下载Apache Kafka最新版本。 在此示例中,我使用的是最新的1.0。 解压缩文件夹并移到bin文件夹。 启动Zookeeper,这对于从Kafka集群开始至关重要。 Zookeeper是协调服务,用于管理代理,对分区的领导者进行选举并在主题更改(删除主题,创建主题等)或代理(添加代理,代理模具等)更改期间向Kafka发出警报。 在此示例中,我仅启动了一个Zookeeper实例。 在生产环境中,我们应该有更多的Zookeeper实例来管理故障转移。 如果没有Zookeeper,Kafka群集将无法工作。

./zookeeper-server-start.sh ../config/zookeeper.properties

第2步:

现在启动卡夫卡经纪人。 在此示例中,我们将启动三个经纪人。 转到Kafka根目录下的config文件夹,并将server.properties文件复制3次,并将其命名为server_1.properties,server_2.properties和server_3.properties。 在这些文件中更改以下属性。

#####server_1.properties#####
broker.id=1
listeners=PLAINTEXT://:9091
log.dirs=/tmp/kafka-logs-1

#####server_2.properties######
broker.id=2
listeners=PLAINTEXT://:9092
log.dirs=/tmp/kafka-logs-2

######server_3.properties#####
broker.id=3
listeners=PLAINTEXT://:9093
log.dirs=/tmp/kafka-logs-3M

现在,使用以下命令运行3个代理。

###Start Broker 1 #######
./kafka-server-start.sh ../config/server_1.properties

###Start Broker 2 #######
./kafka-server-start.sh ../config/server_2.properties

###Start Broker 3 #######
./kafka-server-start.sh ../config/server_3.properties

第三步:

使用以下命令创建主题。

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic first_topic

第4步:

通过使用Kafka控制台生产者,为上一步中创建的主题生成一些消息。 对于控制台生产商,请提及任何中间商地址。 这将是引导服务器,用于访问整个群集。

./kafka-console-producer.sh --broker-list localhost:9091 --topic first_topic
>First message
>Second message
>Third message
>Fourth message
>

步骤5:

使用Kafka控制台使用者使用消息。 对于Kafka消费者,请提及任何一个代理地址作为引导服务器。 请记住,在阅读消息时,您可能看不到命令​​。 由于顺序是在分区级别而不是主题级别维护的。

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first_topic --from-beginning

如果需要,可以使用以下命令描述该主题以查看分区的分布方式以及每个分区的领导者。

./kafka-topics.sh --describe --zookeeper localhost:2181 --topic first_topic

#### The Result for the above command#####
Topic:first_topic	PartitionCount:3	ReplicationFactor:3	Configs:
	Topic: first_topic	Partition: 0	Leader: 1	Replicas: 1,2,3	Isr: 1,2,3
	Topic: first_topic	Partition: 1	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1
	Topic: first_topic	Partition: 2	Leader: 3	Replicas: 3,1,2	Isr: 3,1,2

在上面的描述中,broker-1是分区:0的领导者,broker-1,broker-2和broker-3具有每个分区的副本。

在下一篇文章中,我们将看到生产者和消费者JAVA API。 直到那时, 快乐消息!!!

翻译自: https://www.javacodegeeks.com/2017/11/introduction-apache-kafka.html

apache kafka

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Set up Apache Kafka clusters and develop custom message producers and consumers using practical, hands-on examples Overview Write custom producers and consumers with message partition techniques Integrate Kafka with Apache Hadoop and Storm for use cases such as processing streaming data Provide an overview of Kafka tools and other contributions that work with Kafka in areas such as logging, packaging, and so on In Detail Message publishing is a mechanism of connecting heterogeneous applications together with messages that are routed between them, for example by using a message broker like Apache Kafka. Such solutions deal with real-time volumes of information and route it to multiple consumers without letting information producers know who the final consumers are. Apache Kafka is a practical, hands-on guide providing you with a series of step-by-step practical implementations, which will help you take advantage of the real power behind Kafka, and give you a strong grounding for using it in your publisher-subscriber based architectures. Apache Kafka takes you through a number of clear, practical implementations that will help you to take advantage of the power of Apache Kafka, quickly and painlessly. You will learn everything you need to know for setting up Kafka clusters. This book explains how Kafka basic blocks like producers, brokers, and consumers actually work and fit together. You will then explore additional settings and configuration changes to achieve ever more complex goals. Finally you will learn how Kafka works with other tools like Hadoop, Storm, and so on. You will learn everything you need to know to work with Apache Kafka in the right format, as well as how to leverage its power of handling hundreds of megabytes of messages per second from multiple clients. What you will learn from this book Download and build Kafka Set up single as well as multi-node Kafka clusters and send messages Learn Kafka design internals and message compression Understand how replication works in Kafka Write Kafka message producers and consumers using the Kafka producer API Get an overview of consumer configurations Integrate Kafka with Apache Hadoop and Storm Use Kafka administration tools Approach The book will follow a step-by-step tutorial approach which will show the readers how to use Apache Kafka for messaging from scratch. Who this book is written for Apache Kafka is for readers with software development experience, but no prior exposure to Apache Kafka or similar technologies is assumed. This book is also for enterprise application developers and big data enthusiasts who have worked with other publisher-subscriber based systems and now want to explore Apache Kafka as a futuristic scalable solution. Product Details Paperback: 88 pages Publisher: Packt Publishing (October 17, 2013) Language: English ISBN-10: 1782167935 ISBN-13: 978-1782167938 Product Dimensions: 9.2 x 7.5 x 0.2 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值