【发布时间】:2018-06-10 07:23:47
【问题描述】:
通过关注tutorial,我能够创建一个简单的生产者-消费者示例。在我的示例中,只有 1 个主题,而我正在听那个主题。因此,ReceiverConfig 中的代码是有意义的。特别是围绕GROUP_ID_CONFIG 的点,即我创建主题topic_name,然后在此配置中对其进行配置。现在我的问题是,如果我有超过 1 个主题怎么办。假设我有topic_1、topic_2 等等?我应该为每个单独的主题创建ReceiverConfig 吗?
@EnableKafka
@Configuration
public class ReceiverConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(GROUP_ID_CONFIG, "topic_name");
props.put(AUTO_OFFSET_RESET_CONFIG, "earliest");
return props;
}
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
}
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
【问题讨论】: