kafka介绍

介绍

Kafka是一个分布式的、分区的、冗余的日志提交服务。它使用了独特的设计,提供了所有消息传递系统所具有的功能。

我们先来看下几个消息传递系统的术语:

  • Kafka维护消息类别的东西是主题(topic).
  • 我们称发布消息到Kafka主题的进程叫生产者(producer).
  • 我们称订阅主题、获取消息的进程叫消费者(consumer).
  • Kafka是由多个服务器组成的机器,每个服务器称作代理(broker).

在较高的层次上看,生产者通过网络发送消息到Kafka集群,Kafka集群将这些消息提供给消费者,如下图:

53271bf2-1ca3-4577-a8b7-16d2a5b69e79

客户端与服务器之间的通信通过一个简单的、高性能的、语言无关的TCP protocol. Kafka有Java 客户端,但这客户端在很多语言many languages也是有效的.

主题(Topics)、日志(Logs)

一个主题就是消息的类别或名称。对每个主题,Kafka集群都管理着一个被分区的日志,如下:

1e47a1b0-93c1-4248-945d-72c033b232e5

每个分区就是一个提交日志:每个分区上保存着不断被追加的消息,这些消息是有序的且顺序不可改变;分区上的每个消息都被分配了一个序列号offset,offset唯一标识了分区上的消息。

Kafka集群保留所有发布的消息,不管这些消息是否被消费了,直到消息达到了它的存活周期,这个存活周期是可以被配置的。例如,日志被设置保留2天,消息被消费后还将继续存活,直到2天以后它才被删除释放。 数据的大小不会影响Kafka的性能,保留大量数据是没问题的(Kafka's performance is effectively constant with respect to data size so retaining lots of data is not a problem)

事实上,在每个消费者上保留的元数据只有offset,它是消费者在日志上的位置。这个offset受消费者控制:正常情况下,随着消费者读取消息offset是线性增长的,但是消费者可以控制offset,消费者可以以任何顺序消费消息。例如,一个消费者可以重置到老的offset来从新执行。

这些特性表明,Kafka的消费者是非常廉价的,一个消费者的创建、销毁不会对集群或其他消费者产生多大的影响。

对日志进行分区有几个目的:

1、扩容,一个主题可以有多个分区,这使得可以保存比一个机器保存的多的多的数据。

2、并行。

分布式(Distribution)

日志的分区分布在Kafka集群的多台机器上,每个机器管理数据和响应分区请求。每个分区冗余存储来保证容错,冗余数可以配置。

每个分区都有一个机器作为leader,零个或多个机器作为follower。leader处理这个分区的读写请求,follower执行leader的指令。如果leader宕机了,followers中的一个将自动成为新的leader。每个机器都是它的分区的leader和其他分区的follower,follower角色用来负载均衡。

生产者(Producers)

生产者发布数据到它们选择的主题上。生产者负责将某条消息分配到某个主题的某个分区上。这可以通过简单的循环的方式来实现,或者使用一些分区方法(比如根据消息的key来分区)

消费者(Consumers)

传统的消息传递有两种方式: 队列(queuing)、发布-订阅(publish-subscribe). 队列方式,一组消费者从机器上读消息,每个消息只传递给这组消费者中的一个;分布-订阅方式,消息被广播到所有的消费者。 Kafka提供了一个消费组(consumer group)的说法来概括这两种方式。

消费者都有一个消费组,主题的每个消息被传递到订阅这个主题的消费组中的一个消费者实例,消费者实例可以是一个单独的进程或一个单独的机器。

如果所有的消费者在同一个组里,这就相当于队列方式,在消费者上的负载均衡。(If all the consumer instances have the same consumer group, then this works just like a traditional queue balancing load over the consumers.)

如果消费者在不同的组里,这就相当于发布-订阅方式,所有的消息将被广播到所有的消费者。(If all the consumer instances have different consumer groups, then this works like publish-subscribe and all messages are broadcast to all consumers.)

一般的情况是,主题有少量的消费组,每个消费组就是一个逻辑上的订阅者。每个消费组由很多消费者组成,有很好的扩展性和容错。. This is nothing more than publish-subscribe semantics where the subscriber is cluster of consumers instead of a single process.

4d5c6581-284f-4238-bb7c-ef1e470095a8
A two server Kafka cluster hosting four partitions (P0-P3) with two consumer groups. Consumer group A has two consumer instances and group B has four.

Kafka比传统的消息传递系统有更好的顺序行保证。

A traditional queue retains messages in-order on the server, and if multiple consumers consume from the queue then the server hands out messages in the order they are stored. However, although the server hands out messages in order, the messages are delivered asynchronously to consumers, so they may arrive out of order on different consumers. This effectively means the ordering of the messages is lost in the presence of parallel consumption. Messaging systems often work around this by having a notion of "exclusive consumer" that allows only one process to consume from a queue, but of course this means that there is no parallelism in processing.

Kafka does it better. By having a notion of parallelism—the partition—within the topics, Kafka is able to provide both ordering guarantees and load balancing over a pool of consumer processes. Kafka将主题下的分区分配给消费组里的消费者,每个分区被一个消费者消费,This is achieved by assigning the partitions in the topic to the consumers in the consumer group so that each partition is consumed by exactly one consumer in the group. By doing this we ensure that the consumer is the only reader of that partition and consumes the data in order. Since there are many partitions this still balances the load over many consumer instances. 消费者的数量不能超过分区数Note however that there cannot be more consumer instances than partitions.

Kafka只能保证分区内的消息是有序的 only provides a total order over messages within a partition, not between different partitions in a topic. Per-partition ordering combined with the ability to partition data by key is sufficient for most applications.如果你想要消息是全局有序的,你可以设置主题只有一个分区,同时这意味着只能有一个消费者。 However, if you require a total order over messages this can be achieved with a topic that has only one partition, though this will mean only one consumer process.

保证(Guarantees)

At a high-level Kafka gives the following guarantees:

  • 生产者发送的消息安装它们发送的顺序追加到主题
  • 消费者看到消息的顺序就是消息在日志中存储的顺序
  • For a topic with replication factor N, we will tolerate up to N-1 server failures without losing any messages committed to the log.

More details on these guarantees are given in the design section of the documentation.

来源: <http://kafka.apache.org/documentation.html#introduction>

原文地址:https://www.cnblogs.com/lishouguang/p/4560543.html