Spring Boot 應用可視化監控,一目瞭然!

1、Spring Boot 應用暴露監控指標

Spring Boot 基礎就不介紹了,最新的教程和示例源碼看這個:https://github.com/javastacks/spring-boot-best-practice

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

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

採集應用的指標信息,我們使用的是prometheus,相應的我們引入包:

<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

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

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

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: admin
      password: 123456
    static_configs:
      - targets:
        - 192.168.11.54:8099 #此處填寫 Spring Boot 應用的 IP + 端口號

接着,啓動 Prometheus :

docker run -d --name prometheus -p 9090:9090
-v D:\test\actuator\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

D:\test\actuator\prometheus\prometheus.yml 是我的配置文件存放地址,我們需要將它放到容器裏面去,所以用了-v來做文件映射。

/etc/prometheus/prometheus.yml這個是容器啓動的時候去取的默認配置,這裏我是直接覆蓋掉了它。prom/prometheus這是鏡像,如果本地沒有,就回去你設置好的鏡像倉庫去取。

啓動完成後用**docker ps**看下是否已經啓動成功,之後打開瀏覽器輸入: http://localhost:9090/targets,檢查 Spring Boot 採集狀態是否正常,如果看到下圖就是成功了。

3、Grafana 可視化監控數據

首先,獲取 Grafana 的 Docker 鏡像:

$ docker pull grafana/grafana1

然後,啓動 Grafana:

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

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

Grafana 登錄賬號 admin 密碼 admin

  1. 先配置數據源.

2.配置單個指標的可視化監控面板:

prometh採集的數據:

參考:https://www.jianshu.com/p/7ecb57a3f326

原文鏈接:https://blog.csdn.net/weixin_44196699/article/details/105010045

版權聲明:本文爲CSDN博主「你照亮了我的世界」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。

近期熱文推薦:

1.600+ 道 Java面試題及答案整理(2021最新版)

2.終於靠開源項目弄到 IntelliJ IDEA 激活碼了,真香!

3.阿里 Mock 工具正式開源,幹掉市面上所有 Mock 工具!

4.Spring Cloud 2020.0.0 正式發佈,全新顛覆性版本!

5.《Java開發手冊(嵩山版)》最新發布,速速下載!

覺得不錯,別忘了隨手點贊+轉發哦!

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