即使代理关闭,Kafka 仍会继续生成请求
Kafka keeps producing requests even if broker is down
目前,当我创建生产者来发送我的记录时,例如由于某些原因 kafka 不可用,生产者会无限期地发送相同的消息。例如,在我收到此错误 3 次后如何停止生成消息:
Connection to node -1 could not be established. Broker may not be available.
我正在使用 reactor kafka 生产者:
@Bean
public KafkaSender<String, String> createSender() {
return KafkaSender.create(senderOptions());
}
private SenderOptions<String, String> senderOptions() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
props.put(ProducerConfig.CLIENT_ID_CONFIG, kafkaProperties.getClientId());
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.RETRIES_CONFIG, kafkaProperties.getProducerRetries());
return SenderOptions.create(props);
}
然后用它来发送记录:
sender.send(Mono.just(SenderRecord.create(new ProducerRecord<>(topicName, null, message), message)))
.flatMap(result -> {
if (result.exception() != null) {
return Flux.just(ResponseEntity.badRequest()
.body(result.exception().getMessage()));
}
return Flux.just(ResponseEntity.ok().build());
})
.next();
而不是专注于错误。解决问题 - 它没有连接到代理
您没有在撰写文件中覆盖它,因此您的应用正在尝试连接到自身
bootstrap-servers: ${KAFKA_BOOTSTRAP_URL:localhost:9092}
在 compose yml 中,您似乎忘记了这个
rest-proxy:
environment:
KAFKA_BOOTSTRAP_URL: kafka:9092
或者,如果可能,您可以使用现有的 Confluent REST 代理 docker 映像,而不是重新发明轮子
您可以使用 circuit breaker pattern 来解决此类问题,但在应用此模式之前尝试找到根本原因,并且您的 ProducerConfig.RETRIES_CONFIG 属性 似乎在某处被覆盖了。
恐怕 clusterAndWaitTime = waitOnMetadata(record.topic(), record.partition(), maxBlockTimeMs);
不参与重试,它默认迭代到 maxBlockTimeMs = 60000
。您可以通过 ProducerConfig.MAX_BLOCK_MS_CONFIG
属性:
为生产者减少此选项
public static final String MAX_BLOCK_MS_CONFIG = "max.block.ms";
private static final String MAX_BLOCK_MS_DOC = "The configuration controls how long <code>KafkaProducer.send()</code> and <code>KafkaProducer.partitionsFor()</code> will block."
+ "These methods can be blocked either because the buffer is full or metadata unavailable."
+ "Blocking in the user-supplied serializers or partitioner will not be counted against this timeout.";
更新
我们可以这样解决问题:
@PostMapping(path = "/v1/{topicName}")
public Mono<ResponseEntity<?>> postData(
@PathVariable("topicName") String topicName, String message) {
return sender.send(Mono.just(SenderRecord.create(new ProducerRecord<>(topicName, null, message), message)))
.flatMap(result -> {
if (result.exception() != null) {
sender.close();
return Flux.just(ResponseEntity.badRequest()
.body(result.exception().getMessage()));
}
return Flux.just(ResponseEntity.ok().build());
})
.next();
}
注意sender.close();
中的错误。
我认为是时候针对 Reactor Kafka 项目提出问题,以允许在错误时关闭生产者。
目前,当我创建生产者来发送我的记录时,例如由于某些原因 kafka 不可用,生产者会无限期地发送相同的消息。例如,在我收到此错误 3 次后如何停止生成消息:
Connection to node -1 could not be established. Broker may not be available.
我正在使用 reactor kafka 生产者:
@Bean
public KafkaSender<String, String> createSender() {
return KafkaSender.create(senderOptions());
}
private SenderOptions<String, String> senderOptions() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
props.put(ProducerConfig.CLIENT_ID_CONFIG, kafkaProperties.getClientId());
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.RETRIES_CONFIG, kafkaProperties.getProducerRetries());
return SenderOptions.create(props);
}
然后用它来发送记录:
sender.send(Mono.just(SenderRecord.create(new ProducerRecord<>(topicName, null, message), message)))
.flatMap(result -> {
if (result.exception() != null) {
return Flux.just(ResponseEntity.badRequest()
.body(result.exception().getMessage()));
}
return Flux.just(ResponseEntity.ok().build());
})
.next();
而不是专注于错误。解决问题 - 它没有连接到代理
您没有在撰写文件中覆盖它,因此您的应用正在尝试连接到自身
bootstrap-servers: ${KAFKA_BOOTSTRAP_URL:localhost:9092}
在 compose yml 中,您似乎忘记了这个
rest-proxy:
environment:
KAFKA_BOOTSTRAP_URL: kafka:9092
或者,如果可能,您可以使用现有的 Confluent REST 代理 docker 映像,而不是重新发明轮子
您可以使用 circuit breaker pattern 来解决此类问题,但在应用此模式之前尝试找到根本原因,并且您的 ProducerConfig.RETRIES_CONFIG 属性 似乎在某处被覆盖了。
恐怕 clusterAndWaitTime = waitOnMetadata(record.topic(), record.partition(), maxBlockTimeMs);
不参与重试,它默认迭代到 maxBlockTimeMs = 60000
。您可以通过 ProducerConfig.MAX_BLOCK_MS_CONFIG
属性:
public static final String MAX_BLOCK_MS_CONFIG = "max.block.ms";
private static final String MAX_BLOCK_MS_DOC = "The configuration controls how long <code>KafkaProducer.send()</code> and <code>KafkaProducer.partitionsFor()</code> will block."
+ "These methods can be blocked either because the buffer is full or metadata unavailable."
+ "Blocking in the user-supplied serializers or partitioner will not be counted against this timeout.";
更新
我们可以这样解决问题:
@PostMapping(path = "/v1/{topicName}")
public Mono<ResponseEntity<?>> postData(
@PathVariable("topicName") String topicName, String message) {
return sender.send(Mono.just(SenderRecord.create(new ProducerRecord<>(topicName, null, message), message)))
.flatMap(result -> {
if (result.exception() != null) {
sender.close();
return Flux.just(ResponseEntity.badRequest()
.body(result.exception().getMessage()));
}
return Flux.just(ResponseEntity.ok().build());
})
.next();
}
注意sender.close();
中的错误。
我认为是时候针对 Reactor Kafka 项目提出问题,以允许在错误时关闭生产者。