如何实现一个对Springboot项目的监控程序

452 阅读5分钟

一、简介

当应用程序在生产环境(以及您的其他环境)中运行时,监控其健康状况是明智之举。你想确保一切都在没有任何问题地运行,而了解这一点的唯一方法是衡量你的应用程序的健康状况。当出现问题时,您希望在客户注意到问题之前得到通知,也许您可​​以在客户注意到任何事情之前解决问题。在本文中,您将创建一个示例 Spring Boot 应用程序,您可以在 Spring Actuator、Micrometer、Prometheus 和 Grafana 的帮助下对其进行监控。这在下面的概述中可视化,其中 Spring Actuator 和 Micrometer 是 Spring Boot App 的一部分。

替代文字

简要说明不同组件的用途:

  • Spring Actuator:提供多个端点以监控您的应用程序并与之交互
  • Micrometer:一个支持众多监控系统的应用程序指标门面,Spring Boot Actuator 为其提供支持。
  • Prometheus:一个时间序列数据库,用于收集指标。
  • Grafana:用于显示指标的仪表板。

2. 创建示例应用

首先要做的是创建一个可以监控的示例应用程序。转到Spring Initializr,添加依赖项Spring Boot ActuatorPrometheus然后Spring Web。示例应用程序将是一个具有两个虚拟端点的 Spring MVC 应用程序。

RestController使用两个端点创建一个。端点仅返回一个简单的String.

@RestController
public class MetricsController {

    @GetMapping("/endPoint1")
    public String endPoint1() {
        return "Metrics for endPoint1";
    }

    @GetMapping("/endPoint2")
    public String endPoint2() {
        return "Metrics for endPoint2";
    }

}

启动应用程序:

$ mvn spring-boot:run

验证端点是否正常工作:

$ curl http://localhost:8080/endPoint1
Metrics for endPoint1
$ curl http://localhost:8080/endPoint2
Metrics for endPoint2

验证 Spring Actuator 端点。端点返回 json 中的信息。为了格式化响应以使其可读,您可以将执行器端点的输出通过管道传输到mjson.

$ curl http://localhost:8080/actuator | python -mjson.tool
...
{
   "_links":{
      "self":{
         "href":"http://localhost:8080/actuator",
         "templated":false
      },
      "health":{
         "href":"http://localhost:8080/actuator/health",
         "templated":false
      },
      "health-path":{
         "href":"http://localhost:8080/actuator/health/{*path}",
         "templated":true
      },
      "info":{
         "href":"http://localhost:8080/actuator/info",
         "templated":false
      }
   }
}

默认情况下,上述信息可用。Spring Actuator 可以提供更多信息,但您需要启用它。为了启用 Prometheus 端点,您需要将以下行添加到文件中application.properties

management.endpoints.web.exposure.include=health,info,prometheus

重新启动应用程序并从 Prometheus 端点检索数据。返回并提供大量指标。只显示了一小部分输出,因为它是一个非常长的列表。Prometheus 将使用此端点可用的信息。

$ curl http://localhost:8080/actuator/prometheus
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",cause="G1 Evacuation Pause",} 2.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="G1 Evacuation Pause",} 0.009
...

如前所述,还需要micrometer, Micrometer 可以看作是 SLF4J。Spring Boot Actuator 为 Micrometer 提供自动配置。您唯一需要做的就是在运行时类路径中添加依赖项micrometer-registry-{system},而这正是我们prometheus在创建 Spring Boot 应用程序时添加依赖项所做的。

将指标添加到文件时,也可以访问指标执行器端点application.properties

management.endpoints.web.exposure.include=health,info,metrics,prometheus

重新启动应用程序并从指标端点检索数据。

$ curl http://localhost:8080/actuator/metrics | python -mjson.tool
...
{
    "names": [
        "http.server.requests",
        "jvm.buffer.count",
        "jvm.buffer.memory.used",
...

可以通过将其添加到 URL 来检索每个单独的指标。例如,http.server.requests可以按如下方式检索参数:

$ curl http://localhost:8080/actuator/metrics/http.server.requests | python -mjson.tool
...
{
    "name": "http.server.requests",
    "description": null,
    "baseUnit": "seconds",
    "measurements": [
        {
            "statistic": "COUNT",
            "value": 3.0
        },
        {
            "statistic": "TOTAL_TIME",
            "value": 0.08918682
        },
...

Prometheus

Prometheus是云原生计算基金会开源的监控系统。由于您的应用程序中有一个为 Prometheus 提供指标的端点,您现在可以配置 Prometheus 来监控您的 Spring Boot 应用程序。可以在此处找到这样做的 Spring 文档。

如Prometheus 文档的安装部分所述,有多种安装 Prometheus 的方法。在本节中,您将在 Docker 容器中运行 Prometheus。

您需要创建一个包含基本配置的配置 prometheus.yml文件以添加到 Docker 容器中。最小的属性是:

  • scrape_interval:普罗米修斯多久轮询一次应用程序的指标端点
  • job_name: 只是投票工作的名称
  • metrics_path: 可以访问指标的 URL 的路径
  • targets:主机名和端口号。替换HOST为您主机的 IP 地址
global:
  scrape_interval:     15s

scrape_configs:
  - job_name: 'myspringmetricsplanet'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['HOST:8080']

如果在 Linux 上查找 IP 地址有困难,可以使用以下命令:

$ ip -f inet -o addr show docker0 | awk '{print $4}' | cut -d '/' -f 1

启动 docker 容器并将本地文件绑定挂载prometheus.yml到 docker 容器中的文件。上面的prometheus.yml文件可以在 目录下的 git 仓库中找到prometheus

$ docker run \
    -p 9090:9090 \
    -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

Docker容器启动成功后,首先验证Prometheus能否通过url http://localhost:9090/targets采集数据。

替代文字

Prometheus 似乎无法访问主机上运行的 Spring Boot 应用程序。context deadline exceeded提到一个错误。

可以通过将 Docker 容器添加到主机网络来解决此错误,这将使 Prometheus 能够访问 URL。因此,添加--network host为参数。--network同时删除端口映射,因为这在使用时不起作用。最后,给你的容器起个名字,这样可以更容易地启动和停止容器。该-d参数将以分离模式运行容器。

$ docker run \
    --name prometheus \
    --network host \
    -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
    -d \
    prom/prometheus

再次验证 Prometheus 目标 URL,状态指示 UP,这意味着访问指标端点的先决条件现在已满足。

替代文字

现在可以显示 Prometheus 指标。导航到http://localhost:9090/graphhttp_server_requests_seconds_max在搜索框中输入并单击执行按钮。访问 URL 几次endPoint1以产生一些流量。此参数将为您提供请求时间段内的最长执行时间。

替代文字

4.添加Grafana

最后要添加的组件是Grafana。虽然 Prometheus 能够显示指标,但 Grafana 将允许您在更精美的仪表板中显示指标。Grafana 还支持多种安装方式,但您将在Docker 容器中运行它,就像您对 Prometheus 所做的那样。

$ docker run --name grafana -d -p 3000:3000 grafana/grafana

导航到 URL http://localhost:3000/,这是可访问 Grafana 的 URL。

替代文字

默认用户名/密码是 admin/admin。单击登录按钮后,您需要更改默认密码。谷歌浏览器还会警告您有关默认用户名/密码的信息。

替代文字

接下来要做的是添加一个数据源。单击左侧边栏中的Configuration图标并选择Data Sources

替代文字

单击添加数据源按钮。

替代文字

Prometheus 在列表的顶部,选择Prometheus

替代文字

填写可以访问Prometheus的URL ,设置 HTTP访问Browser,点击页面底部的Save & Test按钮。

替代文字

当一切正常时,会显示一个绿色的通知横幅,表明数据源正在工作。

替代文字

现在是创建仪表板的时候了。您可以创建自己的一个,但也可以使用多个可用的仪表板。用于显示 Spring Boot 指标的一种流行方式是JVM 仪表板

在左侧边栏中,单击 + 号并选择Import

替代文字

输入可以找到 JVM 仪表板的URL grafana.com/grafana/das…加载按钮。

替代文字

为仪表板输入一个有意义的名称(例如MySpringMonitoringPlanet),选择Prometheus作为数据源并单击导入按钮。

替代文字

此刻,您有一个很酷的第一个 Grafana 仪表板供您使用。不要忘记向下滚动,这里有比屏幕截图中显示的更多的指标。默认范围设置为 24 小时,这在您刚启动应用程序时可能有点大。您可以在右上角更改范围。将其更改为 fe最后 30 分钟

替代文字

也可以将自定义面板添加到仪表板。在仪表板的顶部,单击添加面板图标。

替代文字

单击添加新面板

替代文字

Metrics字段中,输入http_server_requests_seconds_max,在右侧栏的Panel title字段中,您可以输入面板的名称。

替代文字

最后,单击右上角的 “应用”按钮,您的面板将添加到仪表板。 不要忘记通过添加面板图标旁边的保存仪表板图标来保存仪表板。****

为应用程序设置一些负载并查看仪表板上的指标会发生什么情况。

$ watch -n 5 curl http://localhost:8080/endPoint1
$ watch -n 10 curl http://localhost:8080/endPoint2

替代文字

5.汇总

在本文中,您了解了如何为 Spring Boot 应用程序设置一些基本监控。需要结合使用 Spring Actuator、Micrometer、Prometheus 和 Grafana,但这些都非常容易设置和配置, 那这篇文章就介绍到这里了!