Spring Boot服務(wù)監(jiān)控(Prometheus)
哲學(xué)
最近看到了一句話:定乎內(nèi)外之分 辯乎榮辱之境。
一個外國作家也說過:
我生命里的的最大突破之一,就是我不再為別人的看法而擔(dān)憂。此后,我真的能自由的去做我認(rèn)為對自己最好的事,只有在我們不需要外來的贊許時,才變得自由。
說的都很好。人就是要突破自己,就像許三多,不要在意別人的看法,做自己認(rèn)為有意義的事,今天比昨天好,這不就是希望。
監(jiān)控
思考完一波哲學(xué),開始搞搞軟件上的東西。這篇記錄下監(jiān)控配置相關(guān)的知識。
為什么需要監(jiān)控系統(tǒng):簡單點(diǎn)說。隨時掌握系統(tǒng)運(yùn)行情況,保證在你預(yù)期內(nèi)運(yùn)行。
先不扯別的,看兩張效果圖:
1、監(jiān)控Linux服務(wù)器的CPU,內(nèi)存,磁盤等:
2、監(jiān)控Tomcat和jvm:
概念
1、Prometheus是什么,一款開源的優(yōu)秀的時間序列數(shù)據(jù)庫監(jiān)控軟件。收集各項(xiàng)指標(biāo),用于監(jiān)控系統(tǒng)狀態(tài)。提供強(qiáng)大的PromQL查詢語句,滿足各種個性化查詢需求。
2、什么是Metrics,Metrics就是監(jiān)控指標(biāo),在外行術(shù)語中,指標(biāo)是數(shù)字度量,時間序列意味著隨著時間的推移記錄變化。用戶想要測量的內(nèi)容因應(yīng)用程序而異。對于web服務(wù)器來說,它可能是請求時間,對于數(shù)據(jù)庫,它可能是活動連接數(shù)或活動查詢數(shù)等。簡單理解,就是你想監(jiān)控的東西,不必過分深究。
3、Grafana又是什么?簡單來說就是圖形化展示工具,和Prometheus天作之合。
安裝配置
1、下載prometheus:
wget https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
2、配置prometheus:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
# 這里這個就是自帶的監(jiān)控,監(jiān)控preometheus自己
- targets: ['localhost:9090']
以上就完成了prometheus的下載和配置,非常的簡單。
訪問host:9090就可以看到如下界面:
這就是prometheus的管理頁面。不夠酷炫,接下來下載grafana。
3、下載grafana
sudo yum install grafana
sudo systemctl start grafana-server
這就完事了。驗(yàn)證下,默認(rèn)的管理端是運(yùn)行在3000端口,也就是http://ip:3000,就可以打開如下頁面,賬號密碼默認(rèn)都是admin。
我們現(xiàn)在有了prometheus和grafana,接下來將grafana連上prometheus。
1、添加數(shù)據(jù)源。
2、連接上prometheus。
3、測試是否連接成功。
這就完成了。截止到現(xiàn)在,最基本prometheus和grafna下載和安裝的操作就完畢了。
exporter
接下來,來監(jiān)控linux的狀態(tài)。這個也是極其的簡單。
首先下載node_exporter,然后啟動
wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz
tar xvfz node_exporter-*.*-amd64.tar.gz
cd node_exporter-*.*-amd64
./node_exporter
再去prometheus修改下配置文件prometheus.yaml,仿照之前的,加上下面的配置。然后重啟prometheus。
- job_name: node
static_configs:
- targets: ['localhost:9100']
這樣已經(jīng)有了監(jiān)控linux運(yùn)行情況的能力了,只是目前還沒有展示出來而已。
接下來加上酷炫的頁面。官網(wǎng)有配置好的,甚至不用自己配置。地址是:
https://grafana.com/grafana/dashboards/。
打開如下,搜索node,選中第一個。
復(fù)制官方提供的模板ID。然后去grafana導(dǎo)入一下即可。
大功告成了,如下圖所示。
jvm的也是類似的操作,自己可以試驗(yàn)。
自定義監(jiān)控指標(biāo)
以上都是官方提供的exporter。監(jiān)控機(jī)器或者jvm的,如果我們想監(jiān)控自己的業(yè)務(wù)呢?例如想監(jiān)控當(dāng)前有多少請求?每個請求的性能如何?或者其他一些自定義的監(jiān)控項(xiàng)?
在寫代碼之前,認(rèn)識幾個概念:prometheus中的四種指標(biāo)類型。Counter(計數(shù)器):Counter類型用于增加的值,例如請求計數(shù)或錯誤計數(shù)。最重要的是,絕對不能將計數(shù)器用于可能減小的值。只增不減。
Gauges(儀表板(我自己的翻譯)):儀表類型可用于向下和向上的值,例如當(dāng)前內(nèi)存使用量或隊(duì)列中的項(xiàng)目數(shù),可增可減。
histogram(直方圖):這個概念比較難以理解。暫時我們認(rèn)為他就是統(tǒng)計分位樹的就好。例如你這個接口99%請求的耗時,TP99。
summaries:本篇不講,感興趣自行查看官網(wǎng)。
這四種類型,都什么時候使用呢?Counter:
1、你想記錄一個只上升的值。
2、希望以后能夠查詢該值的增長速度(即增長率)。
Guage:
1、想要記錄一個可以上升或下降的值。
2、你不需要查詢它的增長率。
histogram:分桶計算,分位計算,計算TP99等。
OK,接下來寫代碼。
監(jiān)控Spring Boot應(yīng)用
用java,一般用Spring Boot項(xiàng)目開發(fā),這個很容易實(shí)現(xiàn),全部都是封裝好的。
從一個最基本的項(xiàng)目入手,只需要如下的依賴即可。注意到這里除了web模塊,還加了兩個監(jiān)控模塊。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
然后寫一個簡單的controller。
package com.test.promethusmetrics;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 測試Counter
*
* @author fengkai
*/
@RestController
public class CounterController {
private final Counter requestCount;
public CounterController(CollectorRegistry collectorRegistry) {
requestCount = Counter.build()
.name("request_count")
.help("Number of hello requests.")
.register(collectorRegistry);
}
@GetMapping(value = "/hello")
public String hello() {
requestCount.inc();
return "Hi!";
}
}
注意到這里用了counter。這就完成了counter計數(shù)的代碼部分。
代碼完成后,還需要讓prometheus去拉取我們Spring Boot的監(jiān)控指標(biāo),配置和之前很相似。
添加如下配置,然后重啟prometheus。
- job_name: "spring"
metrics_path: /actuator/prometheus
static_configs:
- targets: ["192.168.181.1:8080"]
我們在瀏覽器上多請求幾次。然后我們?nèi)rafana上配置監(jiān)控面板,首先添加。
然后配置指標(biāo)。
效果圖如下。
以上只是單純的計數(shù),實(shí)際用途不是很大,其實(shí)更關(guān)心的應(yīng)該是增長率。這又該如何統(tǒng)計呢?
只需要在外層包裹rate函數(shù)就可以了,具體的原理可以后續(xù)再解釋,這里先用起來。
接下來再試一下使用histogram,統(tǒng)計下Spring Boot服務(wù)的請求的耗時情況如何?
代碼部分:
package com.test.promethusmetrics;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Histogram;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import static java.lang.Thread.sleep;
@RestController
public class HistogramController {
private final Histogram requestDuration;
public HistogramController(CollectorRegistry collectorRegistry) {
requestDuration = Histogram.build()
.name("test_wait")
.help("Time for HTTP request.")
.register(collectorRegistry);
}
@GetMapping(value = "/wait")
public String makeMeWait() throws InterruptedException {
Histogram.Timer timer = requestDuration.startTimer();
long sleepDuration = Double.valueOf(Math.floor(Math.random() * 10 * 1000)).longValue();
sleep(sleepDuration);
timer.observeDuration();
return String.format("I kept you waiting for %s ms!", sleepDuration);
}
}
多訪問幾次:localhost:8080/wait然后grafana配置,這里用的是直方圖histogram,計算的性能。QL的語法本篇不講解,可以參考官網(wǎng)。
效果圖如下:
總結(jié)
現(xiàn)在,我們應(yīng)該清楚地了解prometheus中可以使用的不同監(jiān)控指標(biāo)類型,以及何時使用它們,如何查詢它們。并且能夠用grafna配置酷炫的監(jiān)控圖標(biāo)。有了這些知識,可以更有效地發(fā)布應(yīng)用程序中的監(jiān)控,并確保它始終按預(yù)期運(yùn)行。