
? Micrometer 是一個(gè)統(tǒng)一監(jiān)控指標(biāo)采集的門面,這個(gè)有點(diǎn)類似SLF4J,具體的指標(biāo)數(shù)據(jù)采集實(shí)現(xiàn)有AppOptics, Azure Monitor, Atlas, CloudWatch, Datadog, Dynatrace, Elastic, Ganglia, Graphite, Humio, Influx/Telegraf, JMX, KairosDB, New Relic, Prometheus, SignalFx, Stackdriver, StatsD,Wavefront等。因此使用Micrometer時(shí),只需更換底層實(shí)現(xiàn)包,應(yīng)用程序無需修改任何代碼即可對(duì)接到不同監(jiān)控系統(tǒng)。
? 在最新SpringBoot2.0中,Micrometer門面已經(jīng)整合到了spring-boot-starter-actuator項(xiàng)目中,我們只需引入相應(yīng)的具體實(shí)現(xiàn)包即可對(duì)接到相應(yīng)的監(jiān)控系統(tǒng),本次將使用promethues來監(jiān)控、采集SpringBoot的指標(biāo)數(shù)據(jù)。
引入promehtus依賴
implementation 'org.springframework.boot:spring-boot-starter-actuator'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
配置指標(biāo)endpoint
默認(rèn)情況下指標(biāo)的endpoint可以與服務(wù)同一個(gè)端口,通常為了不對(duì)業(yè)務(wù)造成干擾,使用額外的端口向外暴露指標(biāo)endpoint,只需更改application.yml即可實(shí)現(xiàn),如下:
management:
endpoints:
web:
exposure:
include: "*"
server:
port: 30000
瀏覽器訪問http://localhost:30000/actuator/metrics 可以查看到目前存在監(jiān)控指標(biāo),這些指標(biāo)都是默認(rèn)開啟的。

使用promethues采集指標(biāo)數(shù)據(jù)
目前dockerHub上未提供官方的鏡像,這里還使用二進(jìn)制文件的方式來進(jìn)行監(jiān)控?cái)?shù)據(jù)的采集,下載完二進(jìn)制包后,需修改promethues.yml文件,配置scrape_configs,詳細(xì)如下:
# 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).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 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'
scrape_interval: 5s
metrics_path: '/actuator/prometheus' #指標(biāo)路徑
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['127.0.0.1:30000'] #指標(biāo)暴露地址端口
啟動(dòng)promethues
./prometheus --config.file=./prometheus.yml
使用Grafana可視化指標(biāo)
這里使用docker運(yùn)行g(shù)rafana,默認(rèn)賬號(hào)密碼:admin/admin
docker run -d -p 3000:3000 grafana/grafana
來到設(shè)置,準(zhǔn)備添加數(shù)據(jù)源。

這里選擇promethues

輸入 ip(ip不能填127.0.0.1 或者是localhost,局域網(wǎng)ip就行) 和 端口即可,點(diǎn)擊下面的save&Test完成數(shù)據(jù)源的添加。
添加Panel,這里選之前創(chuàng)建的數(shù)據(jù)源,選擇指標(biāo)即可實(shí)現(xiàn)可視化。

監(jiān)控http響應(yīng)
默認(rèn)已經(jīng)開啟了http請(qǐng)求的監(jiān)控,但是未開啟histogram
management:
endpoints:
web:
exposure:
include: "*"
server:
port: 30000
metrics:
distribution:
percentiles-histogram[http.server.requests]: true
maximum-expected-value[http.server.requests]: 10000 #預(yù)期最大值
minimum-expected-value[http.server.requests]: 1 #預(yù)期最小值
編寫測(cè)試接口
@RestController
public class TestController {
@GetMapping(value = "/hello")
public String helloPromethues(){
try {
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
return "helloPromethues";
}
}
使用wrk壓測(cè)該接口
wrk -t4 -c2000 -d100s http://127.0.0.1:8080/hello
在Grafana中創(chuàng)建Panel,指標(biāo)選擇http_server_requests_seconds_bucket,并修改Y軸單位為秒。
