繁体   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