我按照此link中列出的步骤创建了客户反序列化程序。我从Kafka收到的消息有明文和#34;日志消息 - "在json string之前。我希望反序列化器忽略这个字符串并解析json数据。有办法吗?
应用
@SpringBootApplication
public class TransactionauditServiceApplication {
public static void main(String[] args) throws InterruptedException {
new SpringApplicationBuilder(TransactionauditServiceApplication.class).web(false).run(args);
}
@Bean
public MessageListener messageListener() {
return new MessageListener();
}
public static class MessageListener {
@KafkaListener(topics = "ctp_verbose", containerFactory = "kafkaListenerContainerFactory")
public void listen(@Payload ConciseMessage message,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition) {
System.out.println("Received Messasge in group foo: " + message.getStringValue("traceId") + " partion " + partition);
}
}
}
ConsumerConfig
@EnableKafka
@Configuration
public class KafkaConsumerConfig {
@Value(value = "${kafka.bootstrapAddress:localhost:9092}")
private String bootstrapAddress;
@Value(value = "${groupId:audit}")
private String groupId;
@Bean
public ConsumerFactory<String, ConciseMessage> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), new JsonDeserializer<>(ConciseMessage.class));
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, ConciseMessage> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, ConciseMessage> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
答案 0 :(得分:2)
通过编写行new JsonDeserializer<>(ConciseMessage.class)
,您只是告诉Kafka您要将邮件转换为ConciseMessage
类型。所以,这并不能使它成为一个自定义的反序列化器。为了解决您的问题,您很可能必须提出自己的反序列化器实现,该反序列化器具有剥离文本“日志消息 - ”的逻辑。