我有3个主题,每个主题都有50个分区。我想使用@KafkaListener为不同的分区指定侦听器
每个主题都应该有一个听众吗?
我应该为一个主题设置多个侦听器,如果可以的话,如何为该主题指定分区范围?
另外,在这3个主题中,有2个主题的数据要比第3个主题多得多,所以我应该为此主题吸引更多的听众吗?
我应该如何选择并发号码?
到目前为止,我的代码:
@Configuration
@EnableKafka
public class ConsumerConfig {
// Factory to create the consumer classes
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<String, String>(consumerConfigs());
}
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> propsMap = new HashMap<>();
propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return propsMap;
}
//
@Bean
KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(3);
factory.getContainerProperties().setPollTimeout(3000);
return factory;
}
@Bean
public Listener listener() {
return new Listener();
}
}
答案 0 :(得分:1)
您还可以为POJO侦听器配置明确的主题和分区(以及可选的初始偏移量):
@KafkaListener(id = "bar", topicPartitions =
{ @TopicPartition(topic = "topic1", partitions = { "0", "1" }),
@TopicPartition(topic = "topic2", partitions = "0",
partitionOffsets = @PartitionOffset(partition = "1", initialOffset = "100"))
})
public void listen(ConsumerRecord<?, ?> record) {
...
}
一个侦听器可以侦听多个主题,但是如果您的消息量不同,我建议为每个主题分配一个单独的侦听器;否则,小批量主题可能无法获得所需的活动。
编辑
您可以使用SpEL表达式生成分区数组。
例如;两个侦听器,一个配置为奇数分区,另一个配置为偶数……
@SpringBootApplication
public class So53588657Application {
public static void main(String[] args) {
SpringApplication.run(So53588657Application.class, args);
}
@Bean
public NewTopic topic() {
return new NewTopic("so53588657", 50, (short) 1);
}
@KafkaListener(id = "odd", topicPartitions =
@TopicPartition(topic = "so53588657",
partitions = "#{T(com.example.So53588657Application$SplitParts).odds(50)}"))
public void oddParts(String in) {
// ...
}
@KafkaListener(id = "even", topicPartitions =
@TopicPartition(topic = "so53588657",
partitions = "#{T(com.example.So53588657Application$SplitParts).evens(50)}"))
public void evenParts(String in) {
// ...
}
@Bean
public ApplicationRunner runner(KafkaListenerEndpointRegistry registry) {
return args -> registry.getListenerContainers()
.forEach(c -> c.getAssignedPartitions().forEach(ap -> System.out.println(ap)));
}
public static class SplitParts {
public static String[] odds(int partitions) {
return split(partitions, i -> i % 2 == 0);
}
public static String[] evens(int partitions) {
return split(partitions, i -> i % 2 == 1);
}
private static String[] split(int partitions, IntPredicate predicate) {
return IntStream.range(0, partitions)
.filter(predicate)
.mapToObj(i -> String.valueOf(i))
.collect(Collectors.toList())
.toArray(new String[0]);
}
}
}
或者您可以在属性中以逗号分隔列表的形式提供它们并使用
partitions = { "#{'${partition.list}'.split(',')}" })