1、kafka消费者@KafkaListener增加errorHandler属性记录consumer错误,增加properties解决数据获取子系统的消费者发来json数据无法解序列化;

2、配置文件配置consumer反序列化错误处理
This commit is contained in:
Hao Miao
2024-01-30 23:54:02 +08:00
parent 85410b9e53
commit b89fd6c3d0
4 changed files with 56 additions and 16 deletions

View File

@@ -0,0 +1,33 @@
package com.realtime.protection.configuration.kafka;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.springframework.kafka.listener.KafkaListenerErrorHandler;
import org.springframework.kafka.listener.ListenerExecutionFailedException;
import org.springframework.lang.NonNull;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MyKafkaListenerErrorHandler implements KafkaListenerErrorHandler {
@Override
@NonNull
public Object handleError(@NonNull Message<?> message, @NonNull ListenerExecutionFailedException exception) {
return new Object();
}
@Override
@NonNull
public Object handleError(@NonNull Message<?> message, @NonNull ListenerExecutionFailedException exception, Consumer<?, ?> consumer) {
log.error("消息详情:" + message);
log.error("异常信息::" + exception.getCause());
log.error("消费者详情::" + consumer.groupMetadata());
log.error("监听主题::" + consumer.listTopics());
return KafkaListenerErrorHandler.super.handleError(message, exception, consumer);
}
}

View File

@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.*;
public class AlertMessageController
{
private final AlertMessageService alertMessageService;
public AlertMessageController(final AlertMessageService alertMessageService) {
public AlertMessageController(AlertMessageService alertMessageService) {
this.alertMessageService = alertMessageService;
}

View File

@@ -1,6 +1,7 @@
package com.realtime.protection.server.alertmessage.kafkaProducer;
import com.realtime.protection.configuration.entity.alert.AlertMessage;
import com.realtime.protection.server.alertmessage.AlertMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
@@ -9,15 +10,21 @@ import org.springframework.stereotype.Service;
@Slf4j
@Service
public class KafkaConsumerService {
@KafkaListener(topics = "${spring.kafka.consumer.topic-name}")
public void consume(AlertMessage alerm, Acknowledgment ack) {
try {
log.info("消费者监听到数据:{}", alerm);
// 手动提交
ack.acknowledge();
} catch (Exception e) {
throw new RuntimeException("消费失败,数据: " + alerm, e);
}
private final AlertMessageService alertMessageService;
public KafkaConsumerService(AlertMessageService alertMessageService){
this.alertMessageService = alertMessageService;
}
@KafkaListener(id = "${spring.kafka.consumer.group-id}", topics = "${spring.kafka.consumer.topic-name}",
errorHandler = "myKafkaListenerErrorHandler",
properties = {"spring.json.value.default.type=com.realtime.protection.configuration.entity.alert.AlertMessage"})
public void consume(AlertMessage alert, Acknowledgment ack) {
log.info("消费者监听到数据:{}", alert);
alertMessageService.processAlertMessage(alert);
ack.acknowledge();
// 手动提交
}