0


Spring actuator 集成 Prometheus

1、大致思路

  1. 集成Prometheus,必须要实现以下两点。
  2. 一、A客户端想要集成Prometheus,那A客户端就必须暴露一个能够供外部正常访问的接口,并将接口定义在Prometheus的配置文件中,由Prometheus Service去定时向这个接口发起请求,拉取(pull)数据。
  3. 二、只有符合Prometheus要求的数据规格,Prometheus Service才会将数据保存到内置的时序数据库中。

2、Spring Boot Actuator简单说明

  1. 首先,springboot自带的Actuator就具有健康检查(Health)、度量信息(Metrics)、环境信息等功能,只需要暴露指定端点就可以访问上述功能。

常见的 Actuator 端点:

  1. /actuator/health:健康检查端点
  2. /actuator/env:环境信息端点
  3. /actuator/info:应用程序信息端点
  4. /actuator/prometheus:暴露给 Prometheus 的监控数据端点(需要集成 Micrometer

3、引入maven依赖

除了必须要引入Spring-Actuator的依赖包以外,还必须要引入一个第三方依赖

Spring官网给出的解释

  1. Spring虽然对外提供了给Prometheus拉取数据用的端点,但是,还必须要把Spring收集到的数据转化成Prometheus可以拉取的数据规格(数据格式)。因此,就需要用上“**micrometer-registry-prometheus**”这个第三方依赖来将数据进行格式化。
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.7.17</version>
  5. </parent>
  6. <dependencies>
  7. <!-- Spring Boot Web的启动器 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <!-- spring的Actuator-->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-actuator</artifactId>
  16. </dependency>
  17. <!--这个依赖可以实现将actuator的数据转换成Prometheus可以识别的数据类型-->
  18. <dependency>
  19. <groupId>io.micrometer</groupId>
  20. <artifactId>micrometer-registry-prometheus</artifactId>
  21. </dependency>
  22. </dependencies>

4、定义配置文件

4.1 application.yml文件

  1. # 定义项目的名称
  2. application:
  3. name: UMR-Spring-Boot-Env
  4. # 项目启动使用的环境
  5. profiles:
  6. active: dev

4.2 application-dev.yml文件

  1. server:
  2. #项目启动的端口号
  3. port: 9180
  4. spring:
  5. config:
  6. import:
  7. - application-common.yml

4.3 application-common.yml文件(核心文件

  1. management:
  2. #默认和请求接口的端口一致,这里指定一个新的端口号,防止被人猜到
  3. server:
  4. port: 8686
  5. endpoints:
  6. web:
  7. #对外暴露actuator的哪些监控数据,注意单词不要写错了,写错了外部是无法访问接口的
  8. exposure:
  9. include:
  10. - health
  11. - prometheus
  12. endpoint:
  13. # 启用之后,才能够正常调用 /actuator/prometheus 接口
  14. prometheus:
  15. enabled: true
  16. #健康检查
  17. health:
  18. #总是展示健康检查的详细信息
  19. show-details: always
  20. metrics:
  21. tags:
  22. #应用程序名称,这个配置建议加上,Grafana官方提供的面板配置很多用了这个label
  23. application: ${spring.application.name}
  24. # 配置完成之后,启动项目
  25. # 就可以正常访问 /actuator/health接口 或者 /actuator/prometheus ,因为目前只暴露了这两个接口

5、 定义测试接口

  1. 这里就不占用过多篇幅去写启动类以及Service层的代码,并且更严谨的方式是**定义一个切面来进行监控埋点**,这里只是简单的Demo
  1. import com.tedu.main.core.api.dto.Result;
  2. import com.tedu.main.core.api.dto.StudentInfoRequestDTO;
  3. import com.tedu.main.core.service.StuService;
  4. import io.micrometer.core.instrument.Metrics;
  5. import io.micrometer.core.instrument.Tags;
  6. import org.springframework.web.bind.annotation.*;
  7. import java.util.concurrent.TimeUnit;
  8. @RestController
  9. public class RegistryController {
  10. private final StuService service;
  11. public RegistryController(StuService service) {
  12. this.service = service;
  13. }
  14. @PostMapping("/registryStu")
  15. public Result registryStu(@RequestBody StudentInfoRequestDTO dto) {
  16. long startTime = System.currentTimeMillis();
  17. long end;
  18. String code;
  19. try {
  20. service.registry();
  21. end = System.currentTimeMillis();
  22. code = "S00000";
  23. } catch (Exception e) {
  24. end = System.currentTimeMillis();
  25. code = "B12345";
  26. }
  27. // counter() 方法增加一个计数器指标(Metrics),记录请求接口的次数
  28. // UMR_RegistryStu_Request_Count 是指标名称
  29. // Tags.of() 定义的是标签内容
  30. Metrics.counter("UMR_RegistryStu_Request_Count",
  31. Tags.of("method", "/registryStu")
  32. .and("currentLoginUser", dto.getStuName())
  33. .and("code", code)
  34. ).increment();
  35. long totalTime = end - startTime;
  36. // UMR_RegistryStu_Request_Time 是指标名称
  37. Metrics.timer("UMR_RegistryStu_Request_Time",
  38. Tags.of("function", "registryStu")
  39. .and("currentLoginUser", dto.getStuName()))
  40. .record(totalTime, TimeUnit.MILLISECONDS);
  41. return new Result(dto.getStuName(), code, null);
  42. }
  43. @PostMapping("/hello")
  44. public String hello(StudentInfoRequestDTO dto) {
  45. // UMR_RegistryStu_Request_Count 是指标名称
  46. Metrics.counter("UMR_RegistryStu_Request_Count",
  47. Tags.of("method", "hello-API")
  48. .and("currentLoginUser", dto.getStuName())
  49. .and("code", "S000")
  50. ).increment();
  51. return "hello world";
  52. }
  53. }

6、 通过Postman请求

注意,获取采集数据的请求端口号是8686,并不是请求接口的9180端口

对_count数据继续说明

UMR_RegistryStu_Request_Time_seconds_count{application="UMR-Spring-Boot-Env",currentLoginUser="李四",function="registryStu",} 6.0

UMR_RegistryStu_Request_Time_seconds_count 是指标名称。

"UMR_RegistryStu_Request_Time"这块是我们自定义的,"_seconds_count"是引入的第三方组件组件帮我们生成的,"_count"结尾表示的是请求总数。

{ }括号里的是标签,除了application的值是从配置文件中获取的,其他的标签内容全是在代码中定义的。

6.0 是表示请求registryStu接口,“李四”用户总共访问了6次。(包含错误和失败)

对_sum数据进行说明

UMR_RegistryStu_Request_Time_seconds_sum{application="UMR-Spring-Boot-Env",currentLoginUser="李四",function="registryStu",} 14.113

"_sum"表示的是请求总的响应时间,6次请求总耗时14.113秒

对_max进行说明

UMR_RegistryStu_Request_Time_seconds_max{application="UMR-Spring-Boot-Env",currentLoginUser="李四",function="registryStu",} 5.013

"_max"表示某次请求最大的一次耗时5.013秒

对_total数据进行说明

李四请求了6次,其中5次请求返回的状态码是"S00000",1次请求返回的状态码是"B12345"

TYPE UMR_RegistryStu_Request_Count_total counter

UMR_RegistryStu_Request_Count_total{application="UMR-Spring-Boot-Env",code="S00000",currentLoginUser="李四",method="/registryStu",} 5.0
UMR_RegistryStu_Request_Count_total{application="UMR-Spring-Boot-Env",code="B12345",currentLoginUser="李四",method="/registryStu",} 1.0

7、Prometheus服务发现

使用静态配置,将Actuator暴露的采集接口/actuator/prometheus,更新到prometheus.yml配置文件中

移动到Prometheus安装目录,启动Prometheus

Windows:执行prometheus.exe文件

Linux系统:./prometheus --config.file=prometheus.yml

Prometheus默认的启动端口是9090,可以通过命令修改默认启动端口

请求地址:http://localhost:9090/

8、查看Prometheus控制台

也可以通过在Graph搜索定义的指标名称,查看具体情况

组件自带的部分监控指标

http_server_requests_seconds_count:请求数
http_server_requests_seconds_max :http请求数峰值


本文转载自: https://blog.csdn.net/qq_54655539/article/details/142180898
版权归原作者 _UMR_ 所有, 如有侵权,请联系我们删除。

“Spring actuator 集成 Prometheus”的评论:

还没有评论