request.timeout.ms 和 Spring 使用 KafkaTemplate 的 Kafka 同步事件发布
request.timeout.ms and Spring Kafka Synchronous Event Publishing with KafkaTemplate
我对配置通过 Spring Kafka 同步发布的事件超时的最佳实践有点困惑。The Spring Kafka documentation 提供了一个使用 ListenableFuture
的 get(SOME_TIME, TimeUnit)
启用超时为 SOME_TIME
的事件同步发布。 (复制如下以供参考)。
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's Producer Configuration Documentation 并看到 Kafka 有一个针对 request.timeout.ms
的配置,它负责 Kafka 中的以下设置。
The configuration controls the maximum amount of time the client will wait for the response of a request. If the response is not received before the timeout elapses the client will resend the request if necessary or fail the request if retries are exhausted.
用某个时间单位配置 template.send(...).get(...)
是否更有意义(例如,10 seconds/10,000 毫秒,如上面 Spring Kafka 的示例所示),或者更好的方法是配置 request.timeout.ms
(连同 retries
)以在内部通过 Kafka 模拟此行为并对 get()
进行无参数调用吗?
使用 no-args get()
从来都不是一个好主意;如果客户端代码中存在一些错误,您可能会永远挂起。
两个超时真的不一样
以后get()
就是得到发送的结果(成功或失败)
如果您的生产者配置在 get()
超时后可以成功,那么您可以获得重复项(假设您在失败后在应用程序级别重试)。
我想 "best practice" 将使用大于 retries * request.timeout.ms
的 get()
超时,但这可能会很长时间。但它将确保您获得发送的真实结果。在这种情况下出现超时应被视为需要调查的异常情况。
我对配置通过 Spring Kafka 同步发布的事件超时的最佳实践有点困惑。The Spring Kafka documentation 提供了一个使用 ListenableFuture
的 get(SOME_TIME, TimeUnit)
启用超时为 SOME_TIME
的事件同步发布。 (复制如下以供参考)。
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's Producer Configuration Documentation 并看到 Kafka 有一个针对 request.timeout.ms
的配置,它负责 Kafka 中的以下设置。
The configuration controls the maximum amount of time the client will wait for the response of a request. If the response is not received before the timeout elapses the client will resend the request if necessary or fail the request if retries are exhausted.
用某个时间单位配置 template.send(...).get(...)
是否更有意义(例如,10 seconds/10,000 毫秒,如上面 Spring Kafka 的示例所示),或者更好的方法是配置 request.timeout.ms
(连同 retries
)以在内部通过 Kafka 模拟此行为并对 get()
进行无参数调用吗?
使用 no-args get()
从来都不是一个好主意;如果客户端代码中存在一些错误,您可能会永远挂起。
两个超时真的不一样
以后get()
就是得到发送的结果(成功或失败)
如果您的生产者配置在 get()
超时后可以成功,那么您可以获得重复项(假设您在失败后在应用程序级别重试)。
我想 "best practice" 将使用大于 retries * request.timeout.ms
的 get()
超时,但这可能会很长时间。但它将确保您获得发送的真实结果。在这种情况下出现超时应被视为需要调查的异常情况。