Sarama包使用(一)

Kafka的Producer在发送消息时可以设置RequiredAcks参数,决定在收到不同级别的副本确认后才返回响应。NoResponse不需要响应,WaitForLocal等待本地提交,WaitForAll则等待所有同步副本提交。该配置影响消息的可靠性和延迟。在0.8.2.0及以后版本,错误的设置会引发异常,推荐使用`sarama`库中的常量进行设置。
摘要由CSDN通过智能技术生成
type RequiredAcks int16

RequiredAcks is used in Produce Requests to tell the broker how many replica acknowledgements it must see before responding. Any of the constants defined here are valid. On broker versions prior to 0.8.2.0 any other positive int16 is also valid (the broker will wait for that many acknowledgements) but in 0.8.2.0 and later this will raise an exception (it has been replaced by setting the `min.isr` value in the brokers configuration).

在生成请求中,RequiredAcks用来告诉代理在响应前必须看到多少副本确认。这里定义的任何常数都有效。在0.8.2.0之前的代理版本上,任何其他正的int16也是有效的(代理将等待许多确认),但在0.8.2.0和稍后的版本中,这将引发一个异常(它已被设置' min所取代。broker配置中的Isr '值)。

const (
	// NoResponse doesn't send any response, the TCP ACK is all you get.
	NoResponse RequiredAcks = 0
	// WaitForLocal waits for only the local commit to succeed before responding.
	WaitForLocal RequiredAcks = 1
	// WaitForAll waits for all in-sync replicas to commit before responding.
	// The minimum number of in-sync replicas is configured on the broker via
	// the `min.insync.replicas` configuration key.
	WaitForAll RequiredAcks = -1
)
 // NoResponse 不发送任何响应,只得到 TCP ACK        NoResponse RequiredAcks = 0
 // WaitForLocal 仅等待本地提交成功后再响应	    WaitForLocal RequiredAcks = 1
 // WaitForAll 在响应之前等待所有同步副本提交 
 // 通过代理配置同步副本的最小数量
 // min.insync.replicas 副本的配置关键               WaitForAll RequiredAcks = -1
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForLocal
//config.Producer.RequiredAcks = sarama.WaitForAll
//config.Producer.RequiredAcks = sarama.NoResponse

producer设置acks参数,消息同步到master之后返回ack信号,否则抛异常使应用程序感知到并在业务中进行重试发送。这种方式一定程度保证了消息的可靠性,producer等待broker确认信号的时延也不高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值