Spring Boot 可视化监控

本文涉及的产品
多模态交互后付费免费试用,全链路、全Agent
简介: 本文介绍了如何通过Spring Actuator、Micrometer、Prometheus和Grafana为Spring Boot应用程序添加监控功能。首先创建了一个Spring Boot应用,并配置了Spring Actuator以暴露健康状态和指标接口。接着,利用Micrometer收集应用性能数据,并通过Prometheus抓取这些数据进行存储。最后,使用Grafana将Prometheus中的数据可视化,展示在精美的仪表板上。整个过程简单易行,为Spring Boot应用提供了基本的监控能力,同时也为后续扩展更详细的监控指标奠定了基础。

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:用于显示指标的仪表板。

下面,将分别介绍每个组件。

2、创建示例应用

首先要做的是创建一个可以监控的应用程序。通过Spring Initializr,并添加Spring Boot Actuator,Prometheus和Spring Web依赖项, 创建了一个如下所示的Spring MVC应用程序。

  1. @RestController
  2. public class MetricsController {
  3.    @GetMapping("/endPoint1")
  4.    public String endPoint1() {
  5.        return "Metrics for endPoint1";
  6.    }
  7.    @GetMapping("/endPoint2")
  8.    public String endPoint2() {
  9.        return "Metrics for endPoint2";
  10.    }
  11. }

启动应用程序:

  1. $ mvn spring-boot:run

验证接口是否正常:

  1. $ curl http://localhost:8080/endPoint1Metrics for endPoint1$ curl http://localhost:8080/endPoint2Metrics for endPoint2

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

  1. $ curl http://localhost:8080/actuator | python -mjson.tool
  2. ...
  3. {
  4. "_links":{
  5. "self":{
  6. "href":"http://localhost:8080/actuator",
  7. "templated":false
  8.      },
  9. "health":{
  10. "href":"http://localhost:8080/actuator/health",
  11. "templated":false
  12.      },
  13. "health-path":{
  14. "href":"http://localhost:8080/actuator/health/{*path}",
  15. "templated":true
  16.      },
  17. "info":{
  18. "href":"http://localhost:8080/actuator/info",
  19. "templated":false
  20.      }
  21.   }
  22. }

默认情况下,会显示以上信息。除此之外,Spring Actuator可以提供更多信息,但是需要启用它。为了启用Prometheus,需要将以下信息添加到application.properties文件中。

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

重启应用程序,访问http://localhost:8080/actuator/prometheus从Prometheus拉取数据,返回了大量可用的指标信息。这里只显示输出的一小部分,因为它是一个很长的列表。

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

如前所述,还需要Micrometer。Micrometer为最流行的监控系统提供了一个简单的仪表板,允许仪表化JVM应用,而无需关心是哪个供应商提供的指标。它的作用和SLF4J类似,只不过它关注的不是Logging(日志),而是application metrics(应用指标)。简而言之,它就是应用监控界的SLF4J。

Spring Boot Actuator为Micrometer提供了自动配置。Spring Boot2在spring-boot-actuator中引入了micrometer,对1.x的metrics进行了重构,另外支持对接的监控系统也更加丰富(Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront)。

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

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

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

  1. $ curl http://localhost:8080/actuator/metrics | python -mjson.tool
  2. ...
  3. {
  4. "names": [
  5. "http.server.requests",
  6. "jvm.buffer.count",
  7. "jvm.buffer.memory.used",
  8. ...

可以直接通过指标名来检索具体信息。例如,如果查询http.server.requests指标,可以按以下方式检索:

  1. $ curl http://localhost:8080/actuator/metrics/http.server.requests | python -mjson.tool
  2. ...
  3. {
  4. "name": "http.server.requests",
  5. "description": null,
  6. "baseUnit": "seconds",
  7. "measurements": [
  8.        {
  9. "statistic": "COUNT",
  10. "value": 3.0
  11.        },
  12.        {
  13. "statistic": "TOTAL_TIME",
  14. "value": 0.08918682
  15.        },
  16. ...

3、添加Prometheus

Prometheus是Cloud Native Computing Foundation的一个开源监控系统。由于应用程序中有一个/actuator/Prometheus端点来供 Prometheus 抓取数据,因此现在可以配置Prometheus来监控Spring Boot应用程序。

Prometheus有几种安装方法,在本文中,将在Docker容器中运行Prometheus。

需要创建一个prometheus.yml文件,以添加到Docker容器中。

  1. global:
  2.    scrape_interval: 15s
  3. scrape_configs:
  4.    - job_name: 'myspringmetricsplanet'
  5.    metrics_path: '/actuator/prometheus'
  6.    static_configs:
  7.    - targets: ['HOST:8080']
  • scrape_interval:Prometheus多久轮询一次应用程序的指标
  • job_name:轮询任务名称
  • metrics_path:指标的URL的路径
  • targets:主机名和端口号。使用时,替换HOST为主机的IP地址

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

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

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

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

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

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

可以通过将Docker容器添加到主机网络来解决此错误,这将使Prometheus能够访问Spring Boot应用程序。

  1. $ docker run \
  2.    --name prometheus \
  3.    --network host \
  4.    -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
  5.    -d \
  6.    prom/prometheus

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

现在可以显示Prometheus指标。通过访问http://localhost:9090/graph,在搜索框中输入http_server_requests_seconds_max并单击“执行”按钮,将提供请求期间的最长执行时间。

4、添加Grafana

最后添加的组件是Grafana。尽管Prometheus可以显示指标,但Grafana可以在更精美的仪表板中显示指标。Grafana也支持几种安装方式,也将在Docker容器中运行它。

  1. $ 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/dashboards/4701,然后单击“Load(加载)”按钮。

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

到目前为止,就可以使用一个很酷的Grafana仪表板。

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

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

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

最后,单击右上角的Apply 按钮,面板将添加到仪表板。不要忘记保存仪表板。

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

  1. $ 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应用程序扩展和配置更多、更具体的指标。


目录
打赏
0
2
2
1
12
分享
相关文章
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
如何全面监控所有的 Spring Boot 微服务
如何全面监控所有的 Spring Boot 微服务
342 3
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
这篇文章详细介绍了如何将Spring Boot与Dubbo和Zookeeper整合,并通过Dubbo管理界面监控服务注册情况。
680 0
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
springBoot:actuator&admin 图形可视化&spring 打包 (七)
本文介绍了Spring Boot Actuator及其图形化管理界面Spring Boot Admin的使用方法,包括依赖导入、服务端与客户端配置、以及如何打包为JAR和WAR文件并部署。通过这些步骤,可以实现应用的监控和管理功能。
407 0
监控与追踪:如何利用Spring Cloud Sleuth和Netflix OSS工具进行微服务调试
监控与追踪:如何利用Spring Cloud Sleuth和Netflix OSS工具进行微服务调试
153 1
【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控
springboot的热部署、配置的宽松绑定和校验、任务、邮件、监控、springboot整合JdbcTemplate,h2等sql技术、整合redis,mongodb,es等nosql技术、整合redis,Memcached,jetcache,j2cache等缓存技术、整合ActiveMQ,RabbitMQ,RocketMQ,Kafka等消息的中间件的入门、整合缓存/任务/邮件/监控
【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控
Spring Boot Actuator:守护你的应用心跳,让监控变得触手可及!
【8月更文挑战第31天】Spring Boot Actuator 是 Spring Boot 框架的核心模块之一,提供了生产就绪的特性,用于监控和管理 Spring Boot 应用程序。通过 Actuator,开发者可以轻松访问应用内部状态、执行健康检查、收集度量指标等。启用 Actuator 需在 `pom.xml` 中添加 `spring-boot-starter-actuator` 依赖,并通过配置文件调整端点暴露和安全性。Actuator 还支持与外部监控工具(如 Prometheus)集成,实现全面的应用性能监控。正确配置 Actuator 可显著提升应用的稳定性和安全性。
425 0
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
755 6
深入实践springboot实战 蓄势待发 我不是雷锋 我是知识搬运工
springboot,说白了就是一个集合了功能的大类库,包括springMVC,spring,spring data,spring security等等,并且提供了很多和可以和其他常用框架,插件完美整合的接口(只能说是一些常用框架,基本在github上能排上名次的都有完美整合,但如果是自己写的一个框架就无法实现快速整合)。
Neo4j【付诸实践 01】SpringBoot集成报错org.neo4j.driver.exceptions.ClientException:服务器不支持此驱动程序支持的任何协议版本(解决+源代码)
Neo4j【付诸实践 01】SpringBoot集成报错org.neo4j.driver.exceptions.ClientException:服务器不支持此驱动程序支持的任何协议版本(解决+源代码)
655 1
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等