一、背景
SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一。它们三者之间的关系大概如下图:
关系图
二、开发SpringBoot应用
首先,创建一个SpringBoot项目,pom文件如下:
<dependency> |
|
<groupId>org.springframework.boot</groupId> |
|
<artifactId>spring-boot-starter-actuator</artifactId> |
|
</dependency> |
|
<dependency> |
|
<groupId>org.springframework.boot</groupId> |
|
<artifactId>spring-boot-starter-web</artifactId> |
|
</dependency> |
|
<dependency> |
|
<groupId>org.projectlombok</groupId> |
|
<artifactId>lombok</artifactId> |
|
<optional>true</optional> |
|
</dependency> |
|
<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot --> |
|
<dependency> |
|
<groupId>io.prometheus</groupId> |
|
<artifactId>simpleclient_spring_boot</artifactId> |
|
<version>0.8.1</version> |
|
</dependency> |
|
<dependency> |
|
<groupId>org.springframework.boot</groupId> |
|
<artifactId>spring-boot-starter-security</artifactId> |
|
</dependency> |
注意: 这里的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因为最新的simpleclient_spring_boot只支持1.5.X,不确定2.X版本的能否支持。
MonitorDemoApplication启动类增加注解
- package cn.sp;
- import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
- import io.prometheus.client.spring.boot.EnableSpri