Spring Boot for Kafka – 一个应用中的多个KafkaStreams配置

huangapple go评论81阅读模式
英文:

Spring Boot for Kafka - Multiple KafkaStreams config in one app

问题

在 Spring 中,对于 Kafka Streams,我们可以在一个应用程序中定义两个配置吗?
如果我们有两个 @Bean 用于配置,就像这样,我该如何将 secondConfig 用于流处理?

@Configuration
@EnableKafkaStreams
public class KafkaStreamConfig {

	@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
	public KafkaStreamsConfiguration kafkaStreamConfig() {
		var props = new HashMap<String, Object>();

		props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-stream");
		props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

        ....

		return new KafkaStreamsConfiguration(props);
	}

	@Bean(name = "secondConfig")
	public KafkaStreamsConfiguration secondKafkaStreamConfig() {
		var props = new HashMap<String, Object>();

		props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-stream");
		props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "other-machine:9092");

        ....

		return new KafkaStreamsConfiguration(props);
	}
}

谢谢

英文:

In spring for kafka stream can we define two configurations in one app?
If we have two @Bean for config, like this, how can I use secondConfig into the stream?

@Configuration
@EnableKafkaStreams
public class KafkaStreamConfig {

	@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
	public KafkaStreamsConfiguration kafkaStreamConfig() {
		var props = new HashMap&lt;String, Object&gt;();

		props.put(StreamsConfig.APPLICATION_ID_CONFIG, &quot;kafka-stream&quot;);
		props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, &quot;localhost:9092&quot;);

        ....
 
		return new KafkaStreamsConfiguration(props);
	}

	@Bean(name = &quot;secondConfig&quot;)
	public KafkaStreamsConfiguration kafkaStreamConfig() {
		var props = new HashMap&lt;String, Object&gt;();

		props.put(StreamsConfig.APPLICATION_ID_CONFIG, &quot;kafka-stream&quot;);
		props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, &quot;other-machine:9092&quot;);

        ....
 
		return new KafkaStreamsConfiguration(props);
	}


}

Thanks

答案1

得分: 1

Spring只会创建一个工厂bean(来自默认配置bean)。对于第二个工厂bean,您需要定义一个使用它的StreamsBuilderFactoryBean

英文:

Spring will only create one factory bean (from the default configuration bean). For the second one you will need to define a StreamsBuilderFactoryBean that uses it.

huangapple
  • 本文由 发表于 2020年8月15日 07:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63421294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定