簡體   English   中英

使用 spring-cloud-stream-binder-kafka 將 GlobalStateStore 綁定到處理器中

[英]Binding GlobalStateStore into Processor with spring-cloud-stream-binder-kafka

初始問題:我有一個問題,如何將我的 GlobalStateStore 綁定到處理器。 我的應用程序有一個 GlobalStateStore 和一個自己的處理器(“GlobalConfigProcessor”)來保持商店最新。 此外,我還有另一個處理器(“MyClassProcessor”),它在我的消費者 Function 中調用。現在我嘗試從 MyClassProcessor 訪問商店,但我收到一個異常消息:拓撲無效:StateStore config_statestore 尚未添加。

當前情況更新:我設置了一個測試存儲庫,以便更好地了解我的情況。 這可以在這里找到: https://github.com/fx42/store-example

正如您在回購協議中看到的那樣,我有兩個消費者,它們都使用不同的主題。 Config-Topic 提供了一個我想寫入 GlobalStateStore 的事件。 這里涉及到StateStoreUpdateConsumer.java和StateStoreProcessor.java。 使用 MyClassEventConsumer.java,我處理另一個輸入主題並希望從 GlobalStateStore 中讀取值。 如本文檔中所提供的,我無法像初始化 StateStoreBean 一樣初始化 GlobalStateStores,而是必須使用 StreamsBuilderFactoryBeanCustomizer Bean 主動添加它。 此代碼目前在 StreamConfig.java 中已被注釋掉。 沒有這段代碼我得到異常

org.springframework.kafka.KafkaException: Could not start stream: ; nested exception is org.apache.kafka.streams.errors.TopologyException: Invalid topology: Topology has no stream threads and no global threads, must subscribe to at least one source topic or global table.

如果代碼正在使用中,我會得到異常:

org.springframework.kafka.KafkaException: Could not start stream: ; nested exception is org.apache.kafka.streams.errors.StreamsException: Unable to initialize state, this can happen if multiple instances of Kafka Streams are running in the same state directory

所以這導致我的決定,我有一個配置問題,所以拓撲被搞砸了。

問題:

  1. 當我直接通過 GlobalStateStore 提供處理器時
streamBuilder.addGlobalStore(storeBuilder, configInputTopic,
                            Consumed.with(Serdes.String(), Serdes.String()), () -> new StateStoreProcessor(statestoreName));

我必須為此處理器提供消費者 Function 還是我什至必須在 function configuration/application.yml 中提及它?

  1. 有沒有辦法addGlobalStore調用中提供 ProcessorSupplier 而只使用功能方式?

  2. 如果兩個定義的函數有兩種不同的拓撲結構,我該如何處理這個 GlobalStateStore?

這是注釋掉的 StreamBuilderFactoryCustomizer Bean,我用它來將 GlobalStateStore 添加到 FactoryBean:

@Bean
StreamsBuilderFactoryBeanCustomizer streamsBuilderFactoryBeanCustomizer(
            StoreBuilder<KeyValueStore<String, String>> storeBuilder) {

        return factoryBean -> {
            try {
                var streamBuilder = factoryBean.getObject();
                streamBuilder.addGlobalStore(storeBuilder, configInputTopic,
                        Consumed.with(Serdes.String(), Serdes.String()), () -> new StateStoreProcessor(statestoreName));

            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    } };
}

我想通了我的問題。 對我來說,這是我使用的@EnableKafkaStreams 注釋。 我認為這就是我有兩個不同的上下文並行運行並且它們發生沖突的原因。 此外,我需要使用StreamsBuilderFactoryBeanConfigurer而不是StreamsBuilderFactoryBeanCustomizer來正確注冊 GlobalStateStore。 這些更改在鏈接的測試庫中完成,現在可以正確啟動應用程序上下文。

暫無
暫無

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

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