文章目录

  • 1.1概述
  • 1.2消息系统介绍
    • 1.2.1点对点消息传递
    • 1.2.2发布-订阅消息传递
  • 1.3 Kafka的优点
  • 1.4 Kafka架构以及术语解释
    • 1.4.1 Broker
    • 1.4.2 Topic
    • 1.4.3 Partition
    • 1.4.4 Producer
    • 1.4.5 Consumer
    • 1.4.6 Consumer Group
    • 1.4.7 Leader
    • 1.4.8 Follower
    • 1.4.9 Offset
    • 1.4.10 Segment
  • 1.5 Kafka主要配置参数
    • 1.5.1 Broker Config
    • 1.5.2 Producer Config
    • 1.5.3 Consumer Config

1.1概述

Kafka最初是由Linkedin公司开发的,是一个分布式的分区的多副本的多订阅者基于zookeeper协调的分布式日志系统,当然也可以当作MQ系统,常见的可以用作日志收集,消息服务等。Linkedin于2010年贡献给了Apache基金并成为顶级开源项目。

主要应用场景是日志收集系统消息系统
Kafka主要设计目标:

  1. 以时间复杂度为O(1)的方式提供持久化能力,即时对TB级别以上数据也能保证常数时间的访问性能
  2. 高吞吐率。即时在非常廉价的商用机器上也能做到单机支持每秒100K条消息的传输。
  3. 支持Kafka Server间的消息分区,及分布式消费,同时保证每个partition内的消息顺序传输。
  4. 同时支持离线数据处理和实时数据处理
  5. 支持在线水平扩展

1.2消息系统介绍

一个消息系统负责将数据从一个应用系统传递到另外一个应用,应用只需关注于数据,无需关注数据在两个或多个应用间是如何传递的。分布式消息传输基于可靠的消息队列,在客户端应用和消息系统之间异步传递消息。
主要由两种消息传递模式

  1. 点对点传递模式
  2. 发布-订阅模式

大部分的消息系统选用发布-订阅模式。Kafka就是一种发布订阅模式

1.2.1点对点消息传递

点对点消息系统中,消息持久化到一个队列中。此时,将有一个或多个消费者消费队列中的数据。但是一条消息只能被消费一次。当一个消费者消费了队列中的某条数据之后,该数据则从消息队列中删除。该模式即使有多个消费者同时消费数据,也能保证数据的处理顺序,示意图如下:

生产这发送一条消息到Queue,只有一个消费者能收到

1.2.2发布-订阅消息传递

在发布-订阅消息系统中,消息被持久化到一个topic中。于点对点消息系统不同的四,消费者可以订阅一个或多个topic,消费者可以消费该topic中的所有的数据,同一条数据可以被多个消费者消费,数据被消费后不会立马删除。在发布-订阅消息系统中。消息的生产者被称为发布者,消费者被称为订阅者。模式示意图如下;

发布者发送到topic的消息,只有订阅了topic的订阅者才会收到消息

1.3 Kafka的优点

  • 高吞吐量、低延迟:kafka每秒可以处理几十万条雄安锡,它的延迟最低只有几毫秒
  • 可扩展性:kafka集群支持热扩展
  • 持久性、可靠性:消息被持久化到本地磁盘,并且支持数据备份防止数据丢失
  • 容错性:允许集群中节点失败(若副本数量为n,则允许n-个节点失败)
  • 高并发:支持数千个客户端同时读写
    .

1.4 Kafka架构以及术语解释


上图中

1.4.1 Broker

Kafka 集群包含一个或多个服务器,服务器节点称为broker。

broker存储topic的数据。如果某topic有N个partition,集群有N个broker,那么每个broker存储该topic的一个partition。

如果某topic有N个partition,集群有(N+M)个broker,那么其中有N个broker存储该topic的一个partition,剩下的M个broker不存储该topic的partition数据。

如果某topic有N个partition,集群中broker数目少于N个,那么一个broker存储该topic的一个或多个partition。在实际生产环境中,尽量避免这种情况的发生,这种情况容易导致Kafka集群数据不均衡。

1.4.2 Topic

每条发布到Kafka集群的消息都有一个类别,这个类别被称为Topic。(物理上不同Topic的消息分开存储,逻辑上一个Topic的消息虽然保存于一个或多个broker上但用户只需指定消息的Topic即可生产或消费数据而不必关心数据存于何处)类似于数据库的表名

1.4.3 Partition

topic中的数据分割为一个或多个partition。每个topic至少有一个partition。每个partition中的数据使用多个segment文件存储。partition中的数据是有序的,不同partition间的数据丢失了数据的顺序。如果topic有多个partition,消费数据时就不能保证数据的顺序。在需要严格保证消息的消费顺序的场景下,需要将partition数目设为1。

1.4.4 Producer

生产者即数据的发布者,该角色将消息发布到Kafka的topic中。broker接收到生产者发送的消息后,broker将该消息追加到当前用于追加数据的segment文件中。生产者发送的消息,存储到一个partition中,生产者也可以指定数据存储的partition。

1.4.5 Consumer

消费者可以从broker中读取数据。消费者可以消费多个topic中的数据。

1.4.6 Consumer Group

每个Consumer属于一个特定的Consumer Group(可为每个Consumer指定group name,若不指定group name则属于默认的group)。这是kafka用来实现一个topic消息的广播(发给所有的consumer)和单播(发给任意一个consumer)的手段。一个topic可以有多个CG。topic的消息会复制-给consumer。如果需要实现广播,只要每个consumer有一个独立的CG就可以了。要实现单播只要所有的consumer在同一个CG。用CG还可以将consumer进行自由的分组而不需要多次发送消息到不同的topic。

1.4.7 Leader

每个partition有多个副本,其中有且仅有一个作为Leader,Leader是当前负责数据的读写的partition

1.4.8 Follower

Follower跟随Leader,所有写请求都通过Leader路由,数据变更会广播给所有Follower,Follower与Leader保持数据同步。如果Leader失效,则从Follower中选举出一个新的Leader。当Follower与Leader挂掉、卡住或者同步太慢,leader会把这个follower从“in sync replicas”(ISR)列表中删除,重新创建一个Follower

1.4.9 Offset

kafka的存储文件都是按照offset.kafka来命名,用offset做名字的好处是方便查找。例如你想找位于2049的位置,只要找到2048.kafka的文件即可。当然the first offset就是00000000000.kafka

1.4.10 Segment

每个partion(目录)相当于一个巨型文件被平均分配到多个大小相等segment(段)数据文件中。但每个段segment file消息数量不一定相等,这种特性方便old segment file快速被删除

1.5 Kafka主要配置参数

1.5.1 Broker Config

属性 默认值 描述
broker.id 0 必填参数,broker的唯一标识
log.dirs /tmp/kafka-logs Kafka数据存放的目录。可以指定多个目录,中间用逗号分隔,当新partition被创建的时会被存放到当前存放partition最少的目录。
port 9092 BrokerServer接受客户端连接的端口号
zookeeper.connect null Zookeeper的连接串,格式为:hostname1:port1,hostname2:port2,hostname3:port3。可以填一个或多个,为了提高可靠性,建议都填上。注意,此配置允许我们指定一个zookeeper路径来存放此kafka集群的所有数据,为了与其他应用集群区分开,建议在此配置中指定本集群存放目录,格式为:hostname1:port1,hostname2:port2,hostname3:port3/chroot/path 。需要注意的是,消费者的参数要和此参数一致。
message.max.bytes 1000000 服务器可以接收到的最大的消息大小。注意此参数要和consumer的maximum.message.size大小一致,否则会因为生产者生产的消息太大导致消费者无法消费。
num.io.threads 8 服务器用来执行读写请求的IO线程数,此参数的数量至少要等于服务器上磁盘的数量。
queued.max.requests 500 I/O线程可以处理请求的队列大小,若实际请求数超过此大小,网络线程将停止接收新的请求。
socket.send.buffer.bytes 100 * 1024 The SO_SNDBUFF buffer the server prefers for socket connections.
socket.receive.buffer.bytes 100 * 1024 The SO_RCVBUFF buffer the server prefers for socket connections.
socket.request.max.bytes 100 * 1024 * 1024 服务器允许请求的最大值, 用来防止内存溢出,其值应该小于 Java heap size.
num.partitions 1 默认partition数量,如果topic在创建时没有指定partition数量,默认使用此值,建议改为5
log.segment.bytes 1024 * 1024 * 1024 Segment文件的大小,超过此值将会自动新建一个segment,此值可以被topic级别的参数覆盖。
log.roll.{ms,hours} 24 * 7 hours 新建segment文件的时间,此值可以被topic级别的参数覆盖。
log.retention.{ms,minutes,hours} 7 days Kafka segment log的保存周期,保存周期超过此时间日志就会被删除。此参数可以被topic级别参数覆盖。数据量大时,建议减小此值。
log.retention.bytes -1 每个partition的最大容量,若数据量超过此值,partition数据将会被删除。注意这个参数控制的是每个partition而不是topic。此参数可以被log级别参数覆盖。
log.retention.check.interval.ms 5 minutes 删除策略的检查周期
auto.create.topics.enable true 自动创建topic参数,建议此值设置为false,严格控制topic管理,防止生产者错写topic。
default.replication.factor 1 默认副本数量,建议改为2。
replica.lag.time.max.ms 10000 在此窗口时间内没有收到follower的fetch请求,leader会将其从ISR(in-sync replicas)中移除。
replica.lag.max.messages 4000 如果replica节点落后leader节点此值大小的消息数量,leader节点就会将其从ISR中移除。
replica.socket.timeout.ms 30 * 1000 replica向leader发送请求的超时时间。
replica.socket.receive.buffer.bytes 64 * 1024 The socket receive buffer for network requests to the leader for replicating data.
replica.fetch.max.bytes 1024 * 1024 The number of byes of messages to attempt to fetch for each partition in the fetch requests the replicas send to the leader.
replica.fetch.wait.max.ms 500 The maximum amount of time to wait time for data to arrive on the leader in the fetch requests sent by the replicas to the leader.
num.replica.fetchers 1 Number of threads used to replicate messages from leaders. Increasing this value can increase the degree of I/O parallelism in the follower broker.
fetch.purgatory.purge.interval.requests 1000 The purge interval (in number of requests) of the fetch request purgatory.
zookeeper.session.timeout.ms 6000 ZooKeeper session 超时时间。如果在此时间内server没有向zookeeper发送心跳,zookeeper就会认为此节点已挂掉。 此值太低导致节点容易被标记死亡;若太高,.会导致太迟发现节点死亡。
zookeeper.connection.timeout.ms 6000 客户端连接zookeeper的超时时间。
zookeeper.sync.time.ms 2000 ZK follower落后 ZK leader的时间。
controlled.shutdown.enable true 允许broker shutdown。如果启用,broker在关闭自己之前会把它上面的所有leaders转移到其它brokers上,建议启用,增加集群稳定性。
auto.leader.rebalance.enable true If this is enabled the controller will automatically try to balance leadership for partitions among the brokers by periodically returning leadership to the “preferred” replica for each partition if it is available.
leader.imbalance.per.broker.percentage 10 The percentage of leader imbalance allowed per broker. The controller will rebalance leadership if this ratio goes above the configured value per broker.
leader.imbalance.check.interval.seconds 300 The frequency with which to check for leader imbalance.
offset.metadata.max.bytes 4096 The maximum amount of metadata to allow clients to save with their offsets.
connections.max.idle.ms 600000 Idle connections timeout: the server socket processor threads close the connections that idle more than this.
num.recovery.threads.per.data.dir 1 The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
unclean.leader.election.enable true Indicates whether to enable replicas not in the ISR set to be elected as leader as a last resort, even though doing so may result in data loss.
delete.topic.enable false 启用deletetopic参数,建议设置为true。
offsets.topic.num.partitions 50 The number of partitions for the offset commit topic. Since changing this after deployment is currently unsupported, we recommend using a higher setting for production (e.g., 100-200).
offsets.topic.retention.minutes 1440 Offsets that are older than this age will be marked for deletion. The actual purge will occur when the log cleaner compacts the offsets topic.
offsets.retention.check.interval.ms 600000 The frequency at which the offset manager checks for stale offsets.
offsets.topic.replication.factor 3 The replication factor for the offset commit topic. A higher setting (e.g., three or four) is recommended in order to ensure higher availability. If the offsets topic is created when fewer brokers than the replication factor then the offsets topic will be created with fewer replicas.
offsets.topic.segment.bytes 104857600 Segment size for the offsets topic. Since it uses a compacted topic, this should be kept relatively low in order to facilitate faster log compaction and loads.
offsets.load.buffer.size 5242880 An offset load occurs when a broker becomes the offset manager for a set of consumer groups (i.e., when it becomes a leader for an offsets topic partition). This setting corresponds to the batch size (in bytes) to use when reading from the offsets segments when loading offsets into the offset manager’s cache.
offsets.commit.required.acks -1 The number of acknowledgements that are required before the offset commit can be accepted. This is similar to the producer’s acknowledgement setting. In general, the default should not be overridden.
offsets.commit.timeout.ms 5000 The offset commit will be delayed until this timeout or the required number of replicas have received the offset commit. This is similar to the producer request timeout.

1.5.2 Producer Config

属性 默认值 描述
metadata.broker.list null 启动时producer查询brokers的列表,可以是集群中所有brokers的一个子集。注意,这个参数只是用来获取topic的元信息用,producer会从元信息中挑选合适的broker并与之建立socket连接。格式是:host2:port1,host2:port2。
request.required.acks 0 参见3.2节介绍
request.timeout.ms 10000 Broker等待ack的超时时间,若等待时间超过此值,会返回客户端错误信息。
producer.type sync 同步异步模式。async表示异步,sync表示同步。如果设置成异步模式,可以允许生产者以batch的形式push数据,这样会极大的提高broker性能,推荐设置为异步。
serializer.class kafka.serializer.DefaultEncoder 序列号类,.默认序列化成 byte[] 。
key.serializer.class kafka.serializer.DefaultEncoder Key的序列化类,默认同上。
partitioner.class kafka.producer.DefaultPartitioner Partition类,默认对key进行hash。
compression.codec none 指定producer消息的压缩格式,可选参数为: “none”, “gzip” and “snappy”。关于压缩参见4.1节
compressed.topics null 启用压缩的topic名称。若上面参数选择了一个压缩格式,那么压缩仅对本参数指定的topic有效,若本参数为空,则对所有topic有效。
message.send.max.retries 3 Producer发送失败时重试次数。若网络出现问题,可能会导致不断重试。
retry.backoff.ms 100 Before each retry, the producer refreshes the metadata of relevant topics to see if a new leader has been elected. Since leader election takes a bit of time, this property specifies the amount of time that the producer waits before refreshing the metadata.
topic.metadata.refresh.interval.ms 600 * 1000 The producer generally refreshes the topic metadata from brokers when there is a failure (partition missing, leader not available…). It will also poll regularly (default: every 10min so 600000ms). If you set this to a negative value, metadata will only get refreshed on failure. If you set this to zero, the metadata will get refreshed after each message sent (not recommended). Important note: the refresh happen only AFTER the message is sent, so if the producer never sends a message the metadata is never refreshed
queue.buffering.max.ms 5000 启用异步模式时,producer缓存消息的时间。比如我们设置成1000时,它会缓存1秒的数据再一次发送出去,这样可以极大的增加broker吞吐量,但也会造成时效性的降低。
queue.buffering.max.messages 10000 采用异步模式时producer buffer 队列里最大缓存的消息数量,如果超过这个数值,producer就会阻塞或者丢掉消息。
queue.enqueue.timeout.ms -1 当达到上面参数值时producer阻塞等待的时间。如果值设置为0,buffer队列满时producer不会阻塞,消息直接被丢掉。若值设置为-1,producer会被阻塞,不会丢消息。
batch.num.messages 200 采用异步模式时,一个batch缓存的消息数量。达到这个数量值时producer才会发送消息。
send.buffer.bytes 100 * 1024 Socket write buffer size
client.id “” The client id is a user-specified string sent in each request to help trace calls. It should logically identify the application making the request.

1.5.3 Consumer Config

属性 默认值 描述
group.id null Consumer的组ID,相同goup.id的consumer属于同一个组。
zookeeper.connect null Consumer的zookeeper连接串,要和broker的配置一致。
consumer.id null 如果不设置会自动生成。
socket.timeout.ms 30 * 1000 网络请求的socket超时时间。实际超时时间由max.fetch.wait + socket.timeout.ms 确定。
socket.receive.buffer.bytes 64 * 1024 The socket receive buffer for network requests.
fetch.message.max.bytes 1024 * 1024 查询topic-partition时允许的最大消息大小。consumer会为每个partition缓存此大小的消息到内存,因此,这个参数可以控制consumer的内存使用量。这个值应该至少比server允许的最大消息大小大,以免producer发送的消息大于consumer允许的消息。
num.consumer.fetchers 1 The number fetcher threads used to fetch data.
auto.commit.enable true 如果此值设置为true,consumer会周期性的把当前消费的offset值保存到zookeeper。当consumer失败重启之后将会使用此值作为新开始消费的值。
auto.commit.interval.ms 60 * 1000 Consumer提交offset值到zookeeper的周期。
queued.max.message.chunks 2 用来被consumer消费的message chunks 数量, 每个chunk可以缓存fetch.message.max.bytes大小的数据量。
rebalance.max.retries 4 When a new consumer joins a consumer group the set of consumers attempt to “rebalance” the load to assign partitions to each consumer. If the set of consumers changes while this assignment is taking place the rebalance will fail and retry. This setting controls the maximum number of attempts before giving up.
fetch.min.bytes 1 The minimum amount of data the server should return for a fetch request. If insufficient data is available the request will wait for that much data to accumulate before answering the request.
fetch.wait.max.ms 100 The maximum amount of time the server will block before answering the fetch request if there isn’t sufficient data to immediately satisfy fetch.min.bytes.
rebalance.backoff.ms 2000 Backoff time between retries during rebalance.
refresh.leader.backoff.ms 200 Backoff time to wait before trying to determine the leader of a partition that has just lost its leader.
auto.offset.reset largest What to do when there is no initial offset in ZooKeeper or if an offset is out of range ;smallest : automatically reset the offset to the smallest offset; largest : automatically reset the offset to the largest offset;anything else: throw exception to the consumer
consumer.timeout.ms -1 若在指定时间内没有消息消费,consumer将会抛出异常。
exclude.internal.topics true Whether messages from internal topics (such as offsets) should be exposed to the consumer.
zookeeper.session.timeout.ms 6000 ZooKeeper session timeout. If the consumer fails to heartbeat to ZooKeeper for this period of time it is considered dead and a rebalance will occur.
zookeeper.connection.timeout.ms 6000 The max time that the client waits while establishing a connection to zookeeper.
zookeeper.sync.time.ms 2000 How far a ZK follower can be behind a ZK leader

参考文章:
https://blog.csdn.net/suifeng3051/article/details/48053965
https://www.cnblogs.com/frankdeng/p/9310684.html
https://cwiki.apache.org/confluence/display/KAFKA/Index(官网)
https://blog.csdn.net/wqc19920906/article/details/82193316/(消息中间件对比)

Kafka笔记:kafka原理简介以及架构相关推荐

  1. Kafka笔记-kafka外网搭建及构建生产者

    程序运行截图如下: 后端如下: 消费者如下: 这里启动kafka先要运行zookpeer ./zookeeper-server-start.sh ../config/zookeeper.propert ...

  2. Kafka笔记-Kafka集群搭建

    如下有3台虚拟机,对应的IP如下: 下一台 下一台 对应的3给IP地址如下: 192.168.94.129 192.168.94.131 192.168.94.132 这里默认jdk 1.8是安装好了 ...

  3. kafka笔记——kafka启动

    1. 准备 阿里云Linux服务器一个(Centos7) 腾讯云Linux服务器一个(CentOs7) 要死....脚本之家

  4. Kafka 原理简介

    Kafka 原理简介 Kafka 是一种高吞吐的分布式发布订阅的消息系统,可以处理消费者规模的网站中的动作流数据,具有高性能的,持久化,多副本,横向扩展能力. https://www.cnblogs. ...

  5. kafka原理_P8架构师带你参透Kafka:设计原理、消息存储、消息消费原理等等

    本文转载自: linkedkeeper.com,作者:张松然 推荐阅读: 一个月面试了3家大厂Java岗,我发现这几个突破点 目录 Kafka的基本介绍 Kafka的设计原理分析 Kafka数据传输的 ...

  6. 【MQ】Kafka笔记

    笔记来源:尚硅谷视频笔记2.0版+2.1版 黑马视频:Kafka深入探秘者来了 kafka笔记地址:https://blog.csdn.net/hancoder/article/details/107 ...

  7. 【Kafka笔记】4.Kafka API详细解析 Java版本(Producer API,Consumer API,拦截器等)

    简介 Kafka的API有Producer API,Consumer API还有自定义Interceptor (自定义拦截器),以及处理的流使用的Streams API和构建连接器的Kafka Con ...

  8. Kafka部署、原理和使用介绍

    Kafka简介及Kafka部署.原理和使用介绍 Kafka简介 定义 Kafka是一种消息队列,是一个分布式的基于发布/订阅模式的,主要用来处理大量数据状态下的消息队列,一般用来做日志的处理.既然是消 ...

  9. 限量笔记!Alibaba技术官都叹服的,Kafka笔记你逃不了的!

    前言: Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就 ...

最新文章

  1. iphone录屏怎么录声音_ev录屏怎么录制声音 ev录屏声音设置教程
  2. 安师大计算机与信息学院导师,安徽师范大学数学计算机科学学院导师介绍:罗永龙...
  3. java和python可以在一个项目中同时使用么-java调用python的几种用法(看这篇就够了)...
  4. ffmpeg h264+ts +udp传输
  5. 作者:温孚江,现任山东农业大学校长、教授,农业大数据创新战略联盟理事长,全国人民代表大会常务委员会委员。...
  6. MiniFrameworkPHP开源框架
  7. Maven scope中import的作用
  8. c语言模糊pid算法实例,模糊PID的c语言算法.docx
  9. 表t_od_use_cnt中没有hour字段,所以hour我们直接
  10. MVP架构模式简单示例
  11. 互联网晚报 | 06月08日 星期三 | ​教育部回应高考试题疑泄露;​上海落户新规;字节跳动考虑出售得物少数股份...
  12. -TEST 20 for NOIP 。。。(80-300)-----(( ! ))
  13. C#学员管理系统(源代码)
  14. 用友U8案例实验应收管理后台配置
  15. Android 11 Audio框架探索之AudioTracK(二)
  16. 奇幻之旅,全世界畅游
  17. 还不了解,日志框架吗?
  18. stm32毕设 stm32人体健康状态检测系统(项目开源)
  19. 虹科QA | SWCF2022 12月6日演讲笔记:C波段卫星与5G之间的干扰排查及解决方案
  20. 实时分析数据库 Druid,Mark 一下

热门文章

  1. VAT code VAT NO.
  2. SAP QUERY这个工具的使用
  3. excel中如何筛选重复数据
  4. PP 关于工单领料的总结
  5. ABAP取字符串中的连续数字
  6. SAP发票校验前收货后是否能更改物料价格
  7. 大学计算机基础实验指导试题,(大学计算机基础实验指导)模拟试题(二)参考答案...
  8. java this$0_java中this$0的含义及用法
  9. fpga初始化错误_一种SRAM型FPGA单粒子效应加固平台设计
  10. linux挂载一个文件夹,linux挂载一个文件夹到另一个文件夹