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
廣告