spring boot 由2.6升级2.7 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 中连接多个 Kafka 集群可以通过创建多个 KafkaTemplate 实例来实现。以下是示例代码: ```java @Configuration public class KafkaConfig { @Value("${kafka.bootstrap.servers1}") private String kafkaBootstrapServers1; @Value("${kafka.bootstrap.servers2}") private String kafkaBootstrapServers2; @Bean public KafkaTemplate<String, String> kafkaTemplate1() { Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapServers1); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(props)); } @Bean public KafkaTemplate<String, String> kafkaTemplate2() { Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapServers2); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(props)); } } ``` 在上面的示例中,我们创建了两个 KafkaTemplate 实例,分别连接两个不同的 Kafka 集群。使用时,只需要注入相应的 KafkaTemplate 实例即可。例如: ```java @Autowired private KafkaTemplate<String, String> kafkaTemplate1; @Autowired private KafkaTemplate<String, String> kafkaTemplate2; public void sendMessageToKafka1(String topic, String message) { kafkaTemplate1.send(topic, message); } public void sendMessageToKafka2(String topic, String message) { kafkaTemplate2.send(topic, message); } ``` 在上面的示例中,我们分别定义了两个方法,用于向不同的 Kafka 集群发送消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值