java - KafkaTemplate 和 KafkaProducer 发送方法的区别?

标签 java spring-boot apache-kafka spring-kafka

我的问题是在使用kafka的Spring Boot微服务中什么适合使用KafkaTemplate.send()或KafkaProducer.send()

我使用 KafkaConsumer 而不是 KafkaListner 来轮询记录,因为 KafkaListner 会在记录进入主题时获取记录,我希望根据业务需求定期轮询记录。

已阅读KafkaProducer的文档https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html

Spring KafkaTemplate

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

我无法做出决定,比如什么是理想的使用方式,或者至少使用其中一种而不是另一种的原因尚不清楚?

我的需要是我希望操作同步,即我想知道发布是否成功,因为如果记录未交付,我需要重试发布。

任何帮助将不胜感激。

最佳答案

对于你的第一个问题,我应该使用kafka模板还是Kafka生产者?

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

关于java - KafkaTemplate 和 KafkaProducer 发送方法的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67191840/

相关文章:

java - Spring boot h2数据库获取驱动程序出错

java - Spring Boot jar 带来 : java. lang.NoSuchMethodError : javax. persistence.Table.indexes( [Ljavax/persistence/Index

java - 嵌入式kafka scalatest ClassNotFoundException:scala.collection.GenTraversableOnce

javascript - Kafka.JS 拒绝连接 <<[BrokerPool] 无法连接到种子代理,正在尝试列表中的另一个代理>>

java - 哪个系列最适合这个

java - 当与 if 语句相同时给出 "Found one too many { characters without a } to match it"

java - 在动态创建的主题上向 Kafka 发送消息会出现错误 LEADER_NOT_AVAILABLE

python 卡夫卡: Is there a way to block a consumer on a kafka topic till a new message is posted?

java - 请求中的 GAE 错误代码 104 破坏了所有并发请求

java - Elasticsearch 中的 SearchPhaseExecutionException