簡體   English   中英

spring-kafka 指標不可用

[英]spring-kafka metrics not available

我正在使用 spring-kafka 2.8.1 和 spring boot 2.6.7
我能夠獲得 spring_kafka_listener 指標,但不能獲得 kafka_consumer 指標[收到的消息、滯后、偏移量等]。
我正在使用 DefaultKafkaConsumerFactory 創建 kafka 消費者,如下所示:

props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, ...);
props.put(ConsumerConfig.CLIENT_ID_CONFIG, config.getConsumerName());
........
props.put(ConsumerConfig.GROUP_ID_CONFIG, config.getGroupId());
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, config.getKeyDeserializer());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, config.getValueDeserializer());
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, config.getEnableAutoCommit());
return new DefaultKafkaConsumerFactory<>(props);

我確實瀏覽了 spring-kafka 文檔,我相信需要添加 MicrometerConsumerListener。 但我無法讓它發揮作用。
有人可以幫忙嗎? 謝謝!

編輯:

@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> 
  kafkaListenerContainerFactory() {

    ConcurrentKafkaListenerContainerFactory<String, String> factory =
      new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    return factory;
}         

    @KafkaListener(topics = "tempTopic", groupId = "tempGroup")
public void listenGroup(String message) {
    System.out.println("Received Message: " + message);
}

編輯:我試過使用這個配置,但還是沒有。

props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, ...);
props.put(ConsumerConfig.CLIENT_ID_CONFIG, config.getConsumerName());
........
props.put(ConsumerConfig.GROUP_ID_CONFIG, config.getGroupId());
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, config.getKeyDeserializer());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, config.getValueDeserializer());
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, config.getEnableAutoCommit());

ConsumerFactory<Object,Object> cf = new DefaultKafkaConsumerFactory<>(config);
cf.addListener(new MicrometerConsumerListener<>(new SimpleMeterRegistry()));
return cf;

編輯:

private MeterRegistry meterRegistry;

@Inject
public KafkaConfiguration(MeterRegistry meterRegsitry){
this.meterRegistry = meterRegistry;
}
 {   props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, ...);
    props.put(ConsumerConfig.CLIENT_ID_CONFIG, config.getConsumerName());
    ........
    props.put(ConsumerConfig.GROUP_ID_CONFIG, config.getGroupId());
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, config.getKeyDeserializer());
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, config.getValueDeserializer());
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, config.getEnableAutoCommit());
    
ConsumerFactory<Object,Object> cf = new DefaultKafkaConsumerFactory<>(config);
cf.addListener(new MicrometerConsumerListener<>(meterRegistry));
return cf;
}
cf.addListener(new MicrometerConsumerListener<>(new SimpleMeterRegistry()));

您正在將儀表添加到不同的注冊表。

請改用 Boot 的自動配置儀表注冊表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM