1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        springboot項目通過prometheus和grafana實現(xiàn)監(jiān)控和報警

        共 5462字,需瀏覽 11分鐘

         ·

        2022-08-10 10:38

        • 一、prometheus

        • 1.1、什么是prometheus?

        prometheus是一套開源的監(jiān)控和告警系統(tǒng),基于go語言開發(fā),它會定期從指定的目標處獲取應用的性能指標信息,然后存儲起來,以供查詢及分析。

        bdec03c797da9243215012bfa7342d81.webp

        • 1.2、安裝prometheus

        • 1.2.1、下載

        下載地址:https://prometheus.io/download/#prometheus,顯示的是最新的版本,選擇對應的系統(tǒng)直接下載就行,我這里下載的是windows版

        6a9d51ecef14a39ce9fd16fb5505b7a9.webp

        • 1.2.2、安裝

        解壓即可

        1ceb5b61dac151755e6196d84378963f.webp

        • 1.2.3、將prometheus集成到應用中

        1.2.3.1、用idea創(chuàng)建一個springboot項目依賴如下:
        <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter</artifactId>    </dependency>
        <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
        <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
        <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
        <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
        application.yml配置如下:
        server:  port: 8080spring:  application:    name: data-analysis#actuator暴露的endpointmanagement:  endpoints:    web:      exposure:        include: "*"  metrics:    tags:      application: ${spring.application.name}
        1.2.3.2、修改prometheus.yml,將應用信息添加進去

        0048682c0ac84b0a2f054b7297f2a45f.webp

        3b2b855950b72f2b7cb0dce79e2fa6ae.webp

        ??#應用名  - job_name: "data-analysis"????#只要添加了spring-boot-starter-actuator這個依賴就有,寫死的/actuator/prometheus    metrics_path: /actuator/prometheus    static_configs:      #這是你應用的ip和端口號      - targets: ["127.0.0.1:8080"]
        1.2.3.3、啟動應用和prometheus雙擊prometheus.exe即可啟動

        03d64db6a0fc5de403eb1b246cb36386.webp

        訪問:http://localhost:9090/targets

        0fe3585e82a52eeaa3e8e47912526913.webp

        可以看到自己的應用已經被prometheus采集到了。點擊鏈接http://127.0.0.1:8080/actuator/prometheus可以看到prometheus采集的應用數(shù)據(jù)

        5a5f6bc846471ddf645fc72a87004a32.webp

        b7544575f6d72d584f15d5dcb4f58809.webp

        可以看到,非常的不直觀,那如何直觀地展現(xiàn)這些數(shù)據(jù)呢?有請grafana出場!
        • 二、grafana

        grafana是一款用Go語言開發(fā)的開源數(shù)據(jù)可視化工具,可以做數(shù)據(jù)監(jiān)控和數(shù)據(jù)統(tǒng)計,帶有告警功能。
        • 2.1、grafana下載

        下載地址:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1&platform=windows

        f719b4ec9978d00b46b5c042223a2b4a.webp

        msi為安裝版,zip為解壓縮版,我這里選擇解壓版,你們根據(jù)喜好選擇即可。
        • 2.2、grafana安裝

        解壓即可

        1d49d6d4e2dfa9ac12d0922f6f0ee6f3.webp

        • 2.3、啟動訪問

        728aa6b25e697cbb22fe83d219cf8f34.webp

        訪問http://localhost:3000/

        253007643f66e095fa96bbd8191c0908.webp

        初始賬號密碼都是admin,第一次登錄后提示你修改密碼,不想修改直接skip即可,下面是登錄成功后的主界面

        015f2ddbad29df418be04245609b2856.webp

        • 2.4、通過grafana展示prometheus采集到的數(shù)據(jù)

        • 2.4.1、展示應用的JVM信息

        2.4.1.1、在啟動類中注入以下bean后重啟
        @Bean  public MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {    return (registry -> registry.config().commonTags("application", applicationName));  }
        2.4.1.2、添加數(shù)據(jù)源有個螺絲樣子的按鈕,鼠標放上去,會浮現(xiàn)出來一個下拉列表,點擊Data?sources

        81f77ecd2d2ef444f0300206e9718698.webp

        接著點Add data source

        903d3ecb7b0e9a5a1e04c82e6e00a49a.webp

        點Prometheus

        aff49513e92eefda2de47f60c73723ee.webp

        接著填下http://localhost:9090/

        ee5c168cdcdd9a0486125cfdfd87cf80.webp

        接著點save

        b990d0694618365557d0ef59d26b118b.webp

        數(shù)據(jù)源添加完畢!
        2.4.1.3、導入一個視圖模板鼠標放到左邊的+號上,會浮動出一個下拉列表,點擊其中的Import

        57403efc905a527b8979aba906c0b10a.webp

        填入https://grafana.com/dashboards/4701

        9a735a41c7ce67ac19d794368515327e.webp

        點擊load,然后會讓你選擇一個數(shù)據(jù)源,選擇剛才添加的prometheus數(shù)據(jù)源即可

        448f8e354264567cca11d7f152e10098.webp

        接著點擊Import,然后出現(xiàn)的界面展示的就是你當前應用的JVM各種指標和參數(shù)情況,是不是直觀多了?

        c7b6cb7da74fc06b87101ec26c2072f0.webp

        • 2.4.2、自定義metrics,監(jiān)控方法執(zhí)行的次數(shù)和耗時

        使用TimedAspect類,能獲取方法的執(zhí)行次數(shù)和總耗時,通過一點簡單的計算也可以得到平均耗時2.4.2.1、改動工程
        依賴中加上:
        <!--TimedAspect中需要用到aop,所以需要導入--><dependency>      <groupId>org.aspectj</groupId>      <artifactId>aspectjweaver</artifactId>    </dependency>
        啟動類中注入一個TimedAspect類型的bean
        @Bean  public TimedAspect timedAspect(MeterRegistry registry) {    return new TimedAspect(registry);  }
        自定義一個service
        package com.data.service;
        import io.micrometer.core.annotation.Timed;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;
        import java.util.Random;
        @Servicepublic class TimeService {
        @Scheduled(cron = "*/3 * * * * ?")????//@Timed注解可以收集到方法的執(zhí)行次數(shù)和耗時 @Timed(description = "timeService.execute") public int execute() throws InterruptedException { int sleepTime = new Random().nextInt(100); Thread.sleep(sleepTime); return sleepTime; }}
        加好后重啟下你的應用,然后點擊prometheus中的鏈接

        3a454b01c3f68e0c2e07ab6366298b36.webp

        搜索timeService.execute

        3b30565497b03dee166d7e51c0795698.webp

        紅線框內的語句,等會要用
        #execute方法執(zhí)行的次數(shù)method_timed_seconds_count{application="data-analysis",class="com.data.service.TimeService",exception="none",method="execute",}#execute方法的總耗時method_timed_seconds_sum{application="data-analysis",class="com.data.service.TimeService",exception="none",method="execute",}#execute方法單次執(zhí)行的最長耗時method_timed_seconds_max{application="data-analysis",class="com.data.service.TimeService",exception="none",method="execute",}
        2.4.2.2、在grafana中展示這些參數(shù)將鼠標放到左邊的加號位置,在浮動出來的下拉列表中點擊Dashboard

        ee876b55b10cd1da0fbdd0a5df6b370d.webp

        接著點Add a new?panel

        605ac9d64500032f2ce7378258a27ed7.webp

        這時候就用到2.4.2.1中說到的語句了,隨便找一個,比如:
        #execute方法執(zhí)行的次數(shù)method_timed_seconds_count{application="data-analysis",class="com.data.service.TimeService",exception="none",method="execute",}
        將其填入Metrics browser后面的框中,你會發(fā)現(xiàn)一個圖形出現(xiàn)了

        0002e40500ce5a1a882d0cd9130e985c.webp

        這個圖表示的就是execute方法已經執(zhí)行的次數(shù),隨著時間遞增的,很簡單。

        接著可以點擊下面的+Query,把其它兩條語句也加上

        994248188d8eca1ceb0973a39577faac.webp

        然后點擊右上角的save按鈕,將其保存起來,下次可以直接查看

        cb6f84787077098cf673028b9f348dd7.webp

        • 2.4.3、grafana的報警功能

        上面說了grafana展示監(jiān)控數(shù)據(jù)的功能,但是開發(fā)和運維不可能7*24小時盯著監(jiān)控看,那如果出現(xiàn)異常情況了怎么能讓人及時知道呢?沒錯,利用grafana的報警功能,它支持釘釘、email等,這里演示下email告警2.4.3.1、添加告警配置修改grafana的defaults.ini文件,添加email相關信息

        4059ef5f6a9419b80bbbabdce13122b4.webp

        配置和我的保持一致即可,除了163郵箱地址和授權碼,這個每個人都不一樣的,至于怎么找到授權碼,見下圖,注意授權碼只展示一次,需要記好

        c3a0687dbccaef8ffa97a164e6f5a93e.webp

        改完后重啟grafana
        2.4.3.2、添加channel

        45cd2b4200950d8ea979837bac6b0087.webp

        然后點擊Add channel

        8d9ad7abb99fc9391ff92a5844bfd19d.webp

        然后填寫channel信息,接著點擊test,然后save

        06910c1b910e0a9a9a2f74877a66d536.webp

        去你的163郵箱里看下,你會發(fā)現(xiàn)收到了一個測試郵件

        84bd13341e35d1c399ceaa5cfe871aff.webp

        2.4.3.3、給timeService execute dashboard添加報警規(guī)則

        147f9796fa754177e56c1c37fc667ce4.webp

        打開這個dashboard,點擊edit

        152dcab0432e5af82c831a006051aa24.webp

        之后點擊Alert,然后點擊Create Alert

        6319f4d49c2b877fc9ce43b4c3c15baa.webp

        填寫配置如下

        c0691cc2af7b5a508eaefb43684b8fa6.webp

        這里大致解釋下上面的意思
        • Rule

        1. Name:規(guī)則的名字

        2. Evaluate every:檢測頻率

        3. For:當觸發(fā)了報警條件時,等過了For設置的時間后,再發(fā)送報警事件


        • Conditions

        1. WHEN:選擇一個聚合函數(shù)

        2. OF:query的條件

        3. IS ABOVE:超過,也可選其他操作符

        注意:WHEN max() OF ( query C,10s,now) IS ABOVE 5,這個規(guī)則大致含義是說:取10秒內的數(shù)據(jù),如果最大值大于5就觸發(fā)告警,那個C是獲取數(shù)據(jù)的查詢語句。


        • No Data & Error Handling

        1. if no data or all values are null:如果沒有數(shù)據(jù)或值都為空

        2. if execution error or timeout:如果執(zhí)行錯誤或超時


        • Notifications

        1. Send to:指定channel

        2. Message:報警內容

        ?

        然后點擊save

        829af3611fe799bcad3be192830edd6e.webp

        2.4.3.4、修改代碼,讓接口的執(zhí)行時間超過5s,測試下告警

        5fdc30fbb9a74301a28b66c3618eb3d3.webp

        重啟應用后等待一會就會收到告警郵件了

        acca15c86f46e94417f37526ce8d6ad3.webp

        瀏覽 119
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产精品爽爽久久久 | 北条麻妃肉丝袜办公室 | 玖玖网 | 另类黄色小说 | 免费成人在线观看 | 97超碰手机在线 | 五月天丁香网 | 李信和阿里拔萝卜是什么意思 | 久久综合五月天 | 99热在线播放 |