Spring-Boot-應用可視化監控

秋

圖文簡介

邏輯關係

效果演示

快速開始

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

首先,添加依賴以下依賴:java

<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 添加以下註解:spring

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {

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

}複製代碼

最後,配置默認的登陸帳號和密碼,在 application.yml 中:docker

security:
  user:
    name: user
    password: pwd複製代碼

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

啓動應用程序後,會看到以下一系列的 Mappingsapp

Mappings

利用帳號密碼訪問 http://localhost:8080/application/prometheus ,能夠看到 Prometheus 格式的指標數據 指標數據spring-boot

二、Prometheus 採集 Spring Boot 指標數據

首先,獲取 Prometheus 的 Docker 鏡像:微服務

$ docker pull prom/prometheus複製代碼

而後,編寫配置文件 prometheus.ymllua

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 :spa

$ 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 採集狀態是否正常。3d

採集狀態

三、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

配置 DataSource

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

選擇 Graph

編輯

配置須要監控的指標

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

指標

多配置幾個指標以後,便可有以下效果:

Grafana 監控界面

參考文檔

文末福利

Java 資料大全 連接:pan.baidu.com/s/1pUCCPstP… 密碼:b2xc 更多資料: 2020 年 精選阿里 Java、架構、微服務精選資料等,加 v ❤ :qwerdd111

本文由博客一文多發平臺 OpenWrite 發佈!

相關文章
相關標籤/搜索