最近在研究监控系统,因此第一次接触了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系统就搭建起来了,比较简单。