[英]Connect to multiple Kafka Cluster's using spring Kafka
I have a spring boot application that consumes messages from a topic(say topic1) in a Kafka cluster.我有一个 spring 启动应用程序,它使用来自 Kafka 集群中某个主题(比如 topic1)的消息。 This is how my code looks like currently.
这就是我的代码目前的样子。
@Configuration
public class KafkaTopicConfig {
@Value(value = "${kafka.bootstrapAddress}")
private String bootstrapAddress;
@Bean
public KafkaAdmin kafkaAdmin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
return new KafkaAdmin(configs);
}
@Bean
public NewTopic topic1() {
return new NewTopic("baeldung", 1, (short) 1);
}
}
@EnableKafka
@Configuration
public class KafkaConsumerConfig {
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
bootstrapAddress);
props.put(
ConsumerConfig.GROUP_ID_CONFIG,
groupId);
props.put(
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class);
props.put(
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(props);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String>
kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
@KafkaListener(topics = "topicName", groupId = "foo")
public void listen(String message) {
System.out.println("Received Messasge in group foo: " + message);
}
Now I want to start consuming from a different topic in another Kafka cluster.现在我想从另一个 Kafka 集群中的不同主题开始消费。 One way is to create another bean for this.
一种方法是为此创建另一个 bean。 But is there a better way to do this?
但是有没有更好的方法来做到这一点?
You need another ConsumerFactory
and KafkaListenerContainerFactory
that connects to the other cluster's bootstrap servers.您需要另一个连接到其他集群的引导服务器的
ConsumerFactory
和KafkaListenerContainerFactory
。
Then you can use the containerFactory
in the @KafkaListener
annotation.然后你可以在
@KafkaListener
注解中使用containerFactory
。
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> otherClusterFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(otherConsumerFactory());
return factory;
}
...
@KafkaListener(...., containerFactory="otherClusterFactory")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.