此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1! |
Spring Boot 包括 Spring Boot 执行器。 本节回答了经常因使用而产生的问题。
更改执行器终端的 HTTP 端口或地址
在独立应用程序中,执行器 HTTP 端口默认与主 HTTP 端口相同。
若要使应用程序侦听其他端口,请设置外部属性: 。
若要侦听完全不同的网络地址(例如,当内部网络用于管理,外部网络用于用户应用程序时),还可以设置为服务器能够绑定到的有效 IP 地址。management.server.port
management.server.address
有关详细信息,请参阅 ManagementServerProperties
源代码和“生产就绪功能”部分中的“自定义管理服务器端口”。
自定义“白标”错误页面
Spring Boot 会安装一个“白标”错误页面,如果您遇到服务器错误,您将在浏览器客户端中看到该页面(使用 JSON 和其他媒体类型的计算机客户端应该看到带有正确错误代码的合理响应)。
设置为关闭默认错误页面。
这样做将恢复您正在使用的 servlet 容器的缺省值。
请注意,Spring Boot 仍会尝试解决错误视图,因此您可能应该添加自己的错误页面,而不是完全禁用它。server.error.whitelabel.enabled=false |
用你自己的错误页面覆盖取决于你使用的模板技术。
例如,如果您使用 Thymeleaf,则可以添加模板。
如果您使用 FreeMarker,则可以添加模板。
通常,您需要一个使用名称 解析的路径或处理路径的 a。
除非您替换了一些默认配置,否则您应该在 中找到 ,因此 命名 将是一种方法。
有关更多选项,请参阅 ErrorMvcAutoConfiguration
。error.html
error.ftlh
View
error
@Controller
/error
BeanNameViewResolver
ApplicationContext
@Bean
error
另请参阅“错误处理”部分,了解如何在 servlet 容器中注册处理程序的详细信息。
设置为关闭默认错误页面。
这样做将恢复您正在使用的 servlet 容器的缺省值。
请注意,Spring Boot 仍会尝试解决错误视图,因此您可能应该添加自己的错误页面,而不是完全禁用它。server.error.whitelabel.enabled=false |
自定义消毒
要控制清理,请定义一个 bean。
调用函数的函数提供对键和值以及它们的来源的访问。
例如,这允许您清理来自特定属性源的每个值。
每个函数都按顺序调用,直到函数更改可清理数据的值。SanitizingFunction
SanitizableData
PropertySource
SanitizingFunction
将运行状况指标映射到千分尺指标
Spring Boot 运行状况指示器返回一个类型来指示整体系统运行状况。
如果要监视特定应用程序的运行状况级别或发出警报,可以使用 Micrometer 将这些状态导出为指标。
默认情况下,Spring Boot 使用状态代码“UP”、“DOWN”、“OUT_OF_SERVICE”和“UNKNOWN”。
要导出这些,您需要将这些状态转换为一组数字,以便它们可以与 千分尺 .Status
Gauge
下面的示例显示了编写此类导出器的一种方法:
-
Java
-
Kotlin
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyHealthMetricsExportConfiguration {
public MyHealthMetricsExportConfiguration(MeterRegistry registry, HealthEndpoint healthEndpoint) {
// This example presumes common tags (such as the app) are applied elsewhere
Gauge.builder("health", healthEndpoint, this::getStatusCode).strongReference(true).register(registry);
}
private int getStatusCode(HealthEndpoint health) {
Status status = health.health().getStatus();
if (Status.UP.equals(status)) {
return 3;
}
if (Status.OUT_OF_SERVICE.equals(status)) {
return 2;
}
if (Status.DOWN.equals(status)) {
return 1;
}
return 0;
}
}
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry
import org.springframework.boot.actuate.health.HealthEndpoint
import org.springframework.boot.actuate.health.Status
import org.springframework.context.annotation.Configuration
@Configuration(proxyBeanMethods = false)
class MyHealthMetricsExportConfiguration(registry: MeterRegistry, healthEndpoint: HealthEndpoint) {
init {
// This example presumes common tags (such as the app) are applied elsewhere
Gauge.builder("health", healthEndpoint) { health ->
getStatusCode(health).toDouble()
}.strongReference(true).register(registry)
}
private fun getStatusCode(health: HealthEndpoint) = when (health.health().status) {
Status.UP -> 3
Status.OUT_OF_SERVICE -> 2
Status.DOWN -> 1
else -> 0
}
}