簡體   English   中英

spring-kafka: RetryTopicConfiguration 自定義名稱用於重試和死信主題

[英]spring-kafka: RetryTopicConfiguration with custom names for retry and dead letter topics

我為重試主題配置提供了一個 bean:

@Bean
public RetryTopicConfiguration kafkaRetryTopicConfig(...) {
  return RetryTopicConfigurationBuilder
      .newInstance()
      .fixedBackOff(...)
      .maxAttempts(..)
      .useSingleTopicForFixedDelays()
      .doNotRetryOnDltFailure()
      .listenerFactory(factory)
      .create(template);
}

我還為重試主題名稱提供者工廠定義了一個 bean:

// Need this because it is not just retry/DLT topic suffix but the entire name is configurable and specified in the configuration.
@Bean
public RetryTopicNamesProviderFactory retryTopicNamingProviderFactory() {
  return new RetryTopicNamesProviderFactory {
    @Override
    public RetryTopicNamesProvider createRetryTopicNamesProvider(DestinationTopic.Properties properties) {
      return new SuffixingRetryTopicNamesProvider(properties) {
        @Override
        public String getTopicName(String topic) {
          // NOTE: The retry and dead letter topic names come from config.
          // Like to create different names for retry and dead letter topics.
          // TODO: But how to distinguish which name I should return here!
          return *******;
        }
      };
    }
  }
}

兩部分問題:

一個。 Spring 似乎與 bean 無關。 它正在處理kafkaRetryTopicConfig bean,但沒有使用retryTopicNamingProviderFactory和自定義主題名稱。 它采用默認命名( .retry.deadLetter )。 是否有任何我應該聲明的 bean 或組件,以便選擇自定義命名提供程序?

灣。 如上面代碼中所示( TODO ),如何區分是createRetryTopicNamesProvider進行重試還是死信主題命名? 看到RetryTopicNamesProviderFactory處理重試和死信主題的命名。

非常感謝幫助。

正如您在文檔中看到的那樣,該框架提供了一個properties object ,您可以檢查它以了解您是在mainretry還是dlt主題上,以及為該主題配置的suffixes

public class CustomRetryTopicNamesProviderFactory implements RetryTopicNamesProviderFactory {

    @Override
    public RetryTopicNamesProvider createRetryTopicNamesProvider(
                DestinationTopic.Properties properties) {

        if(properties.isMainEndpoint()) {
            return new SuffixingRetryTopicNamesProvider(properties);
        }
        else {
            return new SuffixingRetryTopicNamesProvider(properties) {

                @Override
                public String getTopicName(String topic) {
                    return "my-prefix-" + super.getTopicName(topic);
                }

            };
        }
    }

}

您還可以訪問原始topic名稱,以及為您提供后綴主題名稱的super.getTopicName(topic)方法。

您應該能夠使用此信息返回您想要的主題名稱。

如果這對您的用例來說還不夠自定義,請提供有關如何配置主題名稱的更多詳細信息。

編輯:另外,請告知您正在使用的Spring Kafka版本 - 對於2.9.x及更高版本,不再支持RetryTopicNamesProviderFactory bean 配置。

暫無
暫無

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

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