自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringBoot整合Prometheus構(gòu)建高效、靈活的監(jiān)控系統(tǒng)

開發(fā) 前端
SpringBoot整合Prometheus的原理是SpringBoot應(yīng)用將metrics數(shù)據(jù)上報給Prometheus,數(shù)據(jù)上報有基于Pushgateway方式和直接上報方式。

  對于任何一個應(yīng)用系統(tǒng)來說,監(jiān)控都是至關(guān)重要的一個組成部分。如果應(yīng)用系統(tǒng)在運(yùn)行過程當(dāng)中出現(xiàn)故障(如網(wǎng)絡(luò)中斷、內(nèi)存溢出、磁盤空間不足、軟件自身Bug),任何一個方面出現(xiàn)問題都有可能影響到整個系統(tǒng)的正常運(yùn)行,甚至給企業(yè)帶來資損,因此監(jiān)控必不可少。那么SpringBoot項目如何實(shí)現(xiàn)一套完整、成熟的監(jiān)控系統(tǒng)呢?    

    首先,SpringBoot是一個可以快速創(chuàng)建獨(dú)立、可運(yùn)行的、生產(chǎn)級別的Spring應(yīng)用的項目框架,Prometheus是一個開源的監(jiān)控和告警工具,廣泛應(yīng)用于微服務(wù)架構(gòu)的監(jiān)控。將SpringBoot應(yīng)用與Prometheus整合,可以實(shí)現(xiàn)應(yīng)用的實(shí)時監(jiān)控和性能分析,幫助開發(fā)人員快速定位和解決問題。下面我們來實(shí)現(xiàn)SpringBoot整合prometheus。

1、SpringBoot項目添加相關(guān)的依賴

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


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


    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
</dependencies>

2、SpringBoot項目添加yml配置

server:
  port: 8081


spring:
  application:
    name: prometheus-demo


#prometheus
management:
  endpoints:
    web:
      exposure:
        include: '*'

    在Springboot整合Prometheus的時候,項目啟動后會開啟Actuator服務(wù),SpringBoot Actuator會自動配置一個URL為 /actuator/Pprometheus的http服務(wù)來供prometheus抓取數(shù)據(jù)。

3、添加測試controller

@RestController
@RequestMapping("/test")
public class OrderController {


    @GetMapping("/test1")
    public String test1(){


        return "success";
    }


    @GetMapping("/test2")
    public String test2(){
        List<byte[]> byteList = new ArrayList<>();
        while (true){
            byteList.add(new byte[1024*1024*2]);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e){


            }
        }
    }
}

4、啟動Prometheus后檢查springBoot項目是否正常

    在SpringBoot配置好Prometheus配置文件后,啟動SpringBoot項目,我們需要檢查項目在Prometheus上的狀態(tài)是否正常,如下圖Springboot項目的啟動圖:

圖片

    待Springboot項目啟動完成后,在Prometheus中查詢Springboot項目的監(jiān)控狀態(tài),如下圖所示的正常監(jiān)控狀態(tài):

圖片圖片

項目在Prometheus上的狀態(tài)是綠色表示是正常的狀態(tài)。

5、prometheus的配置文件中添加SpringBoot監(jiān)控任務(wù)

圖片圖片

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ["Prometheus_ip:9090"]
  - job_name: 'springboot-prometheus'
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ["springboot項目的ip:8081"]

6、Grafana配置prometheus數(shù)據(jù)源

6.1 添加prometheus

圖片圖片

6.2 配置prometheus

圖片圖片

    點(diǎn)擊Save&Test可以看到如下的提示就是表示添加Prometheus數(shù)據(jù)源成功:

圖片圖片

6.3 挑選可視化的dashborad

    通過訪問https://grafana.com/grafana/dashboards這個鏈接去挑選可視化的dashborad,下圖是一個典型的SpringBoot項目的監(jiān)控可視化的dashborad:

圖片圖片

然后獲取dashborad的編號:

圖片圖片

圖片圖片

6.4 添加dashborad到Grafana中

    在Grafana中添加dashborad,如下圖所示:

圖片圖片

輸入dashborad編號

圖片圖片

然后選擇prometheus數(shù)據(jù)源

圖片圖片

配置好之后可以就看到Granafa的監(jiān)控面板

圖片圖片

7、測試監(jiān)控

圖片圖片

請求執(zhí)行死循環(huán)的代碼,我們可以發(fā)現(xiàn)請求正在被執(zhí)行,如下圖所示:

圖片圖片

執(zhí)行一段時間后,再觀察Granafa監(jiān)控面板的效果:

圖片圖片

    通過監(jiān)控我們發(fā)現(xiàn)內(nèi)消耗一直不斷增加,代碼出現(xiàn)問題我們可以第一時間快速的發(fā)現(xiàn),然后定位解決。

總結(jié):

(1)SpringBoot整合Prometheus的原理是SpringBoot應(yīng)用將metrics數(shù)據(jù)上報給Prometheus,數(shù)據(jù)上報有基于Pushgateway方式和直接上報方式。

(2)基于Pushgateway方式適用于短期運(yùn)行的應(yīng)用,將metrics數(shù)據(jù)推送到Pushgateway中,由Pushgateway統(tǒng)一推送至Prometheus。

(3)直接上報方式適用于長期運(yùn)行的應(yīng)用,將metrics數(shù)據(jù)直接上報給Prometheus中。直接上報方式是通過配置文件的配置,將SpringBoot應(yīng)用會把metrics數(shù)據(jù)暴露在/actuator/prometheus路徑下,Prometheus通過抓取該路徑的數(shù)據(jù)即可獲取應(yīng)用的metrics數(shù)據(jù)。

責(zé)任編輯:武曉燕 來源: 龍蝦編程
相關(guān)推薦

2022-11-08 00:00:00

監(jiān)控系統(tǒng)Prometheus

2025-02-06 09:43:08

HybridFlowRay大語言模型

2023-07-10 08:26:19

2023-09-06 08:46:47

2025-02-11 07:55:45

2020-12-30 08:09:46

運(yùn)維Prometheus 監(jiān)控

2025-03-06 02:00:00

.NETGrafana工具

2020-12-29 10:45:22

運(yùn)維Prometheus-監(jiān)控

2010-06-17 14:34:18

Rsync 使用

2011-11-29 13:09:02

2009-05-14 17:09:24

城域網(wǎng)寬帶傳送

2023-11-06 01:39:02

Go語言開發(fā)

2020-12-28 10:13:32

運(yùn)維Prometheus監(jiān)控

2023-09-01 07:31:24

2025-02-28 08:03:45

2015-03-12 09:42:56

2020-03-09 10:09:18

混合云云計算

2021-03-26 20:37:14

Prometheus監(jiān)控指標(biāo)

2012-03-08 16:06:37

2023-11-07 10:01:34

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號