[英]Kafka consumer is reconsuming messages from topic
我們的Java應用程序中的Kafka使用者出於某種原因重用了該主題中的消息。
生產者將消息發送到具有四個分區的主題。 我們有一個使用者正在消費來自此主題的消息。 該應用程序在工作日(周末除外)的所有時間運行:在周末不會調用輪詢方法。
使用者配置:自動提交,自動提交時間為5秒(默認)。
該應用程序可以正常運行,直到周日再次恢復調用輪詢方法為止。 我們看到從該主題輪詢了數百萬條消息。 消費者基本上是在輪詢來自該主題的所有消息。 將新偏移量與周末停止之前的偏移量進行比較時。 新的偏移量要小得多,就像對所有四個分區重置為非常小的數字一樣。
我們不知道用戶端發生了什么,因為它沒有調用輪詢方法,因此沒有打印日志消息。 我們檢查了kafka服務器日志,但未找到任何內容。
有人看過嗎? 消費者的配置是否會錯誤?
<spring.kafka.version>1.1.2.RELEASE</spring.kafka.version>
...
<bean id="defaultKafkaConsumer"
class="org.apache.kafka.clients.consumer.KafkaConsumer">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="${kafka.bootstrap.servers}"></entry>
<entry key="max.block.ms" value="5000"></entry>
<entry key="group.id" value="kafkaconnect.tca"></entry>
<entry key="auto.offset.reset" value="earliest"></entry>
<entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
<entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
</map>
</constructor-arg>
</bean>
getKafkaConsumer().subscribe(Arrays.asList(getKafkaTopic()));
// set up the polling task
handler = timer.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
processPoll();
} catch (Throwable t) {
LOG.error(String.format("error processing poll for inet: %s, details: %s - %s", getId(), t.getMessage(), t.getCause()), t);
}
}
}, 3, 3, TimeUnit.MILLISECONDS);
processPoll() Method: destination will not be ready during the weekend.
try {
if (!isDestinationReady()) {
if (destinationIgnoreCnt++ ==0) {
LOG.warn(String.format("outbound destination session is not ready - trying: %s/%s",destinationIgnoreCnt,destinationwaitingloop));
} else if ((destinationIgnoreCnt++ % destinationwaitingloop) == 0) {
LOG.warn(String.format("outbound destination session is not ready - trying %s/%s", destinationIgnoreCnt,destinationwaitingloop));
destinationIgnoreCnt = 1;
}
messageIgnoreCnt = 0;
return;
}
if(!isDestinationOpen()) {
if (destinationIgnoreCnt++ ==0) {
LOG.error(String.format("outbound destination is not opended - trying:%s/%s.", destinationIgnoreCnt,destinationwaitingloop) );
} else if ((destinationIgnoreCnt++ % destinationwaitingloop) == 0) {
LOG.error(String.format("outbound destination is not opended - trying %s/%s.", destinationIgnoreCnt,destinationwaitingloop));
destinationIgnoreCnt = 1;
}
messageIgnoreCnt = 0;
return;
}
if (messageIgnoreCnt++ == 0) {
LOG.info(String.format("kafka poller started. poll interval %s wait: %s", pollingInterval, 60000));
} else if ((messageIgnoreCnt++ % 30) == 0) {// approximately 30mins
LOG.info(String.format("kafka poller started. poll interval %s wait %s", pollingInterval, 60000));
messageIgnoreCnt = 1;
}
if (getKafkaConsumer() == null) {
LOG.critical("KafkaListener consumer is null");
return;
}
ConsumerRecords<String, String> records = getKafkaConsumer().poll(60000);
if (records == null || records.isEmpty()) {
LOG.debug("zero records received from Kafka");
return;
}
for (ConsumerRecord<String, String> record : records) {
LOG.info(String.format("consuming from topic = %s ", record.toString()));
try {
String jsonMsg = record.value();
DirectBatchRequest payload = JacksonUtilities.getObjectMapper().readValue(jsonMsg, DirectBatchRequest.class);
if (payload != null) {
LOG.info(String.format("Got it reportId:%s", payload.getDestinationId()));
if(payload.getDestinationId() == 0) {
LOG.info(String.format("Applying default destination desk:%s", defaultDeskId));
payload.setDestinationId(defaultDeskId);
}
List<RequestEntryType> requestEntryTypeList = ((StreamDirectRequest) payload).getRequestList();
LOG.info(String.format("Processing size: %s" , requestEntryTypeList.size()) );
processRequest((StreamDirectRequest) payload); //async call
LOG.info(String.format("Processing size: %s sent to Steam" , requestEntryTypeList.size()) );
}
} catch (Throwable t) {
LOG.error(String.format("KafkaListener JSON%s conversion error %s", record, t.getMessage()));
}
}
} catch (Throwable t) {
LOG.error(String.format("KafkaListener exception %s", t.getMessage()));
}
卡夫卡在默認情況下刪除后的偏移量offsets.retention.minutes
如果沒有從消費者群體活動。 默認保留期為1440分鍾(1天)。
在您的情況下,由於消費者組在周末休息,因此偏移被重置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.