grafana+prometheus监控Springboot服务
原创
©著作权归作者所有:来自51CTO博客作者抹香鲸之海的原创作品,请联系作者获取转载授权,否则将追究法律责任
Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,由 SoundCloud 公司开发。
Prometheus 基本原理是通过 HTTP 协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供 HTTP 接口就可以接入监控系统,不需要任何 SDK 或者其他的集成过程。这样做非常适合虚拟化环境比如 VM 或者 Docker 。
Prometheus 应该是为数不多的适合 Docker、Mesos、Kubernetes 环境的监控系统之一。
1.拉取镜像
docker pull prom/prometheus
2.创建prometheus目录,新增prometheus.yml文件,用于指定设置docker地址
mkdir -p /home/prometheus
3.创建prometheus容器,指定端口、名称、挂载目录、设置时区
docker run -d -p 9090:9090 -v /home/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
4.访问页面
120.48.54.67:9090
springboot配置
在spring boot工程中引入actuator的起步依赖,以及micrometer-registry-prometheus的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.6.4</version>
</dependency>
application.yml暴露prometheus的接口;暴露metrics.tags,和spring.application.name一致。
server:
port: 8087
spring:
application:
name: swaggerDemo
management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
回到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.
###以下内容为SpringBoot应用配置
- job_name: 'swaggerDemo'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['120.48.54.67:8087']
重启docker 容器,查看prometheus控制台:
点击查看,可以看到服务信息但是这是prometheus的信息,对于我们来说还是不好理解,这个时候需要结合Grafana 。
使用 Prometheus + Grafana 实现可视化界面
Add data source(添加数据源)
Import(导入模板)
12884是个默认的模板也可以自己去官网选一个模板:
查看仪表盘:
Prometheus+Alertmanager详细配置邮箱告警