文章

SpringBoot-收集指标

模块: SpringBoot Actuator

SpringBoot Actuator 其底层使用了 Mircometer ,可度量 SpringBoot 应用和获取它的各项指标,可通过 HTTP 或 JMX 来调用 Actuator 暴露的各种端点,然后就可以获取一个正在运行中的应用的内部状态

当然内部指标并不是所有都可以向外暴露的,所以我们得有选择的开放,或者加入权限校验之后才能获取如下内容:

1
2
3
4
5
有那些可配置的属性
各依赖包的日志级别
占用了多少内存HTTP
埋点被请求了多少次
应用本身以及协作的外部服务的健康状态

1. 添加依赖

1
2
3
4
5
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.4.3</version>
</dependency>

2. 基础配置

1
2
3
4
5
6
7
8
9
management:
  server:
    port: 9090							# 一般启动独立端口(默认和应用端口一致),启用后源端口不可查
  endpoints:
    web:
      base-path: /actuator				# 默认前缀路径,可修改
      exposure:
        include: health,info,metrics	# 向外暴露的端点,可用通配符('*',需要单引号)
        exclude: env,heapdump			# 排除暴露的端点

3. 查看可消费的端点

可选用 HTTP 访问 localhost:9090/actuator 来获取 HATEOAS(可简单理解为暴露的端点文档),它是所有可暴露端点的地图,可通过属性对应的地址来获取的指标,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "_links": {
    "self": {
      "href": "http://localhost:9090/actuator",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:9090/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "http://localhost:9090/actuator/health",
      "templated": false
    },
    "info": {
      "href": "http://localhost:9090/actuator/info",
      "templated": false
    }
  }
}
本文由作者按照 CC BY 4.0 进行授权