1. 程式人生 > >Spring-Boot-應用視覺化監控

Spring-Boot-應用視覺化監控

![秋](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101826437-1676467765.png) ## 圖文簡介 ![邏輯關係](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101827082-1989047936.png) ![效果演示](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101827514-117513422.png) ## 快速開始 #### 1、Spring Boot 應用暴露監控指標【版本 1.5.7.RELEASE】 首先,新增依賴如下依賴: ``` org.springframework.boot spring-boot-starter-actuator io.prometheus simpleclient_spring_boot 0.0.26 ``` 然後,在啟動類 `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` ![Mappings](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101827751-761550313.png) 利用賬號密碼訪問 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指標資料 ![指標資料](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101828011-2041982373.png) #### 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 採集狀態是否正常。 ![採集狀態](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101828167-967398989.png) #### 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 ![配置 DataSource](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101828331-1913475358.png) 最後,配置單個指標的視覺化監控面板: ![選擇 Graph](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101828458-716466771.png) ![編輯](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101828659-1406540088.png) ![配置需要監控的指標](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101828819-1677522218.png) 提示,此處不能任意填寫,只能填已有的指標點,具體的可以在 Prometheus 的首頁看到,即 http://localhost:9090/graph ![指標](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101829016-94546709.png) 多配置幾個指標之後,即可有如下效果: ![Grafana 監控介面](https://img2020.cnblogs.com/other/509099/202003/509099-20200307101829422-523275359.png) ## 參考文件 - [prometheus 官方文件](https://prometheus.io/docs/introduction/overview/) - [Grafana Docker 安裝](http://docs.grafana.org/installation/docker/) - [Spring Boot 官方文件](http://projects.spring.io/spring-boot/) ## 文末福利 Java 資料大全 連結:https://pan.baidu.com/s/1pUCCPstPnlGDCljtBVUsXQ 密碼:b2xc 更多資料: 2020 年 精選阿里 Java、架構、微服務精選資料等,加 v ❤ :qwerdd111 >
轉載,請保留原文地址,