Prometheus
通过 Prometheus 和 Grafana 监控 Spring Cloud 服务
实现的效果:当em-gateway服务宕机后, 飞书通知到群里。
- prometheus(/proˈmiθɪəs/): Prometheus 负责从各个目标服务中收集监控数据。
- grafana: 开源的可视化系统,可实现Prometheus、Elasticsearch、Postgres、InfluxDB等数据源的可视化,grafana 通过连接 Prometheus 数据源,利用其丰富的可视化选项创建直观的监控仪表板。
- prometheusalert: prometheusalert是一个开源的告警通知工具,旨在简化 Prometheus 告警的通知流程,并支持多种通知渠道。它可以帮助用户将 Prometheus 的告警信息发送到各种常用的消息平台和通知系统,如微信、钉钉、飞书、Slack、邮件等
1. 准备工作:
需要监控的项目,都需要集成actuator, 可以使Prometheus通过http请求收集数据。参考博客
2. docker-compose 安装
1 |
|
3. 配置 Prometheus
1. 修改配置文件
这里使用到了host.docker.internal
, 因为我的Java服务是运行在宿主机的,而prometheus是运行在容器里的,使用host.docker.internal
来访问宿主机
prometheus.yml
1 |
|
2. 配置 Spring Cloud 应用
引入依赖
1
2
3
4<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>修改配置
1
2
3
4
5
6
7
8
9
10
11## 开启所有actuator-endpoint
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include:
- '*'
- 'prometheus'访问项目
/actuator/prometheus
- 再次访问
http://localhost:9090/targets
确保当前服务成功注册到了prometheus上
- 配置属性在prometheus简单查看
4. 配置 Grafana
访问 Grafana 打开浏览器访问 http://localhost:3000,默认用户名和密码是 admin/admin
1. 配置Prometheus作为datasource
2. 测试展示系统的CPU使用情况
3. dashborad模板
网址:Grafana dashboards | Grafana Labs
- dashboard 市场有很多现成的dashborad,可以直接使用,比如:
jmx_exporter: https://grafana.com/grafana/dashboards/3457
node_exporter: https://grafana.com/grafana/dashboards/8919
mysqld_exporter:
redis_exporter: https://grafana.com/grafana/dashboards/11835
- 具体使用步骤
- 关键词搜索
- 复制模板ID
- 填入ID导入
4. 引入Prometheusalert
因为grafana的github的issue上写了他们短期不支持飞书,我也尝试配置webhook,飞书通知失败,所以这里引入了prometheusalert来做飞书通知。
- 在模板管理里,找到自己需要的模板,复制URL, 后续作为webhook地址,供grafana使用。这是我下面示例使用的URL
4. 配置grafana告警
- 修改 prometheusalert.yml (不确定是否真的需要,可以先不配试试)
1 |
|
- 增加 Contact points,配置红框中的属性
- 创建看板,展示em-gateway的运行情况
- 配置Alert
停止服务,查看飞书通知;
Prometheus
http://example.com/Prometheus/