春季kafka与春季JPA

发布于 2025-02-09 23:51:43 字数 557 浏览 4 评论 0原文

我有两个微型服务A和B。

一项服务正在向Kafka主题“ A-Topic”发送消息。 B服务正在消耗消息。

在B服务中,Kafka侦听器将执行以下步骤,

             1. Persist the data in the database     (repo.save(entity))
             2. Publish the response message to "B-Topic".   (kafkatemplte.send("B-Topic",message))

我正在使用Spring @Transactional注释在两个服务中的服务级别。

在成功情况下,数据正在持续存在,并且成功消息仅发布给该主题一次

,因为

在失败方案中,由于完整性约束违规问题,数据库保存失败了。在这种情况下,失败消息将连续发布10次。

如果我从服务类中删除交易注释,则在失败方案中仅发布一次消息。

我不明白,交易注释是如何导致向Kafka发布10次消息的消息。

请让我知道您的意见。

提前致谢。

I have two micro services A and B.

A service is sending message to Kafka topic "A-Topic". B service is consuming the message.

In the B service, kafka listener will do the below steps

             1. Persist the data in the database     (repo.save(entity))
             2. Publish the response message to "B-Topic".   (kafkatemplte.send("B-Topic",message))

I am using the Spring @Transactional annotation at the service level in both services.

In the success scenario, data is getting persisted and success message is published to the topic only once

where as

in the Failure scenario, database save was failed due to integrity constraint violation issue. In this case, failure message is published to the Kafka 10 times continuously.

If I remove Transactional annotation from the service class then the message is published only once in failure scenario also.

I don't understand, how come transactional annotation is causing the message to be published 10 times to kafka.

Please let me know your inputs.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2025-02-16 23:51:43

默认错误处理程序将尝试交付10次;您需要在侦听器容器中启用KAFKA交易,以便将KAFKA发送的发送(以及b-Topic需要 code> simelation simalation.level = read_comment)。

https:// https:// https://docsring.io.io/spring.io/spring-kafka /doc/current/reference/html/#交易

The default error handler will attempt delivery 10 times; you need to enable Kafka transactions in the listener container so the kafka sends will be rolled back (and the consumer on B-Topic needs isolation.level=read_committed).

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文