歡迎來到Spring Boot Actuator系列教程的第二部分,在第一部分,你學到了spring-boot-actuator 模塊能夠作什麼,在spring boot application如何配置,以及怎樣和各類actuator端點交互。git
在本文中,你將學到 spring boot actuator如何和監控系統 Prometheus、被稱爲圖形解決方案Grafana 的集成。github
在本文末尾,你還能在本地機子設置Prometheus 和 Grafana dashboard,可視化和監控全部來自Spring Boot application的指標數據。spring
Prometheus 是一個開源監控系統,它最初是由SoundCloud建立的。docker
它由下面的核心組件組成:數據庫
Grafana 容許你從各類數據源導入數據,像Elasticsearch, Prometheus, Graphite, InfluxDB等等,tomcat
而且用漂亮的圖表可視化這些數據。服務器
它還可讓你設置基於指標數據的警告規則。當警告狀態發生改變時,能夠經過email, slack或者其餘渠道通知到你。網絡
注意這裏,Prometheus dashboard 也有簡單的圖表,可是Grafana 的圖表更好,這就是爲何,本文咱們用Prometheus和Grafana集成一塊兒,導入和可視化咱們的指標數據。app
Spring Boot 使用 Micrometer一個應用指標外觀 ,集成actuator metrics和使用外部監控系統。它支持多個監控系統,像Netflix Atlas, AWS Cloudwatch, Datadog, InfluxData, SignalFx, Graphite, Wavefront, Prometheus 等等。jvm
爲了集成 Prometheus,你須要添加micrometer-registry-prometheus 依賴。
<!-- Micrometer Prometheus registry --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
一旦你添加了上面的依賴,Spring Boot將會自動配置一個PrometheusMeterRegistry 和CollectorRegistry 去收集和導出可被Prometheus服務器scraped格式的指標數據.全部application的指標數據在actuator的/prometheus 端點 均可獲取,Prometheus服務器經過該端點週期性獲取指標數據。
咱們探索prometheus 端點,當micrometer-registry-prometheus依賴可用的時候,它由Spring Boot 暴露出來。
首先,你啓動endpoint-discovery頁面 http://localhost:8080/actuator 能夠看到prometheus 端點信息。
prometheus 端點暴露了被Prometheus服務器格式的指標數據。經過http://localhost:8080/actuator/prometheus你能夠看到暴露的指標數據。
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool # TYPE jvm_buffer_memory_used_bytes gauge jvm_buffer_memory_used_bytes{id="direct",} 81920.0 jvm_buffer_memory_used_bytes{id="mapped",} 0.0 # HELP jvm_threads_live The current number of live threads including both daemon and non-daemon threads # TYPE jvm_threads_live gauge jvm_threads_live 23.0 # HELP tomcat_global_received_bytes_total # TYPE tomcat_global_received_bytes_total counter tomcat_global_received_bytes_total{name="http-nio-8080",} 0.0 # HELP jvm_gc_pause_seconds Time spent in GC pause # TYPE jvm_gc_pause_seconds summary jvm_gc_pause_seconds_count{action="end of minor GC",cause="Allocation Failure",} 7.0 jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Allocation Failure",} 0.232 jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",} 1.0 jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Metadata GC Threshold",} 0.01 jvm_gc_pause_seconds_count{action="end of major GC",cause="Metadata GC Threshold",} 1.0 jvm_gc_pause_seconds_sum{action="end of major GC",cause="Metadata GC Threshold",} 0.302 # HELP jvm_gc_pause_seconds_max Time spent in GC pause # TYPE jvm_gc_pause_seconds_max gauge jvm_gc_pause_seconds_max{action="end of minor GC",cause="Allocation Failure",} 0.0 jvm_gc_pause_seconds_max{action="end of minor GC",cause="Metadata GC Threshold",} 0.0 jvm_gc_pause_seconds_max{action="end of major GC",cause="Metadata GC Threshold",} 0.0 # HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC # TYPE jvm_gc_live_data_size_bytes gauge jvm_gc_live_data_size_bytes 5.0657472E7 ## More data ...... (Omitted for brevity)
你可使用docker pull命令 下載Prometheus docker 鏡像,像這樣:
$ docker pull prom/prometheus
下載完鏡像,你能夠輸入docker 鏡像命令,查看本地存在的鏡像列表
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE prom/prometheus latest b82ef1f3aa07 5 days ago
接着,咱們須要從Spring Boot Actuator的/prometheus 端點,配置Prometheus 以便scrape 指標數據
使用下面的配置,建立一個名叫prometheus.yml 的新文件
# 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). # 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=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['127.0.0.1:9090'] - job_name: 'spring-actuator' metrics_path: '/actuator/prometheus' scrape_interval: 5s static_configs: - targets: ['HOST_IP:8080']
以上配置是從Prometheus文檔提供的配置文件基礎上做了擴展。最重要的,值得注意是,在scrape_configs 部分中, spring-actuator job 的metrics_path ,指定 Actuator的prometheus 端點路徑,目標部分包括了你的Spring Boot 應用的HOST 和 PORT,請確保用運行Spring Boot application 的機子的IP地址,代替HOST_IP,注意這裏,localhost在這裏是無效的,由於咱們從docker 容器鏈接HOST的機子,你必須指定網絡IP地址。
最後,咱們使用 Docker運行Prometheus。輸入下面的命令, 之後臺服務方式,啓動Prometheus 服務-
$ docker run -d --name=prometheus -p 9090:9090 -v <PATH_TO_prometheus.yml_FILE>:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
請確保 用你Prometheus 配置文件存儲的路徑,代替<PATH_TO_prometheus.yml_FILE>
執行上面命令以後,docker 將會在容器內啓動Prometheus 服務 。用下面的命令,你能夠查看全部容器的列表。
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e036eb20b8ad prom/prometheus "/bin/prometheus --c…" 4 minutes ago Up 4 minutes 0.0.0.0:9090->9090/tcp prometheus
就是這樣,你如今能夠導航到http://localhost:9090 查看Prometheus dashboard了。
你能夠在表達式文本框中,輸入一個Prometheus 查詢表達式。
下面是咱們Spring Boot 應用指標的一些Prometheus圖
1.系統的CPU使用狀況
2.響應延遲的緩慢API
輸入下面Docker命令下載和運行Grafana
$ docker run -d --name=grafana -p 3000:3000 grafana/grafana
以上命令會在Docker容器內部 啓動Grafana,在主機機器上保證3000端口可用。
你能夠輸入docker容器ls 命令 查看容器列表
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf9196b30d0d grafana/grafana "/run.sh" Less than a second ago Up 5 seconds 0.0.0.0:3000->3000/tcp grafana e036eb20b8ad prom/prometheus "/bin/prometheus --c…" 16 minutes ago Up 16 minutes 0.0.0.0:9090->9090/tcp prometheus
就是這樣,你如今能夠導航到http://localhost:3000 ,用默認用戶名admin 和密碼admin 登陸Grafana
下列步驟 ,從Prometheus導入指標數據,並在Grafana可視化它。
1.在Grafana 添加 Prometheus數據源
2.用Graph建立一個新Dashboard
3.在Grafana的查詢編輯器,添加一個Prometheus查詢。
4. 從Grafana的dashboard可視化指標
你能夠從Github上,找到完整的Actuator演示源代碼。