Grafana + Promethus 監控JVM

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

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

  因此研究了下JVM的監控,能夠有如下兩種方式,Grafana官網上有不少共享的展現面板模板,可看哪一個更符合本身的需求,固然也能夠本身建立。git

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

  1.Jmx_exporter + Promethus +Grafanaweb

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

    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.jarspringboot

     我使用的是springboot,因此加運行參數便可。app

    1.4配置Promethus的config:jvm

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 + Promethus +Grafana

  上面的方面,不夠靈活,不能定製收集指標,而使用Micrometer則比較靈活,能夠定製本身想要的指標,而不單單是JVM,還能夠是本身應用的指標,但在這裏暫時不詳述。

  能夠到這篇文章瞭解一下:

  http://www.javashuo.com/article/p-anipfzba-kc.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配置Promethus的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系統就搭建起來了,比較簡單。

相關文章
相關標籤/搜索