Spring Kafka分区

tap*_*toe 3 apache-kafka kafka-consumer-api kafka-producer-api spring-kafka

以下两个代码段在发布消息方面的行为有何不同?

方法1

Message<String> message = MessageBuilder.withPayload("testmsg")
        .setHeader(KafkaHeaders.MESSAGE_KEY, "key").setHeader(KafkaHeaders.TOPIC, "test").build();

ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(message);
Run Code Online (Sandbox Code Playgroud)

方法2

ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("test", "testmsg");
Run Code Online (Sandbox Code Playgroud)

主题配置:

$ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
Topic:test   PartitionCount:3    ReplicationFactor:1 Configs:
Topic: test  Partition: 0    Leader: 0   Replicas: 0 Isr: 0
Topic: test  Partition: 1    Leader: 0   Replicas: 0 Isr: 0
Topic: test  Partition: 2    Leader: 0   Replicas: 0 Isr: 0
Run Code Online (Sandbox Code Playgroud)

观察:

如果有3个使用者,则每个分区一个;方法1导致单个使用者从单个分区使用所有消息。采用方法2;消费在3个分区/消费者之间平均分配。

Art*_*lan 5

但是您的代码中有一个答案。第一个与topic提供messageKey

messageKey如果未明确指定,则确实用于确定目标分区:

/**
 * computes partition for given record.
 * if the record has partition returns the value otherwise
 * calls configured partitioner class to compute the partition.
 */
private int partition(ProducerRecord<K, V> record, byte[] serializedKey, byte[] serializedValue, Cluster cluster) {
    Integer partition = record.partition();
    return partition != null ?
            partition :
            partitioner.partition(
                    record.topic(), record.key(), serializedKey, record.value(), serializedValue, cluster);
}
Run Code Online (Sandbox Code Playgroud)

这在哪里DefaultPartitioner

List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
int numPartitions = partitions.size();
if (keyBytes == null) {
    int nextValue = nextValue(topic);
        ...
} else {
   // hash the keyBytes to choose a partition
   return Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
}
Run Code Online (Sandbox Code Playgroud)

因此,所有具有相同消息的消息key都将发送到同一分区。否则,将它们以主题循环方式放置。