如果您正在为您的业务构建网站,您可能需要添加一些管理服务。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 中)调整其设置,请参阅有关端点的文档。