如何启用此处列出的 ExecutorServiceMetrics?
SpringBoot版本:2.1.2.RELEASE
在 /actuator/metrics 下,我可以看到 jvm 和其他一些开箱即用的自动配置指标,但看不到执行器指标。
我试过设置这个,但没有运气。
management:
metrics:
enable.executor: true
任何帮助表示赞赏。
我能够在 Spring Boot 2.1.2.RELEASE 应用程序中获得
ExecutorServiceMetrics
报告指标,除了创建一个受监控的 ExecutorService
bean 之外不需要做任何其他事情。我不需要在我的 application.yml
或 application.properties
中添加任何东西来完成这项工作。
例子:
@Configuration
public class ExecutorConfig {
@Bean
public ExecutorService executorService(final MeterRegistry registry) {
return ExecutorServiceMetrics.monitor(registry, Executors.newFixedThreadPool(20), "my executor", Tags.of("key", "value"));
}
}
然后,只需将您的
executorService
bean 连接到您的组件中并向该 executorService
bean 提交任务。
我发现如果你想用 Spring Boot 锁定它,你必须手动完成。我正在使用 Spring Boot 2.2.9.RELEASE。
使用“applicationTaskExecutor”bean 创建一个 ExecutorServiceMetrics bean(这样,您将获得之前配置的任何 bean 大小)。它会自动绑定。
类似的东西:
@Bean
@ConditionalOnMissingBean
public ExecutorServiceMetrics executorServiceMetrics(@Qualifier("applicationTaskExecutor") ThreadPoolTaskExecutor applicationTaskExecutor) {
return new ExecutorServiceMetrics(applicationTaskExecutor.getThreadPoolExecutor(), "applicationTaskExecutor",
Collections.emptyList());
}
这是我解决它的方法(在 kotlin 中):
@EnableAsync
@Configuration
class AsyncConfig(
private val taskExecutorBuilder: TaskExecutorBuilder,
private val meterRegistry: MeterRegistry) : AsyncConfigurer {
/**
* Add monitoring of executor using micrometer.
*/
override fun getAsyncExecutor(): Executor {
// create executor based on default spring-boot properties
val executor = taskExecutorBuilder.build()
// we need to initialize it before calling monitor
executor.initialize()
// monitor the executor (so it is available in metrics) (must be wrapped)
return ExecutorServiceMetrics.monitor(meterRegistry, executor.threadPoolExecutor, "AsyncExecutor", "async")
}
}
所以基本上:
TaskExecutorBuilder
所以执行器是根据spring.task.execution.*
属性构建的ExecutorServiceMetrics
中(来自io.micrometer.core
)以获取指标请注意,要使其正常工作,您必须返回装饰后的执行器!
在这个例子中,因为我给了一个前缀(
async
),可用的指标是:
如果你不知道一个
meterRegistry
bean是否存在,你可以尝试使用ObjectMapper
@Configuration
public class ExecutorConfig {
@Bean
public ExecutorService executorService(ObjectProvider<MeterRegistry> meterRegistryProvider) {
ExecutorService executorService = Executors.newFixedThreadPool(20);
meterRegistryProvider.ifAvailable(registry -> ExecutorServiceMetrics.monitor(registry, executorService, "my executor", Tags.of("key", "value")));
return executorService;
}
}