簡體   English   中英

DeadLetterPublishingRecoverer 中的 Kafka 序列化程序與 Spring 消息轉換器

[英]Kafka Serializers vs Spring Message Converters in DeadLetterPublishingRecoverer

我對 spring-kafka 中的序列化程序和消息轉換器有點困惑。 什么時候應該使用 Spring 消息轉換器,什么時候只使用 Kafka 序列化程序? 就我在 Spring 中看到的而言,首選方法是使用StringSerializer配置 Kafka 客戶端鍵/值序列化器,然后在KafkaTemplate上配置一個消息轉換器,用於實際的 POJO 到字符串轉換。 那是對的嗎?

我一直在嘗試配置一個DeadLetterPublishingRecoverer ,它應該向 DLT 發送消息以處理反序列化錯誤以及處理反序列化消息時的任何錯誤。 問題是當消息已經反序列JsonSerializer時我需要一個JsonSerializer但當消息無法反序列化時只需要一個簡單的StringSerializer 任何想法如何配置?

模板中的消息轉換僅適用於采用Message<?>send()方法。

使用采用模板映射的 DLPR 構造函數之一:

    /**
     * Create an instance with the provided templates and a default destination resolving
     * function that returns a TopicPartition based on the original topic (appended with
     * ".DLT") from the failed record, and the same partition as the failed record.
     * Therefore the dead-letter topic must have at least as many partitions as the
     * original topic. The templates map keys are classes and the value the corresponding
     * template to use for objects (producer record values) of that type. A
     * {@link java.util.LinkedHashMap} is recommended when there is more than one
     * template, to ensure the map is traversed in order. To send records with a null
     * value, add a template with the {@link Void} class as a key; otherwise the first
     * template from the map values iterator will be used.
     * @param templates the {@link KafkaOperations}s to use for publishing.
     */
    public DeadLetterPublishingRecoverer(Map<Class<?>, KafkaOperations<? extends Object, ? extends Object>> templates) {
        this(templates, DEFAULT_DESTINATION_RESOLVER);
    }

為反序列化異常添加一個為byte[]值配置的模板(在其生產者上有一個ByteArraySerializer )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM