首页 > 解决方案 > 使用 Reactor Kafka 发送事件似乎很慢

问题描述

我的项目使用 Reactor Kafka 1.2.2.RELEASE 将使用 Avro 序列化的事件发送到我的 Kafka 代理。这很好用,但是发送事件似乎很慢。

我们通过 KafkaSender.send() 将自定义指标插入到 Mono 发送事件的生命周期中,并注意到发送消息大约需要 100 毫秒。

我们是这样做的:

send(event, code, getKafkaHeaders()).transform(withMetric("eventName"));

send 方法只是构建记录并发送它:

 private Mono<Void> send(SpecificRecord value, String code, final List<Header> headers) {

         final var producerRecord = new ProducerRecord<>("myTopic", null, code, value, headers);
         final var record = Mono.just(SenderRecord.create(producerRecord, code));

         return Optional.ofNullable(kafkaSender.send(record)).orElseGet(Flux::empty)
                 .switchMap(this::errorIfAnyException)
                 .doOnNext(res -> log.info("Successfully sent event on topic {}", res.recordMetadata().topic()))
                 .then();
     }

withMetric 转换器将指标链接到发送单声道生命周期:

private Function<Mono<Void>, Mono<Void>> withMetric(final String methodName) {

        return mono -> Mono.justOrEmpty(this.metricProvider)
                .map(provider -> provider.buildMethodExecutionTimeMetric(methodName, "kafka"))
                .flatMap(metric -> mono.doOnSubscribe(subscription -> metric.start())
                        .doOnTerminate(metric::end));
    }

也就是这个自定义指标,平均返回 100 毫秒。

我们将它与我们的 Kafka 生产者指标进行了比较,并注意到这些指标返回并且平均 40 毫秒来传递一条消息(0 毫秒的排队和 40 毫秒的请求延迟)。

我们很难理解增量,想知道它是否来自 Reactor Kafka 方法来发送事件。

有人可以帮忙吗?

更新

这是我的生产者配置示例:

acks = all
batch.size = 16384
buffer.memory = 33554432
client.dns.lookup = default
compression.type = none
connections.max.idle.ms = 540000
delivery.timeout.ms = 120000
enable.idempotence = true
key.serializer = class org.apache.kafka.common.serialization.StringSerializer
linger.ms = 0
max.block.ms = 60000
max.in.flight.requests.per.connection = 5
max.request.size = 1048576
metadata.max.age.ms = 300000
metrics.num.samples = 2
metrics.recording.level = INFO
metrics.sample.window.ms = 30000
partitioner.class = class org.apache.kafka.clients.producer.internals.DefaultPartitioner
receive.buffer.bytes = 32768
reconnect.backoff.max.ms = 1000
reconnect.backoff.ms = 50
request.timeout.ms = 30000
retries = 2147483647
retry.backoff.ms = 100
sasl.login.refresh.buffer.seconds = 300
sasl.login.refresh.min.period.seconds = 60
sasl.login.refresh.window.factor = 0.8
sasl.login.refresh.window.jitter = 0.05
sasl.mechanism = GSSAPI
security.protocol = PLAINTEXT
send.buffer.bytes = 131072
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.endpoint.identification.algorithm = https
ssl.keystore.type = JKS
ssl.protocol = TLS
ssl.trustmanager.algorithm = PKIX
ssl.truststore.type = JKS
transaction.timeout.ms = 60000
value.serializer = class io.confluent.kafka.serializers.KafkaAvroSerializer

还有,maxInFlight 是 256,调度器是单的,我这里没有配置什么特别的。

标签: apache-kafkaspring-webfluxspring-kafkareactor-kafka

解决方案


推荐阅读