springboot可视化监控之prometheus
2020-07-07 本文已影响0人
炒面Z
涉及组件介绍
- prometheus 定时拉取springboot的指标数据存储于其本身自带的时序数据库中
- grafana 美观、强大的可视化监控指标展示工具,此处搭配数据源 prometheus 使用
- springboot组件 micrometer-registry-prometheus可以把springboot的指标转化为prometheus 可以存储的格式
1. springboot项目增加组件 micrometer-registry-prometheus 和 actuator
- pom文件
<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>
- 增加指标配置类
MetricsConfig.java
@Configuration
public class MetricsConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.
config().
commonTags("application", applicationName);
}
}
- yml配置文件
management:
endpoints:
jmx:
exposure:
include: "*"
web:
exposure:
include: health,prometheus
endpoint:
health:
show-details: always
metrics:
export:
datadog:
application-key: ${spring.application.name}
2.在 prometheus 配置任务定时抓取springboot指标数据
- 编辑prometheus.yml 配置文件,后重启prometheus
- job_name: 'content-cloud-webapi-test' #任务名
metrics_path: 'actuator/prometheus' # endpoint
scrape_interval: 15s # 信息收集间隔10秒
static_configs:
- targets: ['IP:PORT'] # springboot项目暴露的地址+端口
3.在grafana中配置模板ID展示dashborad,此处使用报表模板ID=4701
![](https://img.haomeiwen.com/i9626161/58f0267abf3d624a.png)
grafana和prometheus在docker下的安装和使用可参考: https://www.jianshu.com/p/87e1ca5b84c9