springboot項目通過prometheus和grafana實現(xiàn)監(jiān)控和報警
一、prometheus
1.1、什么是prometheus?

1.2、安裝prometheus
1.2.1、下載

1.2.2、安裝

1.2.3、將prometheus集成到應用中
application.yml配置如下:<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>
1.2.3.2、修改prometheus.yml,將應用信息添加進去server:port: 8080spring:application:name: data-analysis#actuator暴露的endpointmanagement:endpoints:web:exposure:include: "*"metrics:tags:application: ${spring.application.name}


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




二、grafana
2.1、grafana下載

2.2、grafana安裝

2.3、啟動訪問



2.4、通過grafana展示prometheus采集到的數(shù)據(jù)
2.4.1、展示應用的JVM信息
2.4.1.2、添加數(shù)據(jù)源有個螺絲樣子的按鈕,鼠標放上去,會浮現(xiàn)出來一個下拉列表,點擊Data?sources@Beanpublic MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {return (registry -> registry.config().commonTags("application", applicationName));}





2.4.1.3、導入一個視圖模板鼠標放到左邊的+號上,會浮動出一個下拉列表,點擊其中的Import




2.4.2、自定義metrics,監(jiān)控方法執(zhí)行的次數(shù)和耗時
依賴中加上:
啟動類中注入一個TimedAspect類型的bean<!--TimedAspect中需要用到aop,所以需要導入--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency>
自定義一個service@Beanpublic TimedAspect timedAspect(MeterRegistry registry) {return new TimedAspect(registry);}
加好后重啟下你的應用,然后點擊prometheus中的鏈接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;}}


2.4.2.2、在grafana中展示這些參數(shù)將鼠標放到左邊的加號位置,在浮動出來的下拉列表中點擊Dashboard#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",}


將其填入Metrics browser后面的框中,你會發(fā)現(xiàn)一個圖形出現(xiàn)了#execute方法執(zhí)行的次數(shù)method_timed_seconds_count{application="data-analysis",class="com.data.service.TimeService",exception="none",method="execute",}

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


2.4.3、grafana的報警功能


2.4.3.2、添加channel








Rule
Name:規(guī)則的名字
Evaluate every:檢測頻率
For:當觸發(fā)了報警條件時,等過了For設置的時間后,再發(fā)送報警事件
Conditions
WHEN:選擇一個聚合函數(shù)
OF:query的條件
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
if no data or all values are null:如果沒有數(shù)據(jù)或值都為空
if execution error or timeout:如果執(zhí)行錯誤或超時
Notifications
Send to:指定channel
Message:報警內容
?



評論
圖片
表情
