13

Prometheus为你的微服务保驾护航

 3 years ago
source link: https://mp.weixin.qq.com/s?__biz=MzIwMDY0Nzk2Mw%3D%3D&%3Bmid=2650325676&%3Bidx=1&%3Bsn=6a56693f781b15f9f6ccd4ef42188296
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

前面我们介绍了 Prometheus 的作用和整体的架构,相信大家对 Prometheus 有了一定的了解。

具体可以查看这篇文章: 《天天CRUD的我,也想玩玩高大上的Prometheus》

今天着重介绍下如何在项目中将 Prometheus 用起来,结合漂亮的图表做数据展示,真的非常帅气。

使用之前先介绍一个 Micrometer,Micrometer 是一款监控指标的度量类库,提供了对各种指标的监控。比如 JVM, 线程池,数据库连接池等。

官方网站: https://micrometer.io/ [1]

项目集成

首先在项目中添加下面的 Maven 依赖,如下:

<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.5.9</version>
</dependency>

增加对应的配置,如下:

management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}

exposure.include 配置你要暴露的端点信息,全部就配置成 * 号。

tags.application 配置成跟服务名一样即可。

访问/actuator/prometheus 就可以看到很多指标数据了,至于这些数据是怎么出来的就不做过多讲解,对应的代码都在 micrometer-registry-prometheus 包中,大家可以自己去研究下。

# HELP hikaricp_connections_max Max connections
# TYPE hikaricp_connections_max gauge
hikaricp_connections_max{application="haomai-customer",pool="HikariPool-1",} 10.0
# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds{application="haomai-customer",} 1.611642684781E9
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="haomai-customer",} 2.68435456E8
# HELP tomcat_sessions_created_sessions_total
# TYPE tomcat_sessions_created_sessions_total counter
tomcat_sessions_created_sessions_total{application="haomai-customer",} 0.0

数据采集

如果没有做服务动态发现,那就手动修改 Prometheus 配置文件,新增一个任务进行抓取。

 - job_name: 'haomai-customer-beta'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.1.49:8099']

在 Targets 中查看是否成功。

IVBfyiE.png!mobile图片

数据展示

先去 grafana 搜一个帅气的图表,地址如下:

https://grafana.com/grafana/dashboards?search=spring%20boot [2]

选第一个就行了,星星数量多点。

IfiAbm3.png!mobile

点进去复制图表的编号 12856,去 grafana 中导入即可,选择数据源就可以展示了。

aANnMrz.png!mobile

当然像数据库连接之类的也可以去搜专门的图表来展示,或者自定义图表,这个后面再给大家介绍。

同样还有告警也是需要单独做的,可以用 grafana 自带的告警来做,也可以单独部署 Alertmanager 来做告警。后续再单独再介绍哈。

关于作者:尹吉欢,简单的技术爱好者,《Spring Cloud 微服务-全栈技术与案例解析》, 《Spring Cloud 微服务 入门 实战与进阶》作者, 公众号 猿天地 发起人。

参考资料

[1]

https://micrometer.io/: https://micrometer.io/

[2]

https://grafana.com/grafana/dashboards?search=spring%20boot: https://grafana.com/grafana/dashboards?search=spring%20boot

- END -

后台回复  学习资料   领取学习视频

YnaUVjb.jpg!mobile

如有收获,点个在看,诚挚感谢


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK