Myluzh Blog

Prometheus 监控 Java 应用和 JVM 性能指标

发布时间: 2024-11-28 文章作者: myluzh 分类名称: Kubernetes 朗读文章


0x01 配置java项目
1、在java项目中pom.xml添加依赖
<!-- Micrometer Prometheus registry -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!-- finished -->
2、修改java项目 src/main/resources/application.yml
Spring Boot 2.1 / Micrometer 1.1.0 with a property:
management.metrics.tags.application=${spring.application.name}
将 Spring Boot 应用的名称(spring.application.name)作为标签(application)添加到所有 Micrometer 度量指标中
management:
  metrics:
    tags:
      application: "${spring.application.name}"
3、打包后上线应用,测试应用是否暴露了metrics接口
[root@centos-test-5b7765fcbd-76vzc /]# curl svc-dxp-api.dev-test.svc.cluster.local:9208/actuator/prometheus
# HELP jdbc_connections_min Minimum number of idle connections in the pool.
# TYPE jdbc_connections_min gauge
jdbc_connections_min{application="xfshcloud-dxp",name="DB1",} 1.0
...
0x02 prometheus 采集数据
1、在prometheus.yaml配置文件中,添加采集接口的配置
- job_name: 'jvm-prometheus'
  scheme: http
  metrics_path: '/actuator/prometheus'
  static_configs:
    - targets: ['svc-dxp-api.dev-test.svc.cluster.local:9208']
0x03 添加 grafana 面板
https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

标签: java Prometheus jvm micrometer

发表评论