SpringBoot整合Prometheus


Micrometer 是一個統一監控指標採集的門面,這個有點類似SLF4J,具體的指標數據採集實現有AppOptics, Azure Monitor, Atlas, CloudWatch, Datadog, Dynatrace, Elastic, Ganglia, Graphite, Humio, Influx/Telegraf, JMX, KairosDB, New Relic, Prometheus, SignalFx, Stackdriver, StatsD,Wavefront等。因此使用Micrometer時,只需更換底層實現包,應用程序無需修改任何代碼即可對接到不同監控系統。

​ 在最新SpringBoot2.0中,Micrometer門面已經整合到了spring-boot-starter-actuator項目中,我們只需引入相應的具體實現包即可對接到相應的監控系統,本次將使用promethues來監控、採集SpringBoot的指標數據。

引入promehtus依賴

implementation 'org.springframework.boot:spring-boot-starter-actuator'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'

配置指標endpoint

默認情況下指標的endpoint可以與服務同一個端口,通常爲了不對業務造成干擾,使用額外的端口向外暴露指標endpoint,只需更改application.yml即可實現,如下:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  server:
    port: 30000

瀏覽器訪問http://localhost:30000/actuator/metrics 可以查看到目前存在監控指標,這些指標都是默認開啓的。

使用promethues採集指標數據

目前dockerHub上未提供官方的鏡像,這裏還使用二進制文件的方式來進行監控數據的採集,下載完二進制包後,需修改promethues.yml文件,配置scrape_configs,詳細如下:

# 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' #指標路徑
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['127.0.0.1:30000'] #指標暴露地址端口

啓動promethues

./prometheus --config.file=./prometheus.yml

使用Grafana可視化指標

這裏使用docker運行grafana,默認賬號密碼:admin/admin

docker run -d -p 3000:3000 grafana/grafana

來到設置,準備添加數據源。

這裏選擇promethues

輸入 ip(ip不能填127.0.0.1 或者是localhost,局域網ip就行) 和 端口即可,點擊下面的save&Test完成數據源的添加。

添加Panel,這裏選之前創建的數據源,選擇指標即可實現可視化。

監控http響應

默認已經開啓了http請求的監控,但是未開啓histogram

management:
  endpoints:
    web:
      exposure:
        include: "*"
  server:
    port: 30000
  metrics:
    distribution:
      percentiles-histogram[http.server.requests]: true 
      maximum-expected-value[http.server.requests]: 10000 #預期最大值
      minimum-expected-value[http.server.requests]: 1 #預期最小值

編寫測試接口

@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壓測該接口

wrk -t4 -c2000 -d100s  http://127.0.0.1:8080/hello

在Grafana中創建Panel,指標選擇http_server_requests_seconds_bucket,並修改Y軸單位爲秒。

獲取源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章