Spring Boot 2.3.4 - Kafka 指标在 /actuator/prometheus 中不可见

问题描述

我有一个 Spring Boot 应用程序(版本 2.3.4),我正在使用 @KafkaListener 来使用记录。我还使用执行器和千分尺(1.5.5 版)作为指标。

问题是我在 /actuator/prometheus 中看不到 Kafka 指标。 我正在使用以下依赖项:

'org.springframework.boot' version '2.3.4.RELEASE'
implementation group: 'org.springframework.kafka',name: 'spring-kafka',version: '2.5.10.RELEASE'
implementation group: 'org.apache.kafka',name: 'kafka-clients',version: '2.5.1'

并将这些属性添加到 application.yaml 中:

management:
  server:
    port: 9091
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      probes:
        enabled: true

spring:
  jmx:
    enabled: true

如果我应该添加任何其他内容以使 kafka 指标在 /actuator/prometheus 中可见

请注意,当我使用默认的 KafkaTemplate 时,指标是可见的,但在尝试创建自定义 KafkaTemplate 时,指标会消失:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public ProducerFactory<String,String> customProducerFactory() {
        Map<String,Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"127.0.0.1:9092");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,Serdes.String().serializer().getClass().getName());
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,Serdes.String().serializer().getClass().getName());
        return new DefaultKafkaProducerFactory<>(configProps);
    }

    @Bean
    public KafkaTemplate<String,String> customProducer() {
        return new KafkaTemplate<>(customProducerFactory());
    }

    @KafkaListener(id = "test",topics = "test_topic")
    public void listen(String in) {
        System.out.println(in);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("test_topic").partitions(1).replicas(1).build();
    }


    @Bean
    public ApplicationRunner runner(KafkaTemplate<String,String> template) {
        return args -> {
            template.send("test_topic","foo");
        };
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)