springboot集成Prometheus+Grafana+Micrometer监控JVM

前提

引入pom依赖

<dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

yml配置

server:
  port: 8090
spring:
  application:
    name: prometheus-demo
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    tags:
      application: ${spring.application.name}

启动类中追加MeterRegistryCustomizer配置

@SpringBootApplication
public class PrometheusApplication {

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

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer() {
        return registry -> registry.config().commonTags("application", "prometheus-demo");
    }

}

测试端点

为了方便一会查看效果,新建一个测试类,开放一个访问端点:

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping(path = "/hello")
    public String hello() {

        return "hello :" + new Random().nextInt(10);
    }

}

部署项目并配置prometheus

我们将项目打包放到服务器中启动后,并在prometheus.yml中追加如下配置对此项目进行监控:

- job_name: "prometheus-demo"

    # metrics_path defaults to /metrics
    # scheme defaults to http.
    scrape_interval: 5s
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ["localhost:8090"]

此时完整配置如下:

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global evaluation_interval.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here its Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to /metrics
    # scheme defaults to http.

    static_configs:
      - targets: ["localhost:9090"]
      
  - job_name: node
    static_configs:
      - targets: [localhost:9100]
      
  - job_name: "prometheus-demo"

    # metrics_path defaults to /metrics
    # scheme defaults to http.
    scrape_interval: 5s
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ["localhost:8090"]

重启prometheus后,访问 http://your ip:9090/targets ,界面中会出现刚才我们的测试程序:

引入JVM(Micrometer)

在grafana中导入Micrometer,页面中可看到Micrometer的ID为4701,我们直接load4701即可引入监控页面。

我们这

如果使用ID无法load,可以在官网下载json文件后,将json文件的内容复制到panel json中进行加载:

下面的Name自定义一下,选择我们的Prometheus后点击Import进行监控页面的加载呈现:

我们频繁访问 /test/hello 端点,再查看监控页面,就可以看到监控画面的变化:

经验分享 程序员 微信小程序 职场和发展