【问题标题】:Not able to send kafka message when MessageHistory is enabled with Spring Boot 2使用 Spring Boot 2 启用 MessageHistory 时无法发送 kafka 消息
【发布时间】:2019-08-27 13:43:18
【问题描述】:

我正在将我们的一项服务从 spring boot 1.5 升级到 2,现在我看到一个与 kafka producer 有关的异常,该异常与将 messageHistory 写入标头有关。这是收到的异常:

org.springframework.kafka.listener.ListenerExecutionFailedException: Listener failed; nested exception is java.lang.IllegalArgumentException: Incorrect type specified for header 'history'. Expected [class org.springframework.integration.history.MessageHistory] but actual type is [class org.springframework.kafka.support.DefaultKafkaHeaderMapper$NonTrustedHeaderType]. 

通过深入研究代码,看起来在转换MessageHistory 标头https://github.com/spring-projects/spring-kafka/blob/14157742d7fa51ce8a22dfbdccc2e3c5b43c1c6f/spring-kafka/src/main/java/org/springframework/kafka/support/DefaultKafkaHeaderMapper.java#L253-L275 时在DefaultKafkaHeaderrMapper 中引发了异常,因此任何人都可以帮助我理解:

  1. 应该是MessageHistory之前写入的消息 发送中?

  2. 如果 1) 的答案是肯定的,那么将 MessageHistory 设置为受信任包的正确方法是什么?

  3. 如果 1) 为否,可能是哪些操作不正确以防止将历史记录写入标头?

提前致谢!

【问题讨论】:

    标签: spring-integration spring-kafka


    【解决方案1】:

    您可以将 org.springframework.integration.history 包添加到白名单以恢复以前的行为:DefaultKafkaHeaderMapper.addTrustedPackages():

    /**
     * Add packages to the trusted packages list (default {@code java.util, java.lang}) used
     * when constructing objects from JSON.
     * If any of the supplied packages is {@code "*"}, all packages are trusted.
     * If a class for a non-trusted package is encountered, the header is returned to the
     * application with value of type {@link NonTrustedHeaderType}.
     * @param packagesToTrust the packages to trust.
     */
    public void addTrustedPackages(String... packagesToTrust) {
    

    https://docs.spring.io/spring-kafka/docs/current/reference/html/#headers

    或者您可以完全从映射中排除此类标头。

    另一种添加自定义序列化程序以将 MessageHistory 存储为 List 的方法。

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 1970-01-01
      • 2021-11-09
      • 2021-11-19
      • 1970-01-01
      • 2018-10-05
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多