执行器
Spring Boot 包含了 Spring Boot Actuator。本节将解答在使用过程中经常出现的问题。
更改 Actuator 端点的 HTTP 端口或地址
在独立应用程序中,Actuator HTTP 端口默认与主 HTTP 端口相同。要使应用程序在不同的端口上监听,可以设置外部属性:management.server.port
。要在一个完全不同的网络地址上监听(例如,当你有一个用于管理的内部网络和一个用于用户应用程序的外部网络时),你还可以将 management.server.address
设置为服务器能够绑定的有效 IP 地址。
更多详情,请参阅 ManagementServerProperties 源码以及“生产就绪特性”部分中的自定义管理服务器端口。
自定义脱敏处理
为了控制数据的清理过程,您可以定义一个 SanitizingFunction Bean。调用该函数时会传入一个 SanitizableData 对象,该对象提供了对键、值以及它们来源的 PropertySource 的访问。这使您能够实现例如对来自特定属性源的每个值进行清理的功能。每个 SanitizingFunction 会按顺序被调用,直到某个函数改变了可清理数据的值为止。
将健康指标映射到 Micrometer 指标
以下示例展示了编写此类导出器的一种方式:
- 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
}
}