简体   繁体   English

使用kafka partitioner作为锁

[英]Use kafka partitioner as a lock

I have a spring boot application that sends messages into a Kafka topic, something like:我有一个 spring 启动应用程序,它将消息发送到 Kafka 主题,例如:

public class Message {
    
    @JsonProperty("customer_id")
    @SerializedName("customer_id")
    private String customerId;
    ...
}

For the sake of some business logic, I do not want to process 2 messages of the same customer in parallel.为了某些业务逻辑,我不想并行处理同一客户的 2 条消息。 I want to use the Kafka partition order as some kind of lock.我想使用 Kafka 分区顺序作为某种锁。 As far as I could understand, I can use the customerId as a key to my message and use a custom partitioner (implements Kafka partitioner) to direct the message with the same key to the same partition.据我所知,我可以使用 customerId 作为消息的键,并使用自定义分区程序(实现 Kafka 分区程序)将具有相同键的消息定向到同一分区。 And though I assume I am not the first one in history to seek this kind of functionality I've found it hard to find some kind of library/documentation for implementing such a thing.虽然我假设我不是历史上第一个寻求这种功能的人,但我发现很难找到某种库/文档来实现这种功能。 Can anyone guide me to an existing solution to my needs?谁能指导我找到满足我需求的现有解决方案?

That is the behavior of the default partitioner;这是默认分区程序的行为; see its javadocs:查看它的 javadocs:

/**
 * The default partitioning strategy:
 * <ul>
 * <li>If a partition is specified in the record, use it
 * <li>If no partition is specified but a key is present choose a partition based on a hash of the key
 * <li>If no partition or key is present choose the sticky partition that changes when the batch is full.
 * 
 * See KIP-480 for details about sticky partitioning.
 */
public class DefaultPartitioner implements Partitioner {

Changing the number of partitions in the topic will change the destination partition so you might want to consider a custom partitioner.更改主题中的分区数将更改目标分区,因此您可能需要考虑自定义分区程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM