如何监控你的应用?

1,381 阅读7分钟

1. 前言

监控往往是系统开发过程中最容易被忽视的一部分,但也是系统应用生命周期中相当最重要的一环,有了监控,你才能对你的系统了如指掌。本文将从ActuatorMicrometerPrometheusGrafana4个部分进行简单介绍。

2.Actuator

2.1 添加监控依赖

要想监控SpringBoot应用需要添加如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.2 开启监控端点

添加完依赖后,启动项目,访问http://localhost:8080/actuator/可以看到如下数据

{
    "_links":{
        "self":{
            "href":"http://localhost:8080/actuator",
            "templated":false
        },
        "health-path":{
            "href":"http://localhost:8080/actuator/health/{*path}",
            "templated":true
        },
        "health":{
            "href":"http://localhost:8080/actuator/health",
            "templated":false
        },
        "info":{
            "href":"http://localhost:8080/actuator/info",
            "templated":false
        }
    }
}

默认情况下面只会暴露healthinfo两个端点,如果想暴露所有的端点,需要在yml中添加开启配置

management:
  endpoint:
    health:
      show-details: ALWAYS
  endpoints:
    web:
      exposure:
        include: '*'

再次访问http://localhost:8080/actuator/可以看到更多的端点信息

{
    "_links":{
        "self":{
            "href":"http://localhost:8080/actuator",
            "templated":false
        },
        "custom":{
            "href":"http://localhost:8080/actuator/custom",
            "templated":false
        },
        "beans":{
            "href":"http://localhost:8080/actuator/beans",
            "templated":false
        },
        "caches":{
            "href":"http://localhost:8080/actuator/caches",
            "templated":false
        },
        "caches-cache":{
            "href":"http://localhost:8080/actuator/caches/{cache}",
            "templated":true
        },
        "health-path":{
            "href":"http://localhost:8080/actuator/health/{*path}",
            "templated":true
        },
        "health":{
            "href":"http://localhost:8080/actuator/health",
            "templated":false
        },
        "info":{
            "href":"http://localhost:8080/actuator/info",
            "templated":false
        },
        "conditions":{
            "href":"http://localhost:8080/actuator/conditions",
            "templated":false
        },
        "configprops":{
            "href":"http://localhost:8080/actuator/configprops",
            "templated":false
        },
        "env-toMatch":{
            "href":"http://localhost:8080/actuator/env/{toMatch}",
            "templated":true
        },
        "env":{
            "href":"http://localhost:8080/actuator/env",
            "templated":false
        },
        "loggers-name":{
            "href":"http://localhost:8080/actuator/loggers/{name}",
            "templated":true
        },
        "loggers":{
            "href":"http://localhost:8080/actuator/loggers",
            "templated":false
        },
        "heapdump":{
            "href":"http://localhost:8080/actuator/heapdump",
            "templated":false
        },
        "threaddump":{
            "href":"http://localhost:8080/actuator/threaddump",
            "templated":false
        },
        "metrics-requiredMetricName":{
            "href":"http://localhost:8080/actuator/metrics/{requiredMetricName}",
            "templated":true
        },
        "metrics":{
            "href":"http://localhost:8080/actuator/metrics",
            "templated":false
        },
        "scheduledtasks":{
            "href":"http://localhost:8080/actuator/scheduledtasks",
            "templated":false
        },
        "startup":{
            "href":"http://localhost:8080/actuator/startup",
            "templated":false
        },
        "mappings":{
            "href":"http://localhost:8080/actuator/mappings",
            "templated":false
        }
    }
}

3.Micrometer

通过以上步骤就可以实现在浏览器中监控系统的指标信息,显然这种方式看上去不优雅便且复杂。此时我们就需要借助第三方工具来实现系统的监控,通过micrometer暴露符合prometheus格式的数据,添加相关依赖

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

访问http://localhost:8080/actuator/会发现多了一个端点信息

访问http://localhost:8080/actuator/prometheus可以看到对外暴露的系统指标数据

4.Prometheus

既然系统对外暴露了指标数据,那么就需要有系统来拉取指标数据,这个系统就是prometheus

4.1 Docker拉取Prometheus

docker pull prom/prometheus

4.2 编辑配置文件

在宿主机上创建prometheus.yml文件,编辑内容如下:

global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'spring_micrometer'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['10.100.177.183:8080']

特别注意:由于prometheus是部署在容器中,因此配置文件中的targets选项需要配置宿主机的ip地址,配置localhost会导致拉取不到数据

4.3 运行Prometheus

docker run --name prometheus -p 9090:9090 -v 本地配置文件路径:/etc/prometheus/prometheus.yml -d 镜像id

4.4 访问Prometheus

在浏览器中输入http://localhost:9090,可以正常看到界面,则说明Prometheus启动成功

紧接着选择Status--->Targets,看到如下信息,则说明Prometheus可以正常拉取系统应用指标数据

4.5 查看监控指标

5.Grafana

想要更丰富面板进行可视化监控,就需要引入Grafana

5.1 Docker拉取Grafana

docker pull grafana/grafana

5.2 运行Grafana

docker run --name grafana -p 3000:3000 -d 镜像id

5.3 访问Grafana

浏览器中输入:http://localhost:3000/

5.4 配置数据源

选择Prometheus数据源

填写相关配置信息

5.5 添加监控面板

添加面板可以通过在grafana.com官方网站中进行查找,复制相对应的id点击Load按钮,比如jvm监控面板SpringBoot监控面板

选择之前配置的数据源Prometheus

5.6 查看监控面板