有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

KafkaTemplate和KafkaProducer发送方法之间的java差异?

我的问题是,在使用kafka的spring boot微服务中,什么是适合使用KafkaTemplate的。send()或KafkaProducer。发送()

我使用了卡夫卡康苏默而不是卡夫卡利斯纳来调查记录,因为卡夫卡利斯纳正在获取记录,当他们谈到主题时,我希望根据业务需求定期调查记录

已经阅读了《卡夫卡制作人》的文档

和春天的卡夫卡模板

https://docs.spring.io/spring-kafka/reference/html/#kafka-template

我无法做出一个决定,比如什么是理想的使用,或者至少使用其中一个的原因不清楚

我需要的是我希望操作是同步的,也就是说,我想知道发布是否成功,因为如果没有发送记录,我需要重试发布

任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    对于第一个问题,我应该使用卡夫卡模板还是卡夫卡制作人

    The Kafka Producer is defined in Apache Kafka. The KafkaTemplate is Spring's implementation of it (although it does not implement Producer directly) and so it provides more methods for you to use.

    阅读以下链接:

    What is the difference between Kafka Template and kafka producer?

    用于重试机制,以防发布失败。 我在另一个问题中回答了这个问题

    The acks parameter control how many partition replicas must receive the record before the producer can consider the write successful.

    There are 3 values for the acks parameter:

    acks=0, the producer will not wait for a reply from the broker before assuming the message sent successfully.

    acks=1, the producer will receive a successful response from the broker the moment the leader replica received the message. If the message can't be written to the leader, the producer will receive an error response and can retry.

    acks=all, the producer will receive a successful response from the broker once all in-sync replicas received the message.

    Best way to configure retries in Kaka Producer