如何在春季启动时配置Kafka StreamsBuilderFactoryBean的两个实例 [英] how to configure two instances of Kafka StreamsBuilderFactoryBean in spring boot

查看:782
本文介绍了如何在春季启动时配置Kafka StreamsBuilderFactoryBean的两个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用spring-boot-2.1.3,spring-kafka-2.2.4,我想要具有两个流配置(例如,具有不同的application.id或连接到不同的集群等).因此,我几乎根据文档定义了第一个流配置,然后添加了第二个配置(使用不同的名称)和第二个StreamsBuilderFactoryBean(也使用了不同的名称):

Using spring-boot-2.1.3, spring-kafka-2.2.4, I want to have two streams configurations (e.g. to have different application.id, or connect to different cluster, etc). So I defined the first stream configuration pretty much according to the docs, then added a second one, with a different name, and a second StreamsBuilderFactoryBean (also with a different name):

@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
public KafkaStreamsConfiguration kStreamsConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "myappId1000");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    //...
    return new KafkaStreamsConfiguration(props);
}

@Bean(name = "myKappConfig")
public KafkaStreamsConfiguration myKafkaAppIdConfiguration() {
    Map<String, Object> props = new HashMap<>();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "myappId9999");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    //...
    return new KafkaStreamsConfiguration(props);
}

@Bean(name = "myKappConfigStreamBuilder")
public StreamsBuilderFactoryBean myAppStreamBuilder(
        @Qualifier("myKappConfig") KafkaStreamsConfiguration myKafkaAppIdConfiguration) {
    return new StreamsBuilderFactoryBean(myKafkaAppIdConfiguration);
}

但是,当我尝试运行该应用程序时,我得到了:

However, when I try to run the app, I get:

中的方法kafkaStreamsFactoryBeanConfigurer的参数0 org.springframework.boot.autoconfigure.kafka.KafkaStreamsAnnotationDrivenConfiguration 需要一个bean,但是发现了2个: -& defaultKafkaStreamsBuilder:由类路径资源中的方法"defaultKafkaStreamsBuilder"定义 [org/springframework/kafka/annotation/KafkaStreamsDefaultConfiguration.class] -& myKappConfigStreamBuilder:由类路径资源中的方法"myAppStreamBuilder"定义 [com/teramedica/kafakaex001web/KafkaConfig.class]

Parameter 0 of method kafkaStreamsFactoryBeanConfigurer in org.springframework.boot.autoconfigure.kafka.KafkaStreamsAnnotationDrivenConfiguration required a single bean, but 2 were found: - &defaultKafkaStreamsBuilder: defined by method 'defaultKafkaStreamsBuilder' in class path resource [org/springframework/kafka/annotation/KafkaStreamsDefaultConfiguration.class] - &myKappConfigStreamBuilder: defined by method 'myAppStreamBuilder' in class path resource [com/teramedica/kafakaex001web/KafkaConfig.class]

因为spring-boot自动配置中的代码具有以下功能:

because the code in the spring-boot autoconfigure does:

@Bean
public KafkaStreamsFactoryBeanConfigurer kafkaStreamsFactoryBeanConfigurer(
        StreamsBuilderFactoryBean factoryBean) {
    return new KafkaStreamsFactoryBeanConfigurer(this.properties, factoryBean);
}

完全替换KafkaStreamsAnnotationDrivenConfiguration的简短内容,如何定义多个StreamsBuilderFactoryBean.或者,如何更改给定流的属性?

Short of replacing the KafkaStreamsAnnotationDrivenConfiguration entirely, how do I define more than one StreamsBuilderFactoryBean. Or alternately, how can I change the properties for a given stream?

推荐答案

@Primary标记一个工厂bean.

Mark one factory bean with @Primary.

这篇关于如何在春季启动时配置Kafka StreamsBuilderFactoryBean的两个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆