一招搞定 Spring Boot 可视化监控!

本文介绍了如何使用Spring Boot Actuator、Micrometer、Prometheus和Grafana来实现应用程序的可视化监控。通过添加相关依赖,创建Spring Boot应用,设置Prometheus进行数据收集,以及用Grafana展示精美仪表板,实现对应用程序运行状况的实时监控。
摘要由CSDN通过智能技术生成

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
      }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值