使用Prometheus监控SpringBoot应用

通过之前的文章我们使用Prometheus监控了应用服务器nodeexporter,数据库mysqldexporter,今天我们来监控一下你的应用。(本文以SpringBoot 2.1.9.RELEASE 作为监控目标)

编码

添加依赖

使用Prometheus监控SpringBoot应用只需要在pom文件添加如下两个依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>复制代码

修改配置

修改application.properties或者application.yml文件,对外暴露监控端点

spring.application.name = blog
management.endpoints.web.exposure.include = prometheus
management.metrics.tags.application = ${spring.application.name}复制代码

启动应用

访问http://localhost:50378/actuator/查看开放端点file

访问http://localhost:50378/actuator/prometheus查看Metricsfile

之前的文章中都是监控一个实例,这次咱们把端口设置成随机,启动两个实例,等下看看监控效果。

监控配置

Prometheus

修改prometheus.yml,增加监控任务

  - job_name: 'SpringBoot'
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['10.1.61.10:50378','10.1.61.10:50822']复制代码

重启Prometheus,查看监控状态。file

Grafana

直接从grafana官网寻找JVM监控DashBoard,然后导入到Grafana中(在环境搭建篇中已经讲述了操作过程,这里就不再赘述)。file

查看效果

filefile

彩蛋

我们关掉其中一个SpringBoot实例,等2分钟,然后邮箱会收到这样一封告警邮件file

怎么实现的呢?咱们下期有缘再见!

获取更多内容请关注公众号:JAVA日知录

微信公众号

猜你喜欢

转载自juejin.im/post/5db0f16c6fb9a0207473865c