当前位置: 首页 > 工具软件 > sarama > 使用案例 >

Sarama包使用(一)

夏才
2023-12-01
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确认信号的时延也不高。

 类似资料: