Spring Boot Starter Actuator 监控 Spring Boot 应用

Spring Boot Actuator About 4,497 words

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

根节点

http://localhost:8080/actuator

输出

{"status":"UP"}

如果需要显示详细信息,在yaml中配置:

management:
  endpoint:
    health:
      show-details: always

输出

{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":2000263573504,"free":1985721139200,"threshold":10485760,"exists":true}},"ping":{"status":"UP"}}}

包含所有指标

management:
  endpoints:
    web:
      exposure:
        include: "*"

健康检测

开启探针检测节点

management:
  endpoint:
    health:
      probes:
        enabled: true

生效节点

存活性探针:/health/liveness

可读性探针:/health/readiness

备注:不会受dbmail等服务的影响,如果mail服务宕机了,主节点/health会返回503状态码,且接口耗时超过10秒,而探针节点则会很快返回200状态码。

http://localhost:8080/actuator/health/liveness

http://localhost:8080/actuator/health/readiness

logger 指标

查看所有 logger

http://localhost:8080/actuator/loggers

输出

{
    "levels": [
        "OFF",
        "ERROR",
        "WARN",
        "INFO",
        "DEBUG",
        "TRACE"
    ],
    "loggers": {
        "ROOT": {
            "configuredLevel": "INFO",
            "effectiveLevel": "INFO"
        },
        "_org.springframework": {
            "configuredLevel": null,
            "effectiveLevel": "INFO"
        },
        "com.example": {
            "configuredLevel": null,
            "effectiveLevel": "INFO"
        }
    }
}

查看指定 logger

ROOT更换成对应名称即可。

http://localhost:9090/actuator/loggers/ROOT

输出

{"configuredLevel":"INFO","effectiveLevel":"INFO"}

动态修改日志级别

com.example包下的日志级别调整至debug级别。

curl -X POST --location "http://localhost:9090/actuator/loggers/com.example.actuator" \
    -H "Content-Type: application/vnd.spring-boot.actuator.v3+json" \
    -d "{
          \"configuredLevel\": \"debug\"
        }"

还原日志级别,设置configuredLevelnull即可。

curl -X POST --location "http://localhost:9090/actuator/loggers/com.example.actuator" \
    -H "Content-Type: application/vnd.spring-boot.actuator.v3+json" \
    -d "{
          \"configuredLevel\": null
        }"

info 节点

开启 info

management:
  info:
    os:
      enabled: true
    env:
      enabled: true
    java:
      enabled: true

请求接口

节点URL

http://localhost:8080/actuator/info

输出

{"os":{"name":"Windows 10","version":"10.0","arch":"amd64"}}

自定义 info

配置文件中添加info节点。(info为固定写法,app这些属性都可以自定义。)

info:
  app:
    encoding: "UTF-8"
    java:
      source: "11"
      target: "11"

输出

{"app":{"encoding":"UTF-8","java":{"source":"11","target":"11"}},"java":{"version":"11.0.2","vendor":{"name":"Oracle Corporation","version":"18.9"},"runtime":{"name":"OpenJDK Runtime Environment","version":"11.0.2+9"},"jvm":{"name":"OpenJDK 64-Bit Server VM","vendor":"Oracle Corporation","version":"11.0.2+9"}},"os":{"name":"Windows 10","version":"10.0","arch":"amd64"}}

metrics 节点

可使用以下节点名称查看节点的metrics

如:http://localhost:8080/actuator/metrics/http.server.requests

{
    "names": [
        "application.ready.time",
        "application.started.time",
        "disk.free",
        "disk.total",
        "executor.active",
        "executor.completed",
        "executor.pool.core",
        "executor.pool.max",
        "executor.pool.size",
        "executor.queue.remaining",
        "executor.queued",
        "http.server.requests",
        "jvm.buffer.count",
        "jvm.buffer.memory.used",
        "jvm.buffer.total.capacity",
        "jvm.classes.loaded",
        "jvm.classes.unloaded",
        "jvm.gc.live.data.size",
        "jvm.gc.max.data.size",
        "jvm.gc.memory.allocated",
        "jvm.gc.memory.promoted",
        "jvm.gc.overhead",
        "jvm.gc.pause",
        "jvm.memory.committed",
        "jvm.memory.max",
        "jvm.memory.usage.after.gc",
        "jvm.memory.used",
        "jvm.threads.daemon",
        "jvm.threads.live",
        "jvm.threads.peak",
        "jvm.threads.states",
        "logback.events",
        "process.cpu.usage",
        "process.start.time",
        "process.uptime",
        "system.cpu.count",
        "system.cpu.usage",
        "tomcat.sessions.active.current",
        "tomcat.sessions.active.max",
        "tomcat.sessions.alive.max",
        "tomcat.sessions.created",
        "tomcat.sessions.expired",
        "tomcat.sessions.rejected"
    ]
}

开启 shutdown 节点

请求该节点则会关闭Spring Boot应用。

management:
  endpoint:
    shutdown:
      enabled: true

参考

https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html

https://docs.spring.io/spring-boot/docs/current/actuator-api/htmlsingle

Views: 1,156 · Posted: 2022-09-22

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh