由于 Kafka 具有基于主题的发布-订阅架构,我如何使用 Kafka 处理 Web 应用程序的一对一和组消息传递部分?我正在使用 SpringBoot+Angular 堆栈和 Docker Kafka 服务器。
我看到并使用默认分区器类实现了KafkaTemplate.send(TOPIC,message)方法。
但在这里,我不传递钥匙。我有一个简单的自定义分区器类,我还想发送到KafkaTemplate(TOPIC,key,message)这样的 kafka 服务器,其中在 ProducerConfig 中我设置了用于分区的 customPartitioner 类。
我看到如果我提供自定义分区器,KafkaTemplate 的 Will send(Topic, Key, Message) 方法会调用 Partition 方法吗?但我没有完全明白。
public class CustomPartitionar implements Partitioner {
private PartitionMapper newMapper;
public CustomPartitionar(){
newMapper = new PartitionMapper();
}
@Override
public void configure(Map<String, ?> configs) {
}
@Override
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes,Cluster cluster) {
int partition = 0;
String userName = (String) key;
// Find the id …Run Code Online (Sandbox Code Playgroud)