Spring Boot - Actuator



Spring Boot Actuator 提供安全的端点来监控和管理您的 Spring Boot 应用程序。默认情况下,所有 Actuator 端点都是安全的。在本节中,您将详细了解如何为您的应用程序启用 Spring Boot Actuator。

启用 Spring Boot Actuator

要为您的 Spring Boot 应用程序启用 Spring Boot Actuator 端点,我们需要在构建配置文件中添加 Spring Boot Starter Actuator 依赖项。

Maven 用户可以在您的 pom.xml 文件中添加以下依赖项。

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

Gradle 用户可以在您的 build.gradle 文件中添加以下依赖项。

compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'

在 application.properties 文件中,我们需要禁用 Actuator 端点的安全性。

management.security.enabled = false

YAML 文件用户可以在您的 application.yml 文件中添加以下属性。

management:
   security:
      enabled: false

如果您想使用单独的端口号来访问 Spring Boot Actuator 端点,请在 application.properties 文件中添加管理端口号。

management.port = 9000

YAML 文件用户可以在您的 application.yml 文件中添加以下属性。

management:
   port: 9000

现在,您可以创建一个可执行的 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序。

对于 Maven,您可以使用以下命令:

mvn clean install

“BUILD SUCCESS”之后,您可以在 target 目录下找到 JAR 文件。

对于 Gradle,您可以使用以下命令:

gradle clean build

“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录下找到 JAR 文件。

现在,您可以使用以下命令运行 JAR 文件:

java –jar <JARFILE> 

现在,应用程序已在 Tomcat 端口 8080 上启动。请注意,如果您指定了管理端口号,则同一应用程序将在两个不同的端口号上运行。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.3.3)[0;39m

[2024-09-12T15:56:49Z] [org.springframework.boot.StartupInfoLogger] [main] [50] [INFO ] Starting DemoApplication using Java 21.0.3 with PID 9056 (E:\Dev\demo\target\classes started by Tutorialspoint in E:\Dev\demo)
[2024-09-12T15:56:49Z] [org.springframework.boot.SpringApplication] [main] [654] [INFO ] No active profile set, falling back to 1 default profile: "default"
[2024-09-12T15:56:50Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [111] [INFO ] Tomcat initialized with port 8080 (http)
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Initializing ProtocolHandler ["http-nio-8080"]
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting service [Tomcat]
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting Servlet engine: [Apache Tomcat/10.1.28]
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Initializing Spring embedded WebApplicationContext
[2024-09-12T15:56:50Z] [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext] [main] [296] [INFO ] Root WebApplicationContext: initialization completed in 1081 ms
[2024-09-12T15:56:50Z] [org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping] [main] [59] [INFO ] Adding welcome page template: index
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting ProtocolHandler ["http-nio-8080"]
[2024-09-12T15:56:50Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 8080 (http) with context path '/'
[2024-09-12T15:56:50Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [111] [INFO ] Tomcat initialized with port 9000 (http)
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Initializing ProtocolHandler ["http-nio-9000"]
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting service [Tomcat]
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting Servlet engine: [Apache Tomcat/10.1.28]
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Initializing Spring embedded WebApplicationContext
[2024-09-12T15:56:50Z] [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext] [main] [296] [INFO ] Root WebApplicationContext: initialization completed in 76 ms
[2024-09-12T15:56:50Z] [org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver] [main] [60] [INFO ] Exposing 1 endpoint beneath base path '/actuator'
[2024-09-12T15:56:50Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting ProtocolHandler ["http-nio-9000"]
[2024-09-12T15:56:50Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 9000 (http) with context path '/'
[2024-09-12T15:56:50Z] [org.springframework.boot.StartupInfoLogger] [main] [56] [INFO ] Started DemoApplication in 2.212 seconds (process running for 3.04)
[2024-09-12T15:56:51Z] [org.apache.juli.logging.DirectJDKLog] [RMI TCP Connection(4)-127.0.0.1] [173] [INFO ] Initializing Spring DispatcherServlet 'dispatcherServlet'
[2024-09-12T15:56:51Z] [org.springframework.web.servlet.FrameworkServlet] [RMI TCP Connection(4)-127.0.0.1] [532] [INFO ] Initializing Servlet 'dispatcherServlet'
[2024-09-12T15:56:51Z] [org.springframework.web.servlet.FrameworkServlet] [RMI TCP Connection(4)-127.0.0.1] [554] [INFO ] Completed initialization in 1 ms

以下是一些重要的 Spring Boot Actuator 端点。您可以在 Web 浏览器中输入它们并监控您的应用程序行为。

端点 用法
/metrics 查看应用程序指标,例如内存使用情况、可用内存、线程、类、系统运行时间等。
/env 查看应用程序中使用的环境变量列表。
/beans 查看 Spring Bean 及其类型、作用域和依赖关系。
/health 查看应用程序健康状况
/info 查看关于 Spring Boot 应用程序的信息。
/trace 查看您的 Rest 端点的跟踪列表。

打开 https://:9000/actuator/health 检查应用程序的状态是运行还是停止。

Spring Actuator
广告
© . All rights reserved.