spring boot 2x版本中,actuator使用的是micrometer,prometheus数据源可以直接使用spring boot的actuator接口。
prometheus配置
接上一篇prometheus安装
编辑/usr/local/etc/prometheus.yml, 添加内容:
scrape_configs:
- job_name: "prometheus"
metrics_path: /actuator/prometheus
scheme: http
static_configs:
- targets: ["localhost:8803"]
metrics_path为数据源路径,targets为被监控服务地址。
Springboot应用添加依赖:
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Micrometer Prometheus registry -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
yml添加配置内容,开启actuator监控:
management:
endpoints:
web:
exposure:
include: "*"
exclude: "shutdown"
启动应用,访问http://localhost:8803/actuator/prometheus,内容如下:
# HELP tomcat_sessions_expired_sessions_total
# TYPE tomcat_sessions_expired_sessions_total counter
tomcat_sessions_expired_sessions_total 0.0
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="heap",id="PS Survivor Space",} 1.31072E7
...
表明springboot添加已开启监控。
grafana中展示应用监控
grafana安装
应用中已经注入MeterRegistry实例,引用并初始化:
@Resource
private MeterRegistry meterRegistry;
private Counter userTotal;
@PostConstruct
public void init() {
String userTotalName = "demo_user_total";
userTotal = Counter.builder(userTotalName).register(meterRegistry);
}
添加测试接口:
@RequestMapping("/all")
@ResponseBody
public Collection<User> all() {
userTotal.increment();
return new ArrayList<>();
}
浏览器中访问几次/all接口,并返回grafana中添加一个面板,数据指标为应用中初始化的demo_user_total:
image.png
可以看到访问计数已记录。
micrometer支持Timer、Counter等指标参考micrometer wiki
监控jvm、qps配置参考
网友评论