Kafka為broker,producer和consumer提供了很多的配置參數。 了解並理解這些配置參數對於我們使用kafka是非常重要的。
本文列出了一些重要的配置參數。
官方的文檔 Configuration比較老了,很多參數有所變動, 有些名字也有所改變。我在整理的過程中根據0.8.2的代碼也做了修正。
Boker配置參數
下表列出了Boker的重要的配置參數, 更多的配置請參考 kafka.server.KafkaConfig
name | 默認值 | 描述 |
---|---|---|
brokerid | none | 每一個boker都有一個唯一的id作為它們的名字。 這就允許boker切換到別的主機/端口上, consumer依然知道 |
enable.zookeeper | true | 允許注冊到zookeeper |
log.flush.interval.messages | Long.MaxValue | 在數據被寫入到硬盤和消費者可用前最大累積的消息的數量 |
log.flush.interval.ms | Long.MaxValue | 在數據被寫入到硬盤前的最大時間 |
log.flush.scheduler.interval.ms | Long.MaxValue | 檢查數據是否要寫入到硬盤的時間間隔。 |
log.retention.hours | 168 | 控制一個log保留多長個小時 |
log.retention.bytes | -1 | 控制log文件最大尺寸 |
log.cleaner.enable | false | 是否log cleaning |
log.cleanup.policy | delete | delete還是compat. 其它控制參數還包括log.cleaner.threads,log.cleaner.io.max.bytes.per.second,log.cleaner.dedupe.buffer.size,log.cleaner.io.buffer.size,log.cleaner.io.buffer.load.factor,log.cleaner.backoff.ms,log.cleaner.min.cleanable.ratio,log.cleaner.delete.retention.ms |
log.dir | /tmp/kafka-logs | 指定log文件的根目錄 |
log.segment.bytes | 110241024*1024 | 單一的log segment文件大小 |
log.roll.hours | 24 * 7 | 開始一個新的log文件片段的最大時間 |
message.max.bytes | 1000000 + MessageSet.LogOverhead | 一個socket 請求的最大字節數 |
num.network.threads | 3 | 處理網絡請求的線程數 |
num.io.threads | 8 | 處理IO的線程數 |
background.threads | 10 | 后台線程序 |
num.partitions | 1 | 默認分區數 |
socket.send.buffer.bytes | 102400 | socket SO_SNDBUFF參數 |
socket.receive.buffer.bytes | 102400 | socket SO_RCVBUFF參數 |
zookeeper.connect | localhost:2182/kafka | 指定zookeeper連接字符串, 格式如hostname:port/chroot。chroot是一個namespace |
zookeeper.connection.timeout.ms | 6000 | 指定客戶端連接zookeeper的最大超時時間 |
zookeeper.session.timeout.ms | 6000 | 連接zk的session超時時間 |
zookeeper.sync.time.ms | 2000 | zk follower落后於zk leader的最長時間 |
High-level Consumer配置參數
下表列出了high-level consumer的重要的配置參數。
更多的配置請參考 kafka.consumer.ConsumerConfig
name | 默認值 | 描述 |
---|---|---|
groupid | groupid | 一個字符串用來指示一組consumer所在的組 |
socket.timeout.ms | 30000 | socket超時時間 |
socket.buffersize | 64*1024 | socket receive buffer |
fetch.size | 300 * 1024 | 控制在一個請求中獲取的消息的字節數。 這個參數在0.8.x中由fetch.message.max.bytes,fetch.min.bytes取代 |
backoff.increment.ms | 1000 | 這個參數避免在沒有新數據的情況下重復頻繁的拉數據。 如果拉到空數據,則多推后這個時間 |
queued.max.message.chunks | 2 | high level consumer內部緩存拉回來的消息到一個隊列中。 這個值控制這個隊列的大小 |
auto.commit.enable | true | 如果true,consumer定期地往zookeeper寫入每個分區的offset |
auto.commit.interval.ms | 10000 | 往zookeeper上寫offset的頻率 |
auto.offset.reset | largest | 如果offset出了返回,則 smallest : 自動設置reset到最小的offset. largest : 自動設置offset到最大的offset. 其它值不允許,會拋出異常. |
consumer.timeout.ms | -1 | 默認-1,consumer在沒有新消息時無限期的block。如果設置一個正值, 一個超時異常會拋出 |
rebalance.retries.max | 4 | rebalance時的最大嘗試次數 |
Producer配置參數
下表列出了producer的重要的參數。
更多的配置請參考 kafka.producer.ProducerConfig
name | 默認值 | 描述 |
---|---|---|
serializer.class | kafka.serializer.DefaultEncoder | 必須實現kafka.serializer.Encoder接口,將T類型的對象encode成kafka message |
key.serializer.class | serializer.class | key對象的serializer類 |
partitioner.class | kafka.producer.DefaultPartitioner | 必須實現kafka.producer.Partitioner,根據Key提供一個分區策略 |
producer.type | sync | 指定消息發送是同步還是異步。異步asyc成批發送用kafka.producer.AyncProducer, 同步sync用kafka.producer.SyncProducer |
metadata.broker.list | boker list | 使用這個參數傳入boker和分區的靜態信息,如host1:port1,host2:port2, 這個可以是全部boker的一部分 |
compression.codec | NoCompressionCodec | 消息壓縮,默認不壓縮 |
compressed.topics | null | 在設置了壓縮的情況下,可以指定特定的topic壓縮,為指定則全部壓縮 |
message.send.max.retries | 3 | 消息發送最大嘗試次數 |
retry.backoff.ms | 300 | 每次嘗試增加的額外的間隔時間 |
topic.metadata.refresh.interval.ms | 600000 | 定期的獲取元數據的時間。當分區丟失,leader不可用時producer也會主動獲取元數據,如果為0,則每次發送完消息就獲取元數據,不推薦。如果為負值,則只有在失敗的情況下獲取元數據。 |
queue.buffering.max.ms | 5000 | 在producer queue的緩存的數據最大時間,僅僅for asyc |
queue.buffering.max.message | 10000 | producer 緩存的消息的最大數量,僅僅for asyc |
queue.enqueue.timeout.ms | -1 | 0當queue滿時丟掉,負值是queue滿時block,正值是queue滿時block相應的時間,僅僅for asyc |
batch.num.messages | 200 | 一批消息的數量,僅僅for asyc |
request.required.acks | 0 | 0表示producer毋須等待leader的確認,1代表需要leader確認寫入它的本地log並立即確認,-1代表所有的備份都完成后確認。 僅僅for sync |
request.timeout.ms | 10000 | 確認超時時間 |
kafka.serializer.DefaultEncoder
默認的這個Encoder事實上不做任何處理,接收到什么byte[]就返回什么byte[]:
class DefaultEncoder(props: VerifiableProperties = null) extends Encoder[Array[Byte]] { override def toBytes(value: Array[Byte]): Array[Byte] = value }
NullEncoder
則不管接收什么都返回null:
class NullEncoder[T](props: VerifiableProperties = null) extends Encoder[T] { override def toBytes(value: T): Array[Byte] = null }
StringEncoder
則返回字符串,默認UTF-8格式:
class StringEncoder(props: VerifiableProperties = null) extends Encoder[String] { val encoding = if(props == null) "UTF8" else props.getString("serializer.encoding", "UTF8") override def toBytes(s: String): Array[Byte] = if(s == null) null else s.getBytes(encoding) }
kafka.producer.DefaultPartitioner
默認的分區函數為DefaultPartitioner
,它根據key的hashcode與分區數取余,得到相應的分區。
class DefaultPartitioner(props: VerifiableProperties = null) extends Partitioner { private val random = new java.util.Random def partition(key: Any, numPartitions: Int): Int = { Utils.abs(key.hashCode) % numPartitions } }
但是如果key為null時會發送到哪個分區?在一定時間內往一個特定的分區發送,超過一定時間又會隨機選擇一個,請參考 key為null時Kafka會將消息發送給哪個分區? .所以推薦你發送Kafka消息時總是指定一個key,以便消息能均勻的分到每個分區上。
Kafka為broker,producer和consumer提供了很多的配置參數。 了解並理解這些配置參數對於我們使用kafka是非常重要的。
本文列出了一些重要的配置參數。
官方的文檔 Configuration比較老了,很多參數有所變動, 有些名字也有所改變。我在整理的過程中根據0.8.2的代碼也做了修正。