Spring Boot 應(yīng)用可視化監(jiān)控

圖文簡介

邏輯關(guān)系
效果演示

快速開始

1、Spring Boot 應(yīng)用暴露監(jiān)控指標(biāo)【版本 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);
    }

}

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

security:
  user:
    name: user
    password: pwd

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

啟動應(yīng)用程序后,會看到如下一系列的 Mappings

Mappings

利用賬號密碼訪問 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指標(biāo)數(shù)據(jù)

指標(biāo)數(shù)據(jù)

2、Prometheus 采集 Spring Boot 指標(biāo)數(shù)據(jù)

首先,獲取 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 應(yīng)用的 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 采集狀態(tài)是否正常。

采集狀態(tài)

3、Grafana 可視化監(jiān)控?cái)?shù)據(jù)

首先,獲取 Grafana 的 Docker 鏡像:

$ docker pull grafana/grafana

然后,啟動 Grafana:

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

接著,訪問 http://localhost:3000/ 配置 Prometheus 數(shù)據(jù)源:

Grafana 登錄賬號 admin 密碼 admin

配置 DataSource

最后,配置單個(gè)指標(biāo)的可視化監(jiān)控面板:

選擇 Graph
編輯
配置需要監(jiān)控的指標(biāo)

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

指標(biāo)

多配置幾個(gè)指標(biāo)之后,即可有如下效果:

Grafana 監(jiān)控界面

參考文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容