拒絕黑盒應用-Spring Boot 應用可視化監控


img

圖文簡介

img

邏輯關系

img

效果演示

快速開始

1、Spring Boot 應用暴露監控指標【版本 1.5.7.RELEASE】

首先,添加依賴如下依賴:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_spring_boot</artifactId>
            <version>0.0.26</version>
        </dependency>

然后,在啟動類 Application.java 添加如下注解:

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

最后,配置默認的登錄賬號和密碼,在 application.yml 中:

security:
  user:
    name: user
    password: pwd

提示:不建議配置 management.security.enabled: false

啟動應用程序后,會看到如下一系列的 Mappings

img

Mappings

利用賬號密碼訪問 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指標數據

img

指標數據

2、Prometheus 采集 Spring Boot 指標數據

首先,獲取 Prometheus 的 Docker 鏡像:

$ docker pull prom/prometheus

然后,編寫配置文件 prometheus.yml

global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
scrape_configs:
  - job_name: spring-boot
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /application/prometheus
    scheme: http
    basic_auth:
      username: user
      password: pwd
    static_configs:
      - targets:
        - 127.0.0.1:8080  #此處填寫 Spring Boot 應用的 IP + 端口號

接着,啟動 Prometheus :

$ docker run -d \
--name prometheus \
-p 9090:9090 \
-m 500M \
-v "$(pwd)/prometheus.yml":/prometheus.yml \
-v "$(pwd)/data":/data \
prom/prometheus \
-config.file=/prometheus.yml \
-log.level=info

最后,訪問 http://localhost:9090/targets , 檢查 Spring Boot 采集狀態是否正常。

img

采集狀態

3、Grafana 可視化監控數據

首先,獲取 Grafana 的 Docker 鏡像:

$ docker pull grafana/grafana

然后,啟動 Grafana:

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

接着,訪問 http://localhost:3000/ 配置 Prometheus 數據源:

Grafana 登錄賬號 admin 密碼 admin

img

配置 DataSource

最后,配置單個指標的可視化監控面板:

img

選擇 Graph

img

編輯

img

配置需要監控的指標

提示,此處不能任意填寫,只能填已有的指標點,具體的可以在 Prometheus 的首頁看到,即 http://localhost:9090/graph

img

指標

多配置幾個指標之后,即可有如下效果:

img

Grafana 監控界面

參考文檔

© 著作權歸作者所有,轉載或內容合作請聯系作者

原文鏈接:
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247486173&idx=1&sn=571a068a4d49cf7c054ebde623c67d17&chksm=fb3f1326cc489a30cef820c1be230a351b0ef0501f86f2a9c8dbf19bc694f9e5a32a40173194&token=423629656&lang=zh_CN#rd

本文由博客一文多發平台 OpenWrite 發布!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM