监控工具—Prometheus—监控Java程序
原文作者:青蛙小白
原文地址:Prometheus監控實踐:使用Prometheus監控Java應用
目錄
1、Prometheus JVM Client
2、Prometheus的服務發現
3、Grafana Dashboard和告警規則
之前在《Prometheus監控實踐:Kubernetes集群監控》一本中總結了我們目前基于Prometheus對Kubernetes集群的監控,除了監控Kubernetes集群本身的關鍵指標之外,也對部署在Kubernetes集群上應用的狀態做了監控。 對于Kubernetes集群上Pod, DaemonSet, Deployment, Job, CronJob等各種資源對象,我們通過kube-state-metrics作為Prometheus的exporter完成了對這些Kubernetes資源對象的監控。而為了使監控深入到應用的內部,就需要應用自身暴露作為Exporter暴露監控指標,這就和應用的開發語言和技術框架緊密相關了。我們目前部署在Kubernetes上的微服務主要是由Go和Java兩種語言開發的,本篇將總結一下目前我們使用Prometheus對Java應用的監控實踐。
1、Prometheus JVM Client
Prometheus提供了一個client_java項目可以很方便的將JVM和自定義的指標暴露出來。目前我們主要用到了這個項目中的如下幾個庫:
- simpleclient_hotspot
- simpleclient_spring_web
- simpleclient_spring_boot
- simpleclient_httpserver
使用Java可以開發以下兩種服務:
1)第一種是使用spring-boot-starter-web開發的HTTP Restful API
這類服務要集成Prometheus十分簡單,只需要在項目中依賴管理如引入simpleclient_hotspot和simpleclient_spring_boot。
compile 'io.prometheus:simpleclient_spring_boot:0.1.0' compile 'io.prometheus:simpleclient_hotspot:0.1.0'simpleclient_spring_boot中的io.prometheus.client.spring.boot.PrometheusEndpointConfiguration會將prometheus exporter注冊為Spring Boot Actuator的enpoint。啟動這個配置只需在spring boot項目的Appication類上加上@EnablePrometheusEndpoint的注解,例如:
@EnablePrometheusEndpoint @EnableSpringBootMetricsCollector @SpringBootApplication public class BootApplication {@PostConstructpublic void init() {DefaultExports.initialize();}......當然spring boot的配置文件中需要配置如何暴露這個endpoint:
management:port: 8088security:enabled: falsehealth:defaults:enabled: false endpoints:enabled: falseprometheus:enabled: truepath: /prometheushealth:enabled: truepath: /health此時http://:8088/prometheus這個端點就是這個Java應用暴露的監控指標。
2)第二種是使用最基本的spring-boot-starter和其他starter開發的rpc服務(thrift,gRPC等)
本身不包含嵌入的tomcat,而Prometheus的exporter需要以HTTP暴露監控指標。需要在項目中依賴管理如引入simpleclient_hotspot和simpleclient_httpserver。
compile 'io.prometheus:simpleclient_httpserver:0.1.0' compile 'io.prometheus:simpleclient_hotspot:0.1.0'simpleclient_httpserver庫包含一個簡單的httpserver,使用其完成監控指標的暴露:
DefaultExports.initialize(); new io.prometheus.client.exporter.HTTPServer.HTTPServer(8088);http://:8088這個端點就是這個Java應用暴露的監控指標。
2、Prometheus的服務發現
當Java應用中集成了Prometheus JVM Client后,就可以以HTTP的形式暴露監控指標。 如果Java應用以Pod的形式部署在Kubernetes集群上,為了使Kubernetes集群中的Prometheus可以發現Pod暴露的HTTP監控端點,還需要在Kubernetes manifest文件中加入下面的annotation:
--- kind: Deployment apiVersion: apps/v1beta2 metadata:labels:......name: xxx-svcnamespace: xx spec:......selector:matchLabels:app: xxx-svctemplate:metadata:labels:app: xxx-svcannotations:prometheus.io/scrape: 'true'prometheus.io/path: /prometheusprometheus.io/port: '8088'spec:containers:......這樣Prometheus中配置的job?kubernetes-pods就可以自動發現服務暴露的監控端點:
- job_name: 'kubernetes-pods'kubernetes_sd_configs:- role: podrelabel_configs:- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]action: keepregex: true- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]action: replacetarget_label: __metrics_path__regex: (.+)- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]action: replaceregex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:$2target_label: __address__- action: labelmapregex: __meta_kubernetes_pod_label_(.+)- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: kubernetes_namespace- source_labels: [__meta_kubernetes_pod_name]action: replacetarget_label: kubernetes_pod_name3、Grafana Dashboard和告警規則
關于Java應用中JVM的監控的Dashboard,目前還沒有在網上找到太好的,下圖是目前我們自己畫的一個,收集到了監控數據后,告警規則根據需要配置就可以了。
同系列文章推薦閱讀:
監控工具—Prometheus—基礎介紹
監控工具—Prometheus—安裝部署
監控工具—Prometheus—監控Java程序
監控工具—Prometheus—監控Redis
總結
以上是生活随笔為你收集整理的监控工具—Prometheus—监控Java程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 监控工具—Prometheus—安装部署
- 下一篇: Java数据结构—基本数据类型