如何借助spring-boot-actuator排查启动耗时的bean

基础介绍

Spring Boot Actuator 包含许多附加功能,可帮助您在应用程序投入生产时对其进行监控和管理。您可以选择使用 HTTP 或 JMX 端点来管理和监控您的应用程序。审计、健康和指标收集可以自动应用于您的应用程序

使用

引入

对应springboot程序来说很简单,只需要引入以下的依赖即可

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

yaml配置

一定要*或者指定启动端点startup,不然访问404

默认情况下,除了shutdown启用之外的所有端点。要配置端点的启用,请使用其management.endpoint.<id>.enabled属性。以下示例启用shutdown等端点:

management:
  endpoint:
    startup:
      enabled: true
    info:
      enabled: true
    shutdown:
      enabled: true
    beans:
      enabled: true
    mappings:
      enabled: true

禁用的端点完全从应用程序上下文中删除。如果您只想更改暴露端点的技术,请改用includeexclude属性。

需要注意的是,在yaml中 号比较特殊,因此配置号,请使用 双引号

management:
  endpoint:
    startup:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"

如果是properties直接下面的

management.endpoints.web.exposure.include=*

修改启动类

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.setApplicationStartup(new BufferingApplicationStartup(2048));
        application.run(args);
    }

}

访问端点监控信息

127.0.0.1:19998/actuator/startup

寻找时间最长的

官方文档指南

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

2 个赞