一、整體結構
整體結構圖
- springboot(micrometer)產生監控數據。
- Prometheus獲取springboot的監控數據。
- Grafana 對接 Prometheus 數據源,展示成相關圖表。
二、 實踐步驟
1)創建springboot應用集成 micrometer
-
在pom中添加micrometer依賴
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator-autoconfigure</artifactId> </dependency>
-
修改配置文件
# 定義應用的名稱 spring.application.name=springboot2prometheus # 公開prometheus的接口,也可以寫*表示公開所有 management.endpoints.web.exposure.include=prometheus
-
啓動應用並訪問
http://localhost:8080/actuator/prometheus
即可看到相關的監控數據
2)部署 Prometheus
由於docker安裝非常方便而且速度很快,所以採用docker安裝的方式,如不熟悉docker的可以看往期的docker技術文章
-
執行docker命令
docker run --rm -p 9090:9090 --name prometheus prom/prometheus
-
從容器中拷貝配置文件到本地
mkdir -p ~/etc/prometheus cd ~/etc/prometheus docker cp prometheus:/etc/prometheus/prometheus.yml .
-
修改拷貝出的配置文件,修改最後三行,完整文件如下
# my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. 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'. metrics_path: '/actuator/prometheus' static_configs: - targets: ["192.168.18.114:8080"] # 爲springboot應用所在地址
-
關閉容器並重新執行(掛載上新的配置文件)
docker run --rm -p 9090:9090 -v ~/etc/prometheus:/etc/prometheus --name prometheus prom/prometheus
3)部署Grafana
-
docker安裝
docker run --rm -p 3000:3000 --name=grafana grafana/grafana
-
訪問
http://localhost:3000/
,默認用戶:admin,密碼:admin -
新建數據源,點擊左側的齒輪按鈕中的DataSource
填入以下信息
-
導入儀表盤,使用4701導入
-
點擊import後即可看到監控面板
4)自定義監控數據和麪板
-
修改springboot應用,添加監控數據-監控請求總數
RequestCounterAop.java
@Aspect @Component public class RequestCounterAop { private Counter counter; @Pointcut("bean(*Controller) && execution(public * *(..))") public void pointCut() { } public RequestCounterAop(MeterRegistry registry) { this.counter = registry.counter("requests_total", "status", "success"); } @Before("pointCut()") public void count(JoinPoint joinPoint) { counter.increment(); //請求計數 } }
TestController.java
@RestController @RequestMapping("/home") public class TestController { @GetMapping("") public String hello() { return "hello;"; } }
-
在瀏覽器中訪問幾次寫好的測試接口
http://localhost:8080/home
,訪問http://localhost:8080/actuator/prometheus
即可看到監控數據 -
打開grafana控制面板
填入以下內容,點擊apply就看到監控面板
requests_total{application="springboot2prometheus",status="success",}