关于java:一招搞定-Spring-Boot-可视化监控

9次阅读

共计 5549 个字符,预计需要花费 14 分钟才能阅读完成。

1、简介

当某个应用程序在生产环境中运行时,监控其运行状况是必要的。通过实时理解应用程序的运行状况,你能在问题呈现之前失去正告,也能够在客户留神到问题之前解决问题。

在本文中,咱们将创立一个 Spring Boot 应用程序,在 Spring Actuator,Micrometer,Prometheus 和 Grafana 的帮忙下来监控零碎。其中,Spring Actuator 和 Micrometer 是 Spring Boot App 的一部分。

简要阐明了不同组件的目标:

  • Spring Actuator:在应用程序里提供泛滥 Web 接口,通过它们理解利用程序运行时的外部情况。无关更多信息,请参见 Spring Boot 2.0 中的 Spring Boot Actuator。
  • Micrometer:为 Java 平台上的性能数据收集提供了一个通用的 API,它提供了多种度量指标类型(Timers、Guauges、Counters 等),同时反对接入不同的监控零碎,例如 Influxdb、Graphite、Prometheus 等。Spring Boot Actuator 对此提供了反对。
  • Prometheus:一个工夫序列数据库,用于收集指标。
  • Grafana:用于显示指标的仪表板。

上面,咱们将别离介绍每个组件。本文中应用的代码存档在 GitHub 上。

2、创立示例利用

首先要做的是创立一个能够监控的应用程序。通过Spring Initializr,并增加 Spring Boot Actuator,Prometheus 和 Spring Web 依赖项,咱们创立了一个如下所示的 Spring MVC 应用程序。

@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/endPoint1Metrics for endPoint1$ curl http://localhost:8080/endPoint2Metrics for endPoint2

验证 Spring Actuator 接口。为了使响应信息不便可读,咱们通过 python -mjson.tool 来格式化信息。

$ 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

重启应用程序,拜访 http://localhost:8080/actuato… 从 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为最风行的监控零碎提供了一个简略的仪表板,容许仪表化 JVM 利用,而无需关怀是哪个供应商提供的指标。它的作用和 SLF4J 相似,只不过它关注的不是 Logging(日志),而是 application metrics(利用指标)。简而言之,它就是利用监控界的 SLF4J。

Spring Boot Actuator 为 Micrometer 提供了主动配置。Spring Boot2 在 spring-boot-actuator 中引入了 micrometer,对 1.x 的 metrics 进行了重构,另外反对对接的监控零碎也更加丰盛(AtlasDatadogGangliaGraphiteInfluxJMXNewRelicPrometheusSignalFxStatsDWavefront)。

更新后的 application.properties 文件如下所示:

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

重启应用程序,并从 http://localhost:8080/actuator/metrics 中检索数据。

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

能够间接通过指标名来检索具体信息。例如,如果查问 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
        },
...

3、增加 Prometheus

Prometheus 是 Cloud Native Computing Foundation 的一个开源监控零碎。因为咱们的应用程序中有一个 /actuator/Prometheus 端点来供 Prometheus 抓取数据,因而你当初能够配置 Prometheus 来监控你的 Spring Boot 应用程序。

Prometheus 有几种装置办法,在本文中,咱们将在 Docker 容器中运行 Prometheus。

你须要创立一个 prometheus.yml 文件,以增加到 Docker 容器中。

global:
scrape_interval:15s

scrape_configs:
- job_name: 'myspringmetricsplanet'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['HOST:8080']
  • scrape_interval:Prometheus 多久轮询一次应用程序的指标
  • job_name:轮询工作名称
  • metrics_path:指标的 URL 的门路
  • targets:主机名和端口号。应用时,替换 HOST 为主机的 IP 地址

如果在 Linux 上查找 IP 地址有艰难,则能够应用以下命令:

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

启动 Docker 容器并将本地 prometheus.yml 文件,映射到 Docker 容器中的文件。

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

胜利启动 Docker 容器后,首先验证 Prometheus 是否可能通过 http://localhost:9090/targets收集数据。

如上图所示,咱们遇到 context deadline exceeded 谬误,造成 Prometheus 无法访问主机上运行的 Spring Boot 应用程序。如何解决呢?

能够通过将 Docker 容器增加到你的主机网络来解决此谬误,这将使 Prometheus 可能拜访 Spring Boot 应用程序。

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

再次验证,状态批示为 UP。

当初能够显示 Prometheus 指标。通过拜访 http://localhost:9090/graph,在搜寻框中输出http_server_requests_seconds_max 并单击“执行”按钮,将为你提供申请期间的最长执行工夫。

4、增加 Grafana

最初增加的组件是 Grafana。只管 Prometheus 能够显示指标,但 Grafana 能够帮忙你在更精美的仪表板中显示指标。Grafana 也反对几种装置形式,在本文中,咱们也将在 Docker 容器中运行它。

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

点击 http://localhost:3000/,就能够拜访 Grafana。

默认的用户名 / 明码为admin/admin。单击“登录”按钮后,你须要更改默认明码。

接下来要做的是增加一个数据源。单击左侧边栏中的“配置”图标,而后抉择“Data Sources(数据源)”。

单击Add data source(增加数据源)按钮。

Prometheus 在列表的顶部,抉择 Prometheus。

填写可拜访 Prometheus 的 URL,将 HTTP Access 设置为 Browser,而后单击页面底部的Save&Test 按钮。

一切正常后,将显示绿色的告诉标语,批示数据源正在工作。

当初该创立仪表板了。你能够自定义一个,但也能够应用开源的仪表板。用于显示 Spring Boot 指标的一种罕用仪表板是 JVM 仪表板。

在左侧边栏中,点击 + 号,而后抉择导入。

输出 JVM 仪表板的 URL https://grafana.com/grafana/d…,而后单击“Load(加载)”按钮。

为仪表板输出一个有意义的名称(例如 MySpringMonitoringPlanet),抉择 Prometheus 作为数据源,而后单击 Import 按钮。

到目前为止,你就能够应用一个很酷的 Grafana 仪表板。

也能够将自定义面板增加到仪表板。在仪表板顶部,单击 Add panel(增加面板)图标。

单击 Add new panel(增加新面板)。

在 Metrics 字段中,输出 http_server_requests_seconds_max,在右侧栏中的 Panel title 字段中,能够输出面板的名称。

最初,单击右上角的 Apply 按钮,你的面板将增加到仪表板。不要遗记保留仪表板。

为应用程序设置一些负载,并查看仪表板上的 http_server_requests_seconds_max 指标产生了什么。

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

5、论断

在本文中,咱们学习了如何为 Spring Boot 应用程序增加一些根本监控。这非常容易,只须要通过将 Spring Actuator,Micrometer,Prometheus 和 Grafana 组合应用。

当然,这只是一个终点,然而从这里开始,你能够为 Spring Boot 应用程序扩大和配置更多、更具体的指标。

原文:https://mydeveloperplanet.com…

译文:https://www.kubernetes.org.cn…

近期热文举荐:

1.1,000+ 道 Java 面试题及答案整顿(2021 最新版)

2. 别在再满屏的 if/ else 了,试试策略模式,真香!!

3. 卧槽!Java 中的 xx ≠ null 是什么新语法?

4.Spring Boot 2.5 重磅公布,光明模式太炸了!

5.《Java 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0