一、KafKa所涉及到的名词概念:

1.    Topic:用于划分Message的逻辑概念,一个Topic可以分布在多个Broker上。

2.    Partition:是Kafka中横向扩展和一切并行化的基础,每个Topic都至少被切分为1个Partition。

3.    Offset:消息在Partition中的编号,编号顺序不跨Partition。

4.    Consumer:用于从Broker中取出/消费Message。

5.    Producer:用于往Broker中发送/生产Message。

6.    Replication:Kafka支持以Partition为单位对Message进行冗余备份,每个Partition都可以配置至少1个Replication(当仅1个Replication时即仅该Partition本身)。

7.    Leader:每个Replication集合中的Partition都会选出一个唯一的Leader,所有的读写请求都由Leader处理。其他Replicas从Leader处把数据更新同步到本地,过程类似大家熟悉的MySQL中的Binlog同步。

8.    Broker:Kafka中使用Broker来接受Producer和Consumer的请求,并把Message持久化到本地磁盘。每个Cluster当中会选举出一个Broker来担任Controller,负责处理Partition的Leader选举,协调Partition迁移等工作。

9.    ISR(In-Sync Replica):是Replicas的一个子集,表示目前Alive且与Leader能够“Catch-up”的Replicas集合。由于读写都是首先落到Leader上,所以一般来说通过同步机制从Leader上拉取数据的Replica都会和Leader有一些延迟(包括了延迟时间和延迟条数两个维度),任意一个超过阈值都会把该Replica踢出ISR。每个Partition都有它自己独立的ISR。

二、KafKa模型和Partition Leader选举说明

kafka消费者模型
1、分区消费模型: 每个分区对应每个消费实例
2、组消费模型
 
kafka两种消费模式服务器端源码对比:
1、分区消费模式有以下特点
 a、指定消费topic、partition和offset通过向服务器发送RPC请求进行消费
 b、需要自己提交offset
 c、需要自己处理各种错误,如:leader切换错误
 d、需要自己处理消费者负载均衡策略
 
2、组消费模式具有以下特点
 a、最终也是通过向服务器发送RPC请求完成的(和分区消费模式一样)
 b、组消费模式由kafka服务器端处理各种错误,然后将消息放入队列再分装为迭代器(队列为FetchedDataChunk对象),客户端只需要在迭代器上迭代取出消息
 c、 由kafka服务器端周期性的通过scheduler提交当前消费的offset,无需客户端负责
d、kafka服务端处理消费者负载均衡
(监控工具kafka offset monitor 和 kafka manager 均是基于组消费模式)
kakfa Partition选主机制
    kafka的leader election 方案解决了上述问题,它在所有broker中选出一个controller,所有的partition的leader选举都有controller决定,controller会将leader的改变直接通过RPC的方式(比zookeeper queue的方式更高效)通知需为此作为响应的Broker

 

三、相关命令用法

1、启动kafka
[root@t_cluster03_109_26 kafka]# bin/kafka-server-start.sh -daemon config/server.properties
  
2、手动创建topic
 
[root@t_cluster03_109_26 kafka]# bin/kafka-topics.sh --create --zookeeper 192.168.109.26:2181 --replication-factor 2 --partitions 1 --topic test
Created topic "test".
 
3、查看topic列表(列出集群当前所有可用的topic)
bin/kafka-topics.sh --list -zookeeper zookeeper_address
 
[root@t_cluster03_109_26 kafka]# bin/kafka-topics.sh --list --zookeeper 192.168.109.26:2181
test
 
 
4、手动启动consumer:
 
[root@t_cluster03_109_26 kafka]#  bin/kafka-console-consumer.sh --zookeeper 192.168.109.25:2181 --topic test1 --from-beginning
 
5、手动启动producer
 [root@t_cluster02_109_25 kafka]# bin/kafka-console-producer.sh --broker-list 192.168.109.25:9092 --topic test1
 
6、查看每个指定topic的分区和副本信息
[root@t_cluster03_109_26 kafka]# bin/kafka-topics.sh --describe --zookeeper 192.168.109.26:2181 --topic testtopic
Topic:testtopic    PartitionCount:2    ReplicationFactor:2    Configs:
    Topic: testtopic    Partition: 0    Leader: 3    Replicas: 3,1    Isr: 3,1
    Topic: testtopic    Partition: 1    Leader: 1    Replicas: 1,2    Isr: 1,2
 
7、增加partition的数量(4是增加后的数)
bin/kafka-topics.sh --zookeeper zookeeper_address  --alter --topic topic_name --partitions 4 
 [root@t_cluster01_109_24 kafka]# bin/kafka-topics.sh --describe --zookeeper 192.168.109.25:2181 --topic test
Topic:test    PartitionCount:1    ReplicationFactor:2    Configs:
    Topic: test    Partition: 0    Leader: 2    Replicas: 2,3    Isr: 2,3
[root@t_cluster01_109_24 kafka]# bin/kafka-topics.sh --alter --zookeeper 192.168.109.25:2181 --topic test --partitions 2
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@t_cluster01_109_24 kafka]# bin/kafka-topics.sh --describe --zookeeper 192.168.109.25:2181 --topic test
Topic:test    PartitionCount:2    ReplicationFactor:2    Configs:
    Topic: test    Partition: 0    Leader: 2    Replicas: 2,3    Isr: 2,3
    Topic: test    Partition: 1    Leader: 1    Replicas: 1,2    Isr: 1,2
[root@t_cluster01_109_24 kafka]# bin/kafka-topics.sh --alter --zookeeper 192.168.109.25:2181 --topic test --partitions 3
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@t_cluster01_109_24 kafka]# bin/kafka-topics.sh --describe --zookeeper 192.168.109.25:2181 --topic test
Topic:test    PartitionCount:3    ReplicationFactor:2    Configs:
    Topic: test    Partition: 0    Leader: 2    Replicas: 2,3    Isr: 2,3
    Topic: test    Partition: 1    Leader: 1    Replicas: 1,2    Isr: 1,2
    Topic: test    Partition: 2    Leader: 1    Replicas: 1,3    Isr: 1,3
 
8、集群leader平衡:
bin/kafka-preferred-replica-election.sh --zookeeper zookeeper_address auto.leader.rebalance.enable=true

注:附带KafKa集群配置文件,相关说明可以查看官方文档

[root@t_cluster01_109_24 config]# grep -v '^#' server.properties|grep -v '^$'
broker.id=1
port=9092
host.name=192.168.109.24
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/kafka/kafkalog
num.partitions=2
num.recovery.threads.per.data.dir=1
log.retention.hours=168
message.max.bytes=502400
default.replication.factor=2
replica.fetch.max.bytes=502400
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.168.109.24:2181,192.168.109.25:2181,192.168.109.26:2181
zookeeper.connection.timeout.ms=6000

Kafka随笔一的更多相关文章

  1. Kafka随笔

    1.选举Leader  Leader 是 Partition 级别的,当一个 Broker 挂掉后,所有 Leader 在该 Broker 上的 Partition 都会被重新选举,选出一个新 Lea ...

  2. 【原创】Kafka console consumer源代码分析(一)

    上一篇中分析了Scala版的console producer代码,这篇文章为读者带来一篇console consumer工作原理分析的随笔.其实不论是哪个consumer,大部分的工作原理都是类似的. ...

  3. jmeter随笔(29)-关于自己的jar包和beanshell的使用

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  4. 大数据入门第十八天——kafka整合flume、storm

    一.实时业务指标分析 1.业务 业务: 订单系统---->MQ---->Kakfa--->Storm 数据:订单编号.订单时间.支付编号.支付时间.商品编号.商家名称.商品价格.优惠 ...

  5. KClient——kafka消息中间件源码解读

    目录 kclient消息中间件 kclient-processor top.ninwoo.kclient.app.KClientApplication top.ninwoo.kclient.app.K ...

  6. Kafka+SpringMVC+Maven应用示例

    本文借助主流SpringMVC框架向大家介绍如何在具体应用中简单快捷的使用kafka.kafka.maven以及SpringMVC在现在的企业级应用中都占据着非常重要的地位,所以本文将三者结合起来也可 ...

  7. kafka+windows+java+springboot中的配置

    1.百度kafka+zookeeper+windows配置 1.1  zookeeper配置 dataDir=/tmp/zookeeper # the port at which the client ...

  8. 线程安全使用(四) [.NET] 简单接入微信公众号开发:实现自动回复 [C#]C#中字符串的操作 自行实现比dotcore/dotnet更方便更高性能的对象二进制序列化 自已动手做高性能消息队列 自行实现高性能MVC WebAPI 面试题随笔 字符串反转

    线程安全使用(四)   这是时隔多年第四篇,主要是因为身在东软受内网限制,好多文章就只好发到东软内部网站,懒的发到外面,现在一点点把在东软写的文章给转移出来. 这里主要讲解下CancellationT ...

  9. Kafka Connect简介

    Kafka Connect简介 http://colobu.com/2016/02/24/kafka-connect/#more Kafka 0.9+增加了一个新的特性Kafka Connect,可以 ...

随机推荐

  1. webpack2新特性

    增加 import() 作为代码分割点:System.import已被弃用,在webpack3时会被完全移除: 内置了json加载器,不再需要单独配置了 当打包文件过大时会提示性能警告,可以用 per ...

  2. ASP.NET中GridView数据导出到Excel

    /// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...

  3. AutoCAD .net 开发 SelectionFilter Foreach Linq 性能比较

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. [翻译] ORMLite document -- Getting Started

    前言 此文档翻译于第一次学习 ORMLite 框架,如果发现当中有什么不对的地方,请指正.若翻译与原文档出现任何的不相符,请以原文档为准.原则上建议学习原英文文档. ----------------- ...

  5. Ubuntu配置Tomcat9非root用户启动

    unix类系统的root用户具有极大的权利,所以很多时候我们不希望程序以root身份启动,这也就是配置Tomcat以指定身份(非root)启动的初衷,虽然也没人来攻击我的服务器,但本着学习学习的目的, ...

  6. Code HighLight

    #!/bin/sh BEG=`date --date '-7 days' +%Y%m%d` END=`date --date '-1 days' +%Y%m%d` #BEG='20140509' #E ...

  7. Redis 主从配置

    环境     Master/Slave     系统 IP Redis版本 Master     CentOS6.7         10.10.3.211         redis-3.2.6   ...

  8. monodroid 调用 JNI Native 的一些问题

    在Android版本开发的过程中,需要使用一些用JNI开发的NDK的native库.这里谈一谈踩到的坑,给大家参考. 虽然java的程序我还算熟悉,但是没有了解过 JNI Native 的开发,一般是 ...

  9. 弱省互测#0 t3

    Case 1 题意 要求给出下面代码的答案然后构造输入. 给一个图, n 个点 m 条边 q 次询问,输出所有点对之间最大权值最小的路径. 题解 把每一个询问的输出看成一条边,建一棵最小生成树. Ca ...

  10. 使用display:table来解决一些问题

    一直有,多栏的需求,当然用table布局,很快就做完了.不怎么喜欢用table,刚开始使用display:table,但是有一条老是不能达到我的效果,那就是有一行不固定宽度的时候,就不知道怎么处理,今 ...