springboot集成Prometheus+Grafana+Micrometer监控JVM

目录

前提

引入pom依赖

yml配置

启动类中追加MeterRegistryCustomizer配置

测试端点

部署项目并配置prometheus

引入JVM(Micrometer)


前提

在此之前,需要先安装好prometheus、grafana,这些在上一篇已经配置好,参考如下:Linux(阿里云服务器)安装Prometheus、node_exporter、Grafana集成node exporter面板

引入pom依赖


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        
  
        
            io.micrometer
            micrometer-registry-prometheus
            runtime
        
    

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 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 it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` 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 ,界面中会出现刚才我们的测试程序:

springboot集成Prometheus+Grafana+Micrometer监控JVM_第1张图片 

引入JVM(Micrometer)

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

我们这 

springboot集成Prometheus+Grafana+Micrometer监控JVM_第2张图片

springboot集成Prometheus+Grafana+Micrometer监控JVM_第3张图片

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

springboot集成Prometheus+Grafana+Micrometer监控JVM_第4张图片

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

springboot集成Prometheus+Grafana+Micrometer监控JVM_第5张图片

 springboot集成Prometheus+Grafana+Micrometer监控JVM_第6张图片

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

springboot集成Prometheus+Grafana+Micrometer监控JVM_第7张图片​​​​​​​

你可能感兴趣的:(Springboot集成监控,java,linux,postman)