max.in.flight.requests.per.connection和使用KafkaTemplate

时间:2019-06-04 15:20:24

标签: apache-kafka kafka-producer-api spring-kafka

我对卡夫卡生产者的max.in.flight.requests.per.connection与使用Spring-Kafka同步发布事件之间的关系感到困惑,并希望有人能够消除两者之间的关系。

我希望使用Spring Kafka的KafkaTemplate与Spring Kafka设置同步事件发布。 The Spring Kafka documentation提供了一个示例,该示例使用ListenableFuture的{​​{1}}来启用事件的同步发布(下面重复以供参考)。

get(SOME_TIME, TimeUnit)

我看着Kafka's Producer Configuration Documentation,发现Kafka具有public void sendToKafka(final MyOutputData data) { final ProducerRecord<String, String> record = createRecord(data); try { template.send(record).get(10, TimeUnit.SECONDS); handleSuccess(data); } catch (ExecutionException e) { handleFailure(data, record, e.getCause()); } catch (TimeoutException | InterruptedException e) { handleFailure(data, record, e); } } 的配置,该配置负责Kafka中的以下设置。

  

在阻塞之前客户端将在单个连接上发送的未确认请求的最大数量。请注意,如果将此设置设置为大于1并且发送失败,则存在由于重试(即,如果启用重试)而导致消息重新排序的风险。

在异步处理事件发布时,max.in.flight.requests.per.connection会将设置为1的值赋予哪个值?将max.in.flight.requests.per.connection的值设置为1是否会强制为Kafka Producer同步发布事件?如果我想为Kafka Producer设置事件的同步发布并采用Spring-Kafka推荐的方法,我应该担心max.in.flight.requests.per.connection还是可以忽略这一点?

1 个答案:

答案 0 :(得分:2)

我根本不认为它们之间有关系。发送仍然是异步的;将其设置为一个意味着第二个将阻塞直到第一个完成。

 future1 = template.send(...);
 future2 = template.send(...); // this will block
 future1.get(); // and this will return almost immediately
 future2.get();

您仍然需要获取未来的结果,以测试成功/失败。