假设Kafka主题(my-topic-group)有8个分区,而我的侦听器组(my-topic-group)包含8到10个在不同计算机上运行的不同进程。特定的分区(my-topic-2)未被任何侦听器占用。
以下是Kafa Producer(Jar-1)的代码
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
KafkaProducer producer ;
.....
producer.send(new ProducerRecord('my-topic', student_id % 8, null, payload));
以下是春季(Jar-2)中Kafa监听器配置的代码
@EnableKafka
@Configuration
public class SpringBootKafka {
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
String servers ;
.....
props.put("bootstrap.servers", servers);
props.put("group.id", "my-topic-group");
props.put("key.deserializer", StringDeserializer.class);
props.put("value.deserializer", StringDeserializer.class);
props.put("auto.offset.reset", "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;
}
}
Kafka监听器(Jar-2)
@Component
public class EventsReceiver {
@KafkaListener(topics = "my-topic")
public void receive(ConsumerRecord<String, String> consumerRecord) {
String message = consumerRecord.value();
}
}
最初,Jar-2部署在1台计算机中(泊坞窗),然后慢慢地,我们将Pod的数量增加到10个。没有一个Pod监听 my-topic-partition-2 。而且不止一个人正在听 my-topic-partition-7 。因此,我在监听器中缺少一些kafka事件。
答案 0 :(得分:0)
查看日志;出现实例时,您应该会看到类似以下内容:
Instance 1
partitions assigned: [so51673658-2, so51673658-1, so51673658-4, so51673658-3, so51673658-6, so51673658-5, so51673658-8, so51673658-7, so51673658-9, so51673658-0]
partitions revoked: [so51673658-2, so51673658-1, so51673658-4, so51673658-3, so51673658-6, so51673658-5, so51673658-8, so51673658-7, so51673658-9, so51673658-0]
partitions assigned: [so51673658-2, so51673658-1, so51673658-4, so51673658-3, so51673658-0]
partitions revoked: [so51673658-2, so51673658-1, so51673658-4, so51673658-3, so51673658-0]
partitions assigned: [so51673658-4, so51673658-6, so51673658-5]
Instance 2
partitions assigned: [so51673658-6, so51673658-5, so51673658-8, so51673658-7, so51673658-9]
partitions revoked: [so51673658-6, so51673658-5, so51673658-8, so51673658-7, so51673658-9]
partitions assigned: [so51673658-8, so51673658-7, so51673658-9]
Instance 3
partitions assigned: [so51673658-2, so51673658-1, so51673658-3, so51673658-0]
答案 1 :(得分:0)
我发现了问题,它在2个不同的数据中心中运行,并且都指向同一个Kafka集群。分区2被分配给第二个数据中心。