spring 反序列化 avro 对象时出现 MessageConversionException

spring MessageConversionException when deserialzing an avro object

我正在发送一个 avro 对象 User 并在侦听器中接收它。这是配置

@Bean
public ProducerFactory<String, User> userProducerFactory() {
    Map<String, Object> config = new HashMap<>();

    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, AvroSerializer.class);

    return new DefaultKafkaProducerFactory<>(config);
}

@Bean
public KafkaTemplate<String, User> userKafkaTemplate() {
    return new KafkaTemplate<>(userProducerFactory());
}

@Bean
public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, AvroDeserializer.class);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "avro");

    return props;
}

@Bean
public ConsumerFactory<String, User> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
            new AvroDeserializer<>(User.class));
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, User> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, User> factory
            = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());

    return factory;
}

我正在这样发送用户

@Autowired
KafkaTemplate<String, User> userProducer;
public static void main(String[] args) {
    SpringApplication.run(EvaluatorApplication.class, args);
}

@Override
public void run(String... args) throws Exception {

    User user = User.newBuilder()
                    .setFavoriteColor("red")
                    .setFavoriteNumber(123)
                    .setName("Shantanu")
                    .build();

    userProducer.send("avro.t", user);
}

这是 Kafka 监听器

@KafkaListener(topics = "avro.t", containerFactory = "kafkaListenerContainerFactory", groupId = "transaction-request-upstream-consumer")
public void listenUpstream(User user)
{
    try {
        LOGGER.info("Received {}", user);
        messageProcessor.process(null, null);
        //ack.acknowledge();
    } catch (Exception ex) {
        LOGGER.error("{}", ex);
    }
}

在 AvroDeserializer class 中,消息被反序列化,但我得到的是这个异常。我无法理解它的正面或反面。花了很多时间,完全看不懂。请帮助!!!

[2020-03-26 00:59:16.358] :: [ERROR] :: o.s.k.listener.LoggingErrorHandler.error.254 :: Error while processing: ConsumerRecord(topic = avro.t, partition = 0, leaderEpoch = 0, offset = 4, CreateTime = 1585164556293, serialized key size = -1, serialized value size = 17, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"})
org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public void com.ibm.dip.evaluator.listeners.EventListener.listenUpstream(com.ibm.dip.evaluator.models.User)]
Bean [com.ibm.dip.evaluator.listeners.EventListener@356dc38d]; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}], failedMessage=GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}], failedMessage=GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.decorateException(KafkaMessageListenerContainer.java:1774)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeErrorHandler(KafkaMessageListenerContainer.java:1766)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1679)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:1605)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:1510)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:1257)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:1007)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:927)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}], failedMessage=GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]
    at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:336)
    at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:86)
    at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:51)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage(KafkaMessageListenerContainer.java:1728)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeOnMessage(KafkaMessageListenerContainer.java:1711)
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1666)
    ... 8 common frames omitted
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]
    at org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver.resolveArgument(PayloadMethodArgumentResolver.java:145)
    at org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor$KafkaNullAwarePayloadArgumentResolver.resolveArgument(KafkaListenerAnnotationBeanPostProcessor.java:906)
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117)
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:148)
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:116)
    at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
    at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:326)
    ... 13 common frames omitted

cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User]

通常意味着这是一个 class 加载器问题。

即与侦听器 bean 中的 User 相比,消费者工厂中的 User.class 由不同的 class 加载器加载,因此它们实际上是不同的 classes.

使用 -verbose 尝试 运行 JVM 以查看 class 加载 activity。

这很奇怪。在我尝试过的多种组合中,我了解到 spring 启动版本 2.2.5 + spring-boot dev-tools 依赖性导致了这个问题。无法解释原因。但是当我切换到 spring-boot 2.1.5 并删除开发工具时,一切都开始正常工作了。

我认为这是由开发工具引起的。我有同样的问题

Caused by: java.lang.ClassCastException: com.ffam.paymentservice.loanadvance.Loanadvance cannot be cast to com.ffam.paymentservice.loanadvance.Loanadvance

而且我看到它们来自不同的 class 加载器:一个来自 Laucher$AppClassLoader,另一个来自 RestartClassLoader。删除开发工具后,一切正常。