簡體   English   中英

Spring 雲卡夫卡消費者屬性不工作

[英]Spring Cloud Kafka Consumer Properties not working

我有以下 application.yml 文件:

spring:
  application:
    name: test-app
  cloud:
    stream:
      kafka:
        binder:
          consumerProperties:
            value:
              deserializer: org.apache.kafka.common.serialization.StringDeserializer
          brokers: localhost:9092
          autoCreateTopics: true
          replicationFactor: 1
      bindings:
        listenCloudEvent-in-0:
          destination: com.test.cloudevent
          group: test-app-group
          consumer:
            configuration:
              value:
                deserializer: io.cloudevents.kafka.CloudEventDeserializer
        listenString-in-0:
          destination: com.test.string
          group:  test-app-group
    function:
      definition: listenCloudEvent;listenString

據我所知,以下屬性應該覆蓋默認的反序列化器,但它似乎不起作用。

  consumer:
        configuration:
          value:
            deserializer: io.cloudevents.kafka.CloudEventDeserializer

對於com.test.couldevent主題,我收到以下錯誤。

org.springframework.messaging.converter.MessageConversionException:無法讀取 JSON:無法構造io.cloudevents.CloudEvent的實例(不存在 Creator,如默認構造函數):抽象類型需要映射到具體類型,具有自定義反序列化器,或包含額外的類型信息

如果我將默認反序列化器更改為事件雲反序列化器,那么它可以工作,但其他偵聽器會停止。

這是我的聽眾 function:

@Bean
public Consumer<Flux<Message<CloudEvent>>> listenCloudEvent() {
    return inboundMessage -> inboundMessage
            .onErrorStop()
            .doOnNext(message -> log.info("[{}] Message is received.", message.getPayload().getId()))
            .subscribe();
}

如果您能幫助我解決這個問題,我將不勝感激。 我的主要目標是針對不同的主題使用不同的反序列化器。 但是,盡管在 spring 文檔中指示以這種方式覆蓋默認反序列化器,但我無法成功。

先感謝您!

你錯過了kafka. 消費者屬性中的元素。 configuration是特定於 kafka 的屬性,您正在設置一個通用的消費者屬性。

參見https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream-binder-kafka.html#kafka-consumer-properties

以下屬性僅適用於 Kafka 消費者,並且必須以spring.cloud.stream.kafka.bindings.<channelName>.consumer..為前綴

注意...stream.kafka.bindings... ,而不是...stream.bindings...

暫無
暫無

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

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