spring boot 由2.6升级2.7 kafka报错问题汇总

本文解决Kafka中出现的事务执行错误、集群授权失败及idempotent producer配置问题,提供具体的Spring Kafka配置示例。

1. Cannot execute transactional method because we are in an error state Caused by: org.apache.kafka.common.errors.ClusterAuthorizationException: Cluster authorization failed.

原因

sercurity.protocol 不全局化

解决方案
spring:
  kafka:
    bootstrap-servers: 
    properties:
      security.protocol: SASL_PLAINTEXT
      sasl.mechanism: SCRAM-SHA-256
      sasl.jaas.config: org.apache.kafka.common.security.scram.ScramLoginModule required username="" password="";

改动为 ->

spring:
  kafka:
    bootstrap-servers: 
    producer:
      acks: 1
      security:
        protocol: SASL_PLAINTEXT
    properties:
      sasl.mechanism: SCRAM-SHA-256
      sasl.jaas.config: org.apache.kafka.common.security.scram.ScramLoginModule required username="" password="";

2. No transaction is in process; possible solutions: run the template operation within the scope of a template.executeInTransaction() operation, start a transaction with @Transactional before invoking the template method, run in a transaction started by a listener container when consuming a record

解决方案
在方法上加@Transactional注解 
配置生产者 transaction-id-prefix:

3. Must set acks to all in order to use the idempotent producer. Otherwise we cannot guarantee idempotence.

解决方案
应答级别:多少个分区副本备份完成时向生产者发送ack确认(可选0、1、all/-1)
spring.kafka.producer.acks=-1

Spring Boot 2.6Kafka 的集成非常简单,只需要在项目中添加 Kafka 相关依赖,然后配置 Kafka 的连接信息即可。 以下是 Spring Boot 2.6 整合 Kafka 的步骤: 1. 添加 Kafka 依赖 在 pom.xml 文件中添加 Kafka 相关依赖: ```xml <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.7.3</version> </dependency> ``` 2. 配置 Kafka 连接信息 在 application.properties 或 application.yml 文件中添加 Kafka 连接信息: ```yaml spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.consumer.group-id=my-group spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer ``` 其中,`bootstrap-servers` 是 Kafka 服务器地址;`consumer.group-id` 是消费者组 ID;`producer.key-serializer` 和 `producer.value-serializer` 是序列化器。 3. 创建 Kafka 生产者 在代码中创建 Kafka 生产者: ```java import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @Component public class KafkaProducer { private final KafkaTemplate<String, String> kafkaTemplate; public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate) { this.kafkaTemplate = kafkaTemplate; } public void sendMessage(String topic, String message) { kafkaTemplate.send(topic, message); } } ``` 在构造函数中注入 `KafkaTemplate`,然后在 `sendMessage` 方法中发送消息。 4. 创建 Kafka 消费者 在代码中创建 Kafka 消费者: ```java import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class KafkaConsumer { @KafkaListener(topics = "my-topic", groupId = "my-group") public void handleMessage(String message) { System.out.println("Received message: " + message); } } ``` 使用 `@KafkaListener` 注解监听主题,并在方法中处理消息。 至此,Spring Boot 2.6 整合 Kafka 的步骤就完成了。你可以通过调用 KafkaProducer 的 sendMessage 方法向 Kafka 发送消息,也可以通过监听 Kafka 主题来消费消息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值