簡體   English   中英

Spring Cloud Stream Kafka Streams Binder 3.x: No output to the second output topic in case of multiple output bindings

[英]Spring Cloud Stream Kafka Streams Binder 3.x: No output to the second output topic in case of multiple output bindings

我有以下處理器 bean 方法簽名:

@Bean
public BiFunction<KStream<String, MyClass>, KStream<String, String>, KStream<String, MyClass>[]> myStream() {
    return (inputStream1, intputStream2) -> {

        intputStream2
            .peek((k, v) -> {
                log.debug(...);
            });

        return inputStream1
            .mapValues(...)
            .branch((k,v) -> true, (k,v) -> true);

    };
}

相關屬性:

spring.cloud.stream.function.definition: ...;myStream
spring.cloud.stream.bindings:
  myStream-in-0:
    destination: inp0
  myStream-in-1:
    destination: inp1
  myStream-out-0:
    destination: out0
  myStream-out-1:
    destination: out1

Spring Cloud Kafka Stream 版本 Hoxton.SR4(spring-cloud-stream-binder-kafka-streams:jar.Kafka 版本 2.)

我正在使用嵌入式 Kafka 測試我的拓撲:

@RunWith(SpringRunner.class)
@SpringBootTest(
    properties = "spring.cloud.stream.kafka.binder.brokers=${spring.embedded.kafka.brokers}"
)
@EmbeddedKafka(partitions = 1,
        topics = {
            "inp0", "inp1", "out0", "out1"
        },
        brokerPropertiesLocation = "kafka.properties"
)
@Slf4j
public class MyApplicationTests {

    @Test
    public void embeddedKafkaTest() throws IOException, InterruptedException {
        Consumer<String, MyClass> out0Consumer = createConsumer("out0ConsumerGroup");
        Consumer<String, MyClass> out1Consumer = createConsumer("out1ConsumerGroup");

        this.embeddedKafka.consumeFromAnEmbeddedTopic(out0Consumer, "out0");
        this.embeddedKafka.consumeFromAnEmbeddedTopic(out1Consumer, "out1");

        latch = new CountDownLatch(1);
        // ... publish ...
        latch.await(15, TimeUnit.SECONDS);

        ConsumerRecords<String, MyClass> out0 = KafkaTestUtils.getRecords(out0Consumer);
        assertThat(out0.count(), is(greaterThanOrEqualTo(1)));

        ConsumerRecords<String, MyClass> out1 = KafkaTestUtils.getRecords(out1Consumer);
        assertThat(out1.count(), is(greaterThanOrEqualTo(1)));

    }

private <K,V> Consumer<K, V> createConsumer(String groupName) {
    Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(groupName, "true", this.embeddedKafka);
    consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    return new DefaultKafkaConsumerFactory<K, V>(consumerProps).createConsumer();
}

我的測試表明來自myStream的消息按預期到達並落在主題“out0”中,但“out1”主題仍然為空,並且單元測試在第二個斷言上失敗。

I've tried a couple of things, but it looks like the output to the second output topic is simply not being produced (the output to the first output topic is produced well).

你能看出我的設置有什么錯誤嗎?

還有一件事:myStream bean 方法定義中的 return 語句顯示了編譯器警告:

未選中 generics 為可變參數參數創建數組

但看起來這就是 Spring Cloud Kafka Stream 3.x API 需要定義返回類型的方式?

您將兩個謂詞傳遞給branch方法,並且它們都始終評估為true 第一個謂詞總是獲勝並產生數據到第一個 output 綁定。 分支方法調用在第一個謂詞評估為真后退出。 有關更多詳細信息,請參閱javadoc 您應該使用不同的謂詞(可能檢查鍵/值的某些條件)。 如果第一個謂詞失敗而第二個謂詞成功,那么您將看到生成到第二個 output 主題的數據。

關於該編譯器警告,我認為您可以放心地忽略它,因為 API 將確保傳遞到branch調用的謂詞對象具有正確的類型。 由於該方法的實現使用通用可變參數,因此您會得到該異常。 有關該編譯器警告的詳細信息,請參閱線程。

暫無
暫無

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

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