一 Kafka概述
1.1 Kafka是什么
在流式計算中,Kafka一般用來緩存數據,Storm通過消費Kafka的數據進行計算。
1)Apache Kafka是一個開源消息系統,由Scala寫成。是由Apache軟件基金會開發的一個開源消息系統項目。
2)Kafka最初是由LinkedIn公司開發,並於 2011年初開源。2012年10月從Apache Incubator畢業。該項目的目標是為處理實時數據提供一個統一、高通量、低等待的平台。
3)Kafka是一個分布式消息隊列。Kafka對消息保存時根據Topic進行歸類,發送消息者稱為Producer,消息接受者稱為Consumer,此外kafka集群有多個kafka實例組成,每個實例(server)成為broker。
4)無論是kafka集群,還是producer和consumer都依賴於zookeeper集群保存一些meta信息,來保證系統可用性。
1.2 消息隊列內部實現原理
(1)點對點模式(一對一,消費者主動拉取數據,消息收到后消息清除)
點對點模型通常是一個基於拉取或者輪詢的消息傳送模型,這種模型從隊列中請求信息,而不是將消息推送到客戶端。這個模型的特點是發送到隊列的消息被一個且只有一個接收者接收處理,即使有多個消息監聽者也是如此。
(2)發布/訂閱模式(一對多,數據生產后,推送給所有訂閱者)
發布訂閱模型則是一個基於推送的消息傳送模型。發布訂閱模型可以有多種不同的訂閱者,臨時訂閱者只在主動監聽主題時才接收消息,而持久訂閱者則監聽主題的所有消息,即使當前訂閱者不可用,處於離線狀態。
1.3 為什么需要消息隊列
1)解耦:
允許你獨立的擴展或修改兩邊的處理過程,只要確保它們遵守同樣的接口約束。
2)冗余:
消息隊列把數據進行持久化直到它們已經被完全處理,通過這一方式規避了數據丟失風險。許多消息隊列所采用的"插入-獲取-刪除"范式中,在把一個消息從隊列中刪除之前,需要你的處理系統明確的指出該消息已經被處理完畢,從而確保你的數據被安全的保存直到你使用完畢。
3)擴展性:
因為消息隊列解耦了你的處理過程,所以增大消息入隊和處理的頻率是很容易的,只要另外增加處理過程即可。
4)靈活性 & 峰值處理能力:
在訪問量劇增的情況下,應用仍然需要繼續發揮作用,但是這樣的突發流量並不常見。如果為以能處理這類峰值訪問為標准來投入資源隨時待命無疑是巨大的浪費。使用消息隊列能夠使關鍵組件頂住突發的訪問壓力,而不會因為突發的超負荷的請求而完全崩潰。
5)可恢復性:
系統的一部分組件失效時,不會影響到整個系統。消息隊列降低了進程間的耦合度,所以即使一個處理消息的進程掛掉,加入隊列中的消息仍然可以在系統恢復后被處理。
6)順序保證:
在大多使用場景下,數據處理的順序都很重要。大部分消息隊列本來就是排序的,並且能保證數據會按照特定的順序來處理。(Kafka保證一個Partition內的消息的有序性)
7)緩沖:
有助於控制和優化數據流經過系統的速度,解決生產消息和消費消息的處理速度不一致的情況。
8)異步通信:
很多時候,用戶不想也不需要立即處理消息。消息隊列提供了異步處理機制,允許用戶把一個消息放入隊列,但並不立即處理它。想向隊列中放入多少消息就放多少,然后在需要的時候再去處理它們。
1.4 Kafka架構
1)Producer :消息生產者,就是向kafka broker發消息的客戶端。
2)Consumer :消息消費者,向kafka broker取消息的客戶端
3)Topic :可以理解為一個隊列。
4) Consumer Group (CG):這是kafka用來實現一個topic消息的廣播(發給所有的consumer)和單播(發給任意一個consumer)的手段。一個topic可以有多個CG。topic的消息會復制-給consumer。如果需要實現廣播,只要每個consumer有一個獨立的CG就可以了。要實現單播只要所有的consumer在同一個CG。用CG還可以將consumer進行自由的分組而不需要多次發送消息到不同的topic。
5)Broker :一台kafka服務器就是一個broker。一個集群由多個broker組成。一個broker可以容納多個topic。
6)Partition:為了實現擴展性,一個非常大的topic可以分布到多個broker(即服務器)上,一個topic可以分為多個partition,每個partition是一個有序的隊列。partition中的每條消息都會被分配一個有序的id(offset)。kafka只保證按一個partition中的順序將消息發給consumer,不保證一個topic的整體(多個partition間)的順序。
7)Offset:kafka的存儲文件都是按照offset.kafka來命名,用offset做名字的好處是方便查找。例如你想找位於2049的位置,只要找到2048.kafka的文件即可。當然the first offset就是00000000000.kafka
1.5 分布式模型
Kafka每個主題的多個分區日志分布式地存儲在Kafka集群上,同時為了故障容錯,每個分區都會以副本的方式復制到多個消息代理節點上。其中一個節點會作為主副本(Leader),其他節點作為備份副本(Follower,也叫作從副本)。主副本會負責所有的客戶端讀寫操作,備份副本僅僅從主副本同步數據。當主副本出現故障時,備份副本中的一個副本會被選擇為新的主副本。因為每個分區的副本中只有主副本接受讀寫,所以每個服務器端都會作為某些分區的主副本,以及另外一些分區的備份副本,這樣Kafka集群的所有服務端整體上對客戶端是負載均衡的。
Kafka的生產者和消費者相對於服務器端而言都是客戶端。
Kafka生產者客戶端發布消息到服務端的指定主題,會指定消息所屬的分區。生產者發布消息時根據消息是否有鍵,采用不同的分區策略。消息沒有鍵時,通過輪詢方式進行客戶端負載均衡;消息有鍵時,根據分區語義(例如hash)確保相同鍵的消息總是發送到同一分區。
Kafka的消費者通過訂閱主題來消費消息,並且每個消費者都會設置一個消費組名稱。因為生產者發布到主題的每一條消息都只會發送給消費者組的一個消費者。所以,如果要實現傳統消息系統的“隊列”模型,可以讓每個消費者都擁有相同的消費組名稱,這樣消息就會負責均衡到所有的消費者;如果要實現“發布-訂閱”模型,則每個消費者的消費者組名稱都不相同,這樣每條消息就會廣播給所有的消費者。
分區是消費者現場模型的最小並行單位。如下圖(圖1)所示,生產者發布消息到一台服務器的3個分區時,只有一個消費者消費所有的3個分區。在下圖(圖2)中,3個分區分布在3台服務器上,同時有3個消費者分別消費不同的分區。假設每個服務器的吞吐量時300MB,在下圖(圖1)中分攤到每個分區只有100MB,而在下圖(圖2)中,集群整體的吞吐量有900MB。可以看到,增加服務器節點會提升集群的性能,增加消費者數量會提升處理性能。
同一個消費組下多個消費者互相協調消費工作,Kafka會將所有的分區平均地分配給所有的消費者實例,這樣每個消費者都可以分配到數量均等的分區。Kafka的消費組管理協議會動態地維護消費組的成員列表,當一個新消費者加入消費者組,或者有消費者離開消費組,都會觸發再平衡操作。
Kafka的消費者消費消息時,只保證在一個分區內的消息的完全有序性,並不保證同一個主題匯中多個分區的消息順序。而且,消費者讀取一個分區消息的順序和生產者寫入到這個分區的順序是一致的。比如,生產者寫入“hello”和“Kafka”兩條消息到分區P1,則消費者讀取到的順序也一定是“hello”和“Kafka”。如果業務上需要保證所有消息完全一致,只能通過設置一個分區完成,但這種做法的缺點是最多只能有一個消費者進行消費。一般來說,只需要保證每個分區的有序性,再對消息假設鍵來保證相同鍵的所有消息落入同一分區,就可以滿足絕大多數的應用。
二 Kafka集群部署
2.1 環境准備
2.1.1 集群規划
hadoop102 hadoop103 hadoop104
zk zk zk
kafka kafka kafka
2.1.2 jar包下載
http://kafka.apache.org/downloads.html
2.1.3 虛擬機准備
1)准備3台虛擬機
2)配置ip地址
3)配置主機名稱
4)3台主機分別關閉防火牆
[root@hadoop102 atguigu]# chkconfig iptables off
[root@hadoop103 atguigu]# chkconfig iptables off
[root@hadoop104 atguigu]# chkconfig iptables off
2.1.4 安裝jdk
2.1.5 安裝Zookeeper
0)集群規划
在hadoop102、hadoop103和hadoop104三個節點上部署Zookeeper。
1)解壓安裝
(1)解壓zookeeper安裝包到/opt/module/目錄下
[atguigu@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
(2)在/opt/module/zookeeper-3.4.10/這個目錄下創建zkData
mkdir -p zkData
(3)重命名/opt/module/zookeeper-3.4.10/conf這個目錄下的zoo_sample.cfg為zoo.cfg
mv zoo_sample.cfg zoo.cfg
2)配置zoo.cfg文件
(1)具體配置
dataDir=/opt/module/zookeeper-3.4.10/zkData
增加如下配置
#######################cluster##########################
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
server.4=hadoop104:2888:3888
(2)配置參數解讀
Server.A=B:C:D。
A是一個數字,表示這個是第幾號服務器;
B是這個服務器的ip地址;
C是這個服務器與集群中的Leader服務器交換信息的端口;
D是萬一集群中的Leader服務器掛了,需要一個端口來重新進行選舉,選出一個新的Leader,而這個端口就是用來執行選舉時服務器相互通信的端口。
集群模式下配置一個文件myid,這個文件在dataDir目錄下,這個文件里面有一個數據就是A的值,Zookeeper啟動時讀取此文件,拿到里面的數據與zoo.cfg里面的配置信息比較從而判斷到底是哪個server。
3)集群操作
(1)在/opt/module/zookeeper-3.4.10/zkData目錄下創建一個myid的文件
touch myid
添加myid文件,注意一定要在linux里面創建,在notepad++里面很可能亂碼
(2)編輯myid文件
vi myid
在文件中添加與server對應的編號:如2
(3)拷貝配置好的zookeeper到其他機器上
scp -r zookeeper-3.4.10/ root@hadoop103.atguigu.com:/opt/app/
scp -r zookeeper-3.4.10/ root@hadoop104.atguigu.com:/opt/app/
並分別修改myid文件中內容為3、4
(4)分別啟動zookeeper
[root@hadoop102 zookeeper-3.4.10]# bin/zkServer.sh start
[root@hadoop103 zookeeper-3.4.10]# bin/zkServer.sh start
[root@hadoop104 zookeeper-3.4.10]# bin/zkServer.sh start
(5)查看狀態
[root@hadoop102 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@hadoop103 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@hadoop104 zookeeper-3.4.5]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
2.2 Kafka集群部署
1)解壓安裝包
[atguigu@hadoop102 software]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C /opt/module/
2)修改解壓后的文件名稱
[atguigu@hadoop102 module]$ mv kafka_2.11-0.11.0.0/ kafka
3)在/opt/module/kafka目錄下創建logs文件夾
[atguigu@hadoop102 kafka]$ mkdir logs
4)修改配置文件
[atguigu@hadoop102 kafka]$ cd config/
[atguigu@hadoop102 config]$ vi server.properties
輸入以下內容:
#broker的全局唯一編號,不能重復 broker.id=0 #是否允許刪除topic delete.topic.enable=true #處理網絡請求的線程數量 num.network.threads=3 #用來處理磁盤IO的線程數量 num.io.threads=8 #發送套接字的緩沖區大小 socket.send.buffer.bytes=102400 #接收套接字的緩沖區大小 socket.receive.buffer.bytes=102400 #請求套接字的最大緩沖區大小 socket.request.max.bytes=104857600 #kafka運行日志存放的路徑 log.dirs=/opt/module/kafka/logs #topic在當前broker上的分區個數 num.partitions=1 #用來恢復和清理data下數據的線程數量 num.recovery.threads.per.data.dir=1 #segment文件保留的最長時間,超時將被刪除 log.retention.hours=168 #配置連接Zookeeper集群地址 zookeeper.connect=hadoop102:2181,hadoop103:2181,hadoop104:2181 |
5)配置環境變量
[root@hadoop102 module]# vi /etc/profile
#KAFKA_HOME export KAFKA_HOME=/opt/module/kafka export PATH=$PATH:$KAFKA_HOME/bin |
[root@hadoop102 module]# source /etc/profile
6)分發安裝包
[root@hadoop102 etc]# xsync profile
[atguigu@hadoop102 module]$ xsync kafka/
7)分別在hadoop103和hadoop104上修改配置文件/opt/module/kafka/config/server.properties中的broker.id=1、broker.id=2
注:broker.id不得重復
8)啟動集群
依次在hadoop102、hadoop103、hadoop104節點上啟動kafka
[atguigu@hadoop102 kafka]$ bin/kafka-server-start.sh config/server.properties &
[atguigu@hadoop103 kafka]$ bin/kafka-server-start.sh config/server.properties &
[atguigu@hadoop104 kafka]$ bin/kafka-server-start.sh config/server.properties &
9)關閉集群
[atguigu@hadoop102 kafka]$ bin/kafka-server-stop.sh stop
[atguigu@hadoop103 kafka]$ bin/kafka-server-stop.sh stop
[atguigu@hadoop104 kafka]$ bin/kafka-server-stop.sh stop
2.3 Kafka命令行操作
1)查看當前服務器中的所有topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --list
2)創建topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --create --replication-factor 3 --partitions 1 --topic first
選項說明:
--topic 定義topic名
--replication-factor 定義副本數
--partitions 定義分區數
3)刪除topic
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --delete --topic first
需要server.properties中設置delete.topic.enable=true否則只是標記刪除或者直接重啟。
4)發送消息
[atguigu@hadoop102 kafka]$ bin/kafka-console-producer.sh --broker-list hadoop102:9092 --topic first
>hello world
>atguigu atguigu
5)消費消息
[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh --zookeeper hadoop102:2181 --from-beginning --topic first
--from-beginning:會把first主題中以往所有的數據都讀取出來。根據業務場景選擇是否增加該配置。
6)查看某個Topic的詳情
[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --describe --topic first
2.4 Kafka配置信息
2.4.1 Broker配置信息
屬性 |
默認值 |
描述 |
broker.id |
|
必填參數,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 |
H 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. |
2.4.2 Producer配置信息
屬性 |
默認值 |
描述 |
metadata.broker.list |
|
啟動時producer查詢brokers的列表,可以是集群中所有brokers的一個子集。注意,這個參數只是用來獲取topic的元信息用,producer會從元信息中挑選合適的broker並與之建立socket連接。格式是:host1: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 |
|
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. |
2.4.3 Consumer配置信息
屬性 |
默認值 |
描述 |
group.id |
|
Consumer的組ID,相同goup.id的consumer屬於同一個組。 |
zookeeper.connect |
|
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大小的數據量。 |
auto.commit.interval.ms |
60 * 1000 |
Consumer提交offset值到zookeeper的周期。 |
queued.max.message.chunks |
2 |
用來被consumer消費的message chunks 數量, 每個chunk可以緩存fetch.message.max.bytes大小的數據量。 |
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 |