如何解决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'
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");
};
}
}
解决方法
解决方案是在自定义 kafkaTemplate 中添加一个监听器:
@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());
DefaultKafkaProducerFactory<String,String> producerFactory = new DefaultKafkaProducerFactory<>(configProps);
producerFactory.addListener(new MicrometerProducerListener<>(meterRegistry));
}
@Bean
public KafkaTemplate<String,String> customProducer() {
return new KafkaTemplate<>(customProducerFactory());
}
,
我刚刚用 Boot 2.4.2 (spring-kafka 2.6.5) 试了一下,没有问题:
@SpringBootApplication
public class So65791799Application {
public static void main(String[] args) {
SpringApplication.run(So65791799Application.class,args);
}
@KafkaListener(id = "so65791799",topics = "so65791799")
public void listen(String in) {
System.out.println(in);
}
@Bean
public NewTopic topic() {
return TopicBuilder.name("so65791799").partitions(1).replicas(1).build();
}
@Bean
public ApplicationRunner runner(KafkaTemplate<String,String> template) {
return args -> {
template.send("so65791799","foo");
};
}
}
server:
port: 9091
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
probes:
enabled: true
spring:
jmx:
enabled: true
kafka:
consumer:
auto-offset-reset: earliest
http://localhost:9091/actuator/prometheus
...
# HELP kafka_consumer_fetch_manager_records_per_request_avg The average number of records in each request
# TYPE kafka_consumer_fetch_manager_records_per_request_avg gauge
kafka_consumer_fetch_manager_records_per_request_avg{client_id="consumer-so65791799-1",kafka_version="2.6.0",spring_id="kafkaConsumerFactory.consumer-so65791799-1",} 0.5
...
我将其退回到 Boot 2.3.5,但它仍然适用于我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。