如果您正在為您的業務構建網站,您可能需要新增一些管理服務。Spring Boot 透過其 actuator 模組提供了幾個此類服務(例如健康、審計、bean 等)。
如果您使用 Gradle,請將以下依賴項新增到您的 build.gradle(.kts) 檔案中
Groovy
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Kotlin
implementation("org.springframework.boot:spring-boot-starter-actuator")
如果您使用 Maven,請將以下依賴項新增到您的 pom.xml 檔案中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然後重新啟動應用程式。如果您使用 Gradle,請在終端視窗中執行以下命令
如果您使用 Maven,請在終端視窗中執行以下命令
您應該會看到應用程式已添加了一組新的 RESTful 端點。這些是 Spring Boot 提供的管理服務。以下清單顯示了典型的輸出
management.endpoint.configprops-org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationPropertiesReportEndpointProperties
management.endpoint.env-org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointProperties
management.endpoint.health-org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties
management.endpoint.logfile-org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointProperties
management.endpoints.jmx-org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointProperties
management.endpoints.web-org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties
management.endpoints.web.cors-org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties
management.health.diskspace-org.springframework.boot.actuate.autoconfigure.system.DiskSpaceHealthIndicatorProperties
management.info-org.springframework.boot.actuate.autoconfigure.info.InfoContributorProperties
management.metrics-org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties
management.metrics.export.simple-org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties
management.server-org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties
|
還有一個 /actuator/shutdown 端點,但預設情況下,它只能透過 JMX 可見。要將其作為 HTTP 端點啟用,請將 management.endpoint.shutdown.enabled=true 新增到您的 application.properties 檔案中,並透過 management.endpoints.web.exposure.include=health,info,shutdown 暴露它。但是,您可能不應為公開可用的應用程式啟用 shutdown 端點。 |
$ curl https://:8080/actuator/health
{"status":"UP"}
您還可以嘗試透過 curl 呼叫 shutdown,以檢視當您未將必要的行(如前面的註釋所示)新增到 application.properties 時會發生什麼
$ curl -X POST https://:8080/actuator/shutdown
{"timestamp":1401820343710,"error":"Not Found","status":404,"message":"","path":"/actuator/shutdown"}
由於我們沒有啟用它,所以請求的端點不可用(因為該端點不存在)。
有關這些 REST 端點中的每一個的更多詳細資訊以及如何使用 application.properties 檔案(在 src/main/resources 中)調整其設定,請參閱有關端點的文件。