一、整体结构
整体结构图
- 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",}