kafka-clients之监控

以生产者为例介绍spring如何整合kafka-clients,micrometer,prometheus。上报生产者监控打点

可以通过在Spring Boot应用中手动添加Kafka Producer的度量监控,确保Prometheus能够采集到Producer的指标。以下是一个示例代码,其中使用了MeterRegistry注册Kafka Producer的度量指标:

1. 引入依赖

确保在pom.xml中引入Micrometer和Prometheus相关的依赖:

xml 复制代码
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>

2. 配置Kafka和Prometheus监控

在配置类中,手动为Producer添加度量注册。

java 复制代码
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.kafka.KafkaMetrics;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class KafkaProducerConfig {

    @Bean
    public KafkaProducer<String, String> kafkaProducer() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.METRICS_RECORDING_LEVEL_CONFIG, "INFO");  // 启用INFO级别的监控

        return new KafkaProducer<>(configProps);
    }

    @Bean
    public MeterBinder kafkaProducerMetrics(KafkaProducer<String, String> kafkaProducer) {
        return new KafkaMetrics(kafkaProducer);
    }
}

3. 配置Prometheus端点

application.yml中配置Prometheus的端点:

yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    export:
      prometheus:
        enabled: true
  endpoint:
    prometheus:
      enabled: true

4. 配置Prometheus采集

确保Prometheus的配置文件中包含Spring Boot应用的Prometheus端点,通常是http://<application-host>:<port>/actuator/prometheus

5. 使用Producer并触发度量

在应用代码中使用KafkaProducer发送消息,以确保Producer的指标数据生成:

java 复制代码
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KafkaProducerService {

    private final KafkaProducer<String, String> producer;

    @Autowired
    public KafkaProducerService(KafkaProducer<String, String> producer) {
        this.producer = producer;
    }

    public void sendMessage(String topic, String key, String value) {
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);
        producer.send(record);
    }
}

6. 验证

运行应用并访问http://<application-host>:<port>/actuator/prometheus,检查是否包含Producer的指标数据。这样Prometheus应该能够采集到Producer的度量指标。

相关推荐
ZHOU_WUYI7 小时前
一个简单的分布式追踪系统
分布式
码不停蹄的玄黓11 小时前
MySQL分布式ID冲突详解:场景、原因与解决方案
数据库·分布式·mysql·id冲突
王小王-12312 小时前
基于Hadoop的公共自行车数据分布式存储和计算平台的设计与实现
大数据·hive·hadoop·分布式·hadoop公共自行车·共享单车大数据分析·hadoop共享单车
要开心吖ZSH13 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring
幼稚园的山代王14 小时前
RabbitMQ 4.1.1初体验
分布式·rabbitmq·ruby
百锦再14 小时前
RabbitMQ用法的6种核心模式全面解析
分布式·rabbitmq·路由·消息·通道·交换机·代理
一路向北North15 小时前
RabbitMQ简单消息监听和确认
分布式·rabbitmq·ruby
真实的菜15 小时前
Kafka生态整合深度解析:构建现代化数据架构的核心枢纽
架构·kafka·linq
一路向北North1 天前
使用reactor-rabbitmq库监听Rabbitmq
分布式·rabbitmq·ruby
Amy187021118231 天前
赋能低压分布式光伏“四可”建设,筑牢电网安全新防线
分布式