### 接口说明
springboot集成Prometheus需要开发的接口有:
- 监控JVM、tomcat等相关的指标;
- 自定义监控程序相关指标;
### 监控JVM、tomcat等相关的指标
`micrometer`已经为我们做好了相关的接口,只需要引入依赖即可.
```xml
org.springframework.boot
spring-boot-starter-actuator
io.micrometer
micrometer-registry-prometheus
```
设置`application.yml`
```yml
server:
port: 9090
spring:
application:
name: application-name
management:
endpoint:
endpoints:
web:
exposure:
include: ‘*‘
metrics:
tags:
application: ${spring.application.name}
```
启动程序后,访问`/actuator/prometheus`即可获取相关指标.
### 自定义监控程序相关指标
如果上面的接口返回的指标不够用,需要自己开发,可以参考下面的:
```java
@GetMapping(value = "/metrics", produces = "text/plain")
@ResponseBody
String metrics() {
// 这里产生的随机数,实际按需修改
return "user_random{application=\"application\"} " + (int)(Math.random()*10);
}
```
### 然后配置到Prometheus的Targets中即可.springboot集成Prometheus监控指标
原文:https://blog.51cto.com/happywzy/2940330