[英]How to ensure messages reach kafka broker?
I have have a message producer on my local machine and a broker on remote host (aws). 我的本地计算机上有一个消息生成器,远程主机(aws)上有一个代理。
After sending a message from the producer, I wait and call the console consumer on the remote host and see excessive logs. 从生产者发送消息后,我等待并调用远程主机上的控制台使用者,并看到过多的日志。 Without the value from producer.
没有生产者的价值。
The producer flushes the data after calling the send method. 生产者在调用send方法后刷新数据。 Everything is configured correctly.
一切都配置正确。
How can I check to see that the broker received the message from the producer and to see if the producer received the answer? 如何检查代理是否收到了生产者的消息以及生产者是否收到了答复?
The Send
method asynchronously sends the message to the topic and returns a Future
of RecordMetadata
. Send
方法将消息异步发送到主题,并返回Future
of RecordMetadata
。
java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record)
Asynchronously sends a record to a topic
异步发送记录到主题
After the flush
call, check to see that the Future has completed by calling the isDone
method. flush
调用后,通过调用isDone
方法来检查Future是否已完成。 (for example, Future.isDone() == true
) (例如,
Future.isDone() == true
)
Invoking this method makes all buffered records immediately available to send (even if linger.ms is greater than 0) and blocks on the completion of the requests associated with these records.
调用此方法将使所有缓冲的记录立即可用于发送(即使linger.ms大于0),并在与这些记录关联的请求完成时阻塞。 The post-condition of flush() is that any previously sent record will have completed (eg Future.isDone() == true).
flush()的后置条件是任何先前发送的记录都将完成(例如Future.isDone()== true)。 A request is considered completed when it is successfully acknowledged according to the acks configuration you have specified or else it results in an error.
根据您指定的acks配置成功确认请求后,该请求即被视为完成,否则会导致错误。
The RecordMetadata
contains the offset
and the partition
RecordMetadata
包含offset
和partition
public int partition() public int partition()
The partition the record was sent to
记录发送到的分区
public long offset() 公共long offset()
the offset of the record, or -1 if {hasOffset()} returns false.
记录的偏移量;如果{hasOffset()}返回false,则返回-1。
Or you can also use Callback function to ensure messages was sent to topic or not 或者,您也可以使用回调功能来确保消息是否发送到主题
Fully non-blocking usage can make use of the Callback parameter to provide a callback that will be invoked when the request is complete.
完全无阻塞的用法可以利用Callback参数来提供一个回调,该回调将在请求完成时被调用。
here is clear example in docs 这是文档中的清晰示例
ProducerRecord<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("the-topic", key, value);
producer.send(myRecord,
new Callback() {
public void onCompletion(RecordMetadata metadata, Exception e) {
if(e != null) {
e.printStackTrace();
} else {
System.out.println("The offset of the record we just sent is: " + metadata.offset());
}
}
});
You can try get() API of send , which will return the Future of RecordMetadata 您可以尝试send的get()API,它将返回RecordMetadata的Future。
ProducerRecord<String, String> record =
new ProducerRecord<>("SampleTopic", "SampleKey", "SampleValue");
try {
producer.send(record).get();
} catch (Exception e) {
e.printStackTrace();
}
使用“一次发送”,您无需担心消息是否到达: https : //www.baeldung.com/kafka-exactly-once,https : //www.confluent.io/blog/一旦语义完全可能,这就是Apache Kafka的处理方式/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.