Kafka Producer重试参数retries设置取舍【实战笔记】

目录
1.retries参数说明
2.retries使用建议
3.retries后续发展
1.retries参数说明

参数的设置通常是一种取舍,看下retries参数在版本0.11.3说明:

Setting a value greater than zero will cause the client to resend
any record whose send fails with a potentially transient error.
Note that this retry is no different than if the client resent the
record upon receiving the error.
Allowing retries without setting max.in.flight.requests.per.connection to 1 will potentially change 
the ordering of records because if two batches are sent to a single
partition, and the first fails and is retried but the second succeeds,
then the records in the second batch may appear first.

备注:当发送失败时客户端会进行重试,重试的次数由retries指定,此参数默认设置为0。即:快速失败模式,当发送失败时由客户端来处理后续是否要进行继续发送。如果设置retries大于0而没有设置max.in.flight.requests.per.connection=1则意味着放弃发送消息的顺序性。

2.retries使用建议

使用retries的默认值交给使用方自己去控制,结果往往是不处理。所以通用设置建议设置如下:

retries = Integer.MAX_VALUE
max.in.flight.requests.per.connection = 1

备注:这样设置后,发送客户端会一直进行重试直到broker返回ack;同时只有一个连接向broker发送数据保证了数据的顺序性。在Leader选举、集群中一个broker挂掉时,发送端会一直重试直到Leader选举结束。避免由于客户端对异常未处理造成的数据丢失,例如:遇到类似“This server is not the leader for that topic-partition”会自动恢复。

3.retries后续发展

该参数的设置已经在kafka 2.4版本中默认设置为Integer.MAX_VALUE;同时增加了delivery.timeout.ms的参数设置。

The default value for the producer's retries config was changed to
Integer.MAX_VALUE, as we introduced delivery.timeout.ms in KIP-91,
which sets an upper bound on the total time between sending a
record and receiving acknowledgement from the broker.
By default, the delivery timeout is set to 2 minutes.

KIP-91: https://cwiki.apache.org/confluence/display/KAFKA/KIP-91+Provide+Intuitive+User+Timeouts+in+The+Producer
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Kafka Producer 配置参数主要包括以下几个: 1. bootstrap.servers:Kafka 集群中 Broker 的地址清单,Producer 会根据这个地址清单来连接 Kafka 集群。 2. acks:Producer 发送消息后等待 Broker 返回确认信息的方式,可选值为0、1、all。0表示 Producer 不需要等待 Broker 的确认,1表示 Producer 只需要等待 Leader Broker 的确认,all表示 Producer 需要等待所有副本 Broker 都确认后才认为消息发送成功。 3. retries:Producer 在发送消息失败时的重试次数,0表示不重试。 4. batch.size:每个 batch 中消息的数量,Producer 会将多个消息打包成一个 batch 进行批量发送。 5. linger.ms:Producer 在发送消息前等待的时间,如果在等待时间内消息数量未达到 batch.size,则会立即发送消息。如果在等待时间内消息数量已经达到 batch.size,则会立即发送消息。 6. buffer.memory:Producer 允许使用的缓存大小,用于存储还未发送到 Broker 的消息,当缓存中的消息达到一定数量时,Producer 会将其发送到 Broker。 7. key.serializer 和 value.serializer:Producer 发送消息时使用的 key 和 value 的序列化方式。 8. max.in.flight.requests.per.connection:Producer 允许在未收到 Broker 确认信息前发送的最大请求数量。 9. compression.type:Producer 发送消息时使用的压缩方式,可选值为none、gzip、snappy。 以上是 Kafka Producer 常用的配置参数,还有其他一些高级参数可以根据具体的业务需求进行设置

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值