Apache kafka 如何处理UnkownProducerdException

Apache kafka 如何处理UnkownProducerdException,apache-kafka,spring-cloud,spring-kafka,Apache Kafka,Spring Cloud,Spring Kafka,我们在Spring Cloud和Kafka方面遇到了一些问题,有时我们的微服务会抛出一个UnknownProducerIDException,这是由于代理端的参数transactional.id.expiration.ms过期所致 我的问题是,是否可以捕获该异常并重试失败的消息?如果是,处理它的最佳选择是什么 我看了一下: - - 我们正在使用SpringCloudHoxton.RELEASEversion和SpringKafka版本2.2.4.RELEASE 我们使用的是AWS Kafka解决

我们在Spring Cloud和Kafka方面遇到了一些问题,有时我们的微服务会抛出一个
UnknownProducerIDException
,这是由于代理端的参数
transactional.id.expiration.ms
过期所致

我的问题是,是否可以捕获该异常并重试失败的消息?如果是,处理它的最佳选择是什么

我看了一下:
-
-

我们正在使用SpringCloud
Hoxton.RELEASE
version和SpringKafka版本
2.2.4.RELEASE

我们使用的是AWS Kafka解决方案,所以我们不能在我前面提到的属性上设置新值

以下是异常的一些痕迹:

2020-04-07 20:54:00.563 ERROR 5188 --- [ad | producer-2] o.a.k.c.p.internals.TransactionManager   : [Producer clientId=producer-2] The broker returned org.apache.kafka.common.errors.UnknownProducerIdException: This exception is raised by the broker if it could not locate the producer metadata associated with the producerId in question. This could happen if, for instance, the producer's records were deleted because their retention time had elapsed. Once the last records of the producerId are removed, the producer's metadata is removed from the broker, and future appends by the producer will return this exception. for topic-partition test.produce.another-2 with producerId 35000, epoch 0, and sequence number 8
2020-04-07 20:54:00.563  INFO 5188 --- [ad | producer-2] o.a.k.c.p.internals.TransactionManager   : [Producer clientId=producer-2] ProducerId set to -1 with epoch -1
2020-04-07 20:54:00.565 ERROR 5188 --- [ad | producer-2] o.s.k.support.LoggingProducerListener    : Exception thrown when sending a message with key='null' and payload='{...}' to topic <some-topic>:

文件application.yml(不要忘记设置环境变量
KAFKA_HOST
):

spring:
云:
流:
卡夫卡:
活页夹:
自动创建主题:true
经纪人:${KAFKA_HOST}
交易:
制作人:
已启用错误通道:true
生产者财产:
阿克斯:全部
retry.backoff.ms:200
玲儿:100
每个连接的最大飞行请求数:1
enable.幂等性:true
重试次数:3次
压缩类型:snappy
request.timeout.ms:5000
key.serializer:org.apache.kafka.common.serialization.StringSerializer
消费者财产:
session.timeout.ms:20000
最大轮询间隔毫秒:350000
enable.auto.commit:true
allow.auto.create.topics:true
auto.commit.interval.ms:12000
最高投票记录:5
isolation.level:读取已提交
配置:
auto.offset.reset:最新版本
绑定:
测试输入:
#contentType:文本/纯文本
目的地:test.product
组:组输入
消费者:
最大尝试次数:3次
startOffset:最新版本
自动提交错误:true
queueBufferingMaxMessages:100000
自动提交设置:true
测试输出:
#contentType:文本/纯文本
目的地:测试。生产。另一个
组:组输出
制作人:
阿克斯:全部
调试:正确
侦听器处理程序:

@springboot应用程序
@EnableBinding(Bindings.class)
公共类应用程序{
私有静态最终记录器log=LoggerFactory.getLogger(PocApplication.class);
公共静态void main(字符串[]args){
run(pocapapplication.class,args);
}
@自动连线
私人BinderAwareChannelResolver BinderAwareChannelResolver;
@StreamListener(Topics.TESTLISTENINPUT)
public void listen(消息输入,字符串头键){
最终消息生成器;
消息通道消息通道;
messageChannel=this.binderAwareChannelResolver.resolveDestination(“测试输出”);
对象有效负载=in.getPayload();
builder=MessageBuilder.withPayload(有效载荷);
试一试{
log.info(“接收到的事件:{}”,in);
如果(!messageChannel.send(builder.build())){
在.getPayload()中,出现log.error(“尝试发送消息时发生了一些事情!{}”);
}
log.info(“提交成功”);
}捕获(未知生成异常){
日志错误(“捕捉到UnknownProducedException”,e);
}捕获(卡夫卡例外){
log.error(“捕捉到KafkaException”,e);
}捕获(例外e){
System.out.println(“提交失败”+e.getMessage());
}
}
}
问候

        } catch (UnknownProducerIdException e) {
            log.error("UnkownProducerIdException catched ", e);
要捕获异常,需要设置
sync
kafka producer属性()。否则,错误将异步返回

你不应该“吃”那里的例外;它必须被抛出回容器,以便容器回滚事务

而且

        }catch (Exception e) {
            System.out.println("Commit failed " + e.getMessage());
        }
提交是在流侦听器返回到容器后由容器执行的,因此您永远不会在这里看到提交错误;同样,必须让异常传播回容器


容器将根据使用者绑定的重试配置重试传递。

能否编辑问题以显示完整堆栈跟踪?当然!我添加了它@GaryRussellI,我在复制它时遇到了困难;你能告诉我你是怎么发送的吗?这就是说,异常将在另一个线程上返回,因此您需要在将来添加一个侦听器,或者调用
get(time,unit)
以获取发送结果。明天我将用一个示例更新它。我正在使用
MessageChannel
接口和绑定。我如何添加您正在交谈的听众?有没有一个例子可以让我使用?谢谢你,所以使用SpringCloudStream时要复杂一点(如果你是这个意思的话)。我等着看你的密码。嗨,加里,非常感谢你的回答。我们之前在
producer
配置中尝试了
sync
选项,但仍然无法捕获异常。当你说“扔回容器”是什么意思?如果你正在使用事务,同步需要在事务生产者配置上进行。当然,但我们这里不使用事务。而且它没有启用事务性,我很困惑;您说过您正在使用事务过期来强制执行
UnkownProducerIdException
。这在2.4.0代理中似乎已修复:。也许可以解释为什么我不能复制它(我的经纪人是2.4.1)。