2016-04-26 78 views
0

我有一個卡夫卡生產者,基本上做下面的工作。我有一個至少有10個分區的主題,我不在乎它們消耗的順序(我的後端將處理它)。我還將啓動至少10個消費者(假設每個消費者都緊盯着1個分區)。如果我開始發佈郵件(使用下面的代碼),kafka會處理負載並將郵件均勻地放在所有分區中,或者我應該引入一個密鑰(這對我的應用程序來說真的沒有關係)並自行實施輪循機制?向卡夫卡發佈消息的最佳方式是什麼?

KeyedMessage<String, String> data = new KeyedMessage<>(topic, txt); 
producer.send(data); 
producer.close(); 

有什麼想法?

回答

1

在默認情況下org.apache.kafka.clients.producer.internals.DefaultPartitioner將根據代碼中使用

if (keyBytes == null) { 
     int nextValue = counter.getAndIncrement(); 
     List<PartitionInfo> availablePartitions =  cluster.availablePartitionsForTopic(topic); 
     if (availablePartitions.size() > 0) { 
      int part = DefaultPartitioner.toPositive(nextValue) % availablePartitions.size(); 
      return availablePartitions.get(part).partition(); 
     } else { 
      // no partitions are available, give a non-available partition 
      return DefaultPartitioner.toPositive(nextValue) % numPartitions; 
     } 
    } else { 
     // hash the keyBytes to choose a partition 
     return DefaultPartitioner.toPositive(Utils.murmur2(keyBytes)) % numPartitions; 
    } 

link to source code

,卡夫卡將平分所有分區

之間的所有消息
相關問題