Grafana + Prometheus 監控JVM


  最近在研究監控系統,所以第一次接觸了Grafana跟Prometheus,Grafana是一個很強大的可視化指標工具,而Prometheus是一個時序數據庫。

  項目總會慢慢做大,一些必要的監控以及預警是很有必要的。

  所以研究了下JVM的監控,可以有以下兩種方式,Grafana官網上有很多共享的展示面板模板,可看哪個更符合自己的需求,當然也可以自己創建。

  首先安裝Grafana跟Prometheus,這個比較簡單,可自行百度。

  1.Jmx_exporter + Prometheus +Grafana

    1.1下載Jmx_exporter架包:https://github.com/prometheus/jmx_exporter

    1.2在配置Jmx_exporter的config.yaml:

---
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem"]
blacklistObjectNames: []
rules:
  - pattern: 'java.lang<type=OperatingSystem><>(committed_virtual_memory|free_physical_memory|free_swap_space|total_physical_memory|total_swap_space)_size:'
    name: os_$1_bytes
    type: GAUGE
    attrNameSnakeCase: true
  - pattern: 'java.lang<type=OperatingSystem><>((?!process_cpu_time)\w+):'
    name: os_$1
    type: GAUGE
    attrNameSnakeCase: true

    1.3啟動你的應用:java -javaagent:./jmx_prometheus_javaagent-0.12.0.jar=8698:config.yaml -jar yourJar.jar

     我使用的是springboot,所以加運行參數即可。

    1.4配置Promethus的config:

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: 'monitor-demo'

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

    static_configs:
    - targets: ['localhost:8698']  

    1.5Grafana我使用的是模板是8563,直接Import就好,在Import之前應首先在Grafana創建Promethus的數據源

       https://grafana.com/grafana/dashboards/8563

  2.Micrometer + Prometheus +Grafana

  上面的方面,不夠靈活,不能定制收集指標,而使用Micrometer則比較靈活,可以定制自己想要的指標,而不僅僅是JVM,還可以是自己應用的指標,但在這里暫時不詳述。

  可以到這篇文章了解一下:

  https://www.cnblogs.com/rolandlee/p/11343848.html

  這里使用Grafana模板是4701

  https://grafana.com/grafana/dashboards/4701

    2.1 maven所需要的到依賴

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

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>io.github.mweirauch</groupId>
            <artifactId>micrometer-jvm-extras</artifactId>
            <version>0.2.0</version>
        </dependency>

    2.2application.properties配置(注意,這里沒有配置management.endpoints.web.base-path,所以默認的請求路徑是/actuator,而Promethus的路徑是/actuator/prometheus

server.port=8699
        
#spring.application.name=JVM-micrometer

management.metrics.tags.application=JVM-micrometer

#prometheus配置
management.metrics.export.prometheus.enabled=true
management.metrics.export.prometheus.step=1ms
management.metrics.export.prometheus.descriptions=true

management.endpoint.prometheus.enabled=true
management.endpoints.web.exposure.include=health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics

    2.3配置Prometheus的config:

    - job_name: 'actuator-demo'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['localhost:8699'] 

    2.4在Grafana上Import4701即可

   至此監控JVM系統就搭建起來了,比較簡單。

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM