英文:
Using a custom DiskSpaceHealthIndicator (Spring Boot Actuator)?
问题
我的Spring应用程序的application.yaml文件:
management:
...
endpoint:
health:
show-details: ALWAYS
info:
enabled: false
health:
diskspace:
path: "some-path"
threshold: 536870912
这将使用https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.html来执行健康检查。
我想扩展/包装org.springframework.boot.actuate.system.DiskSpaceHealthIndicator
以添加一些特定于应用程序的行为。是否有一种方法可以配置我的应用程序来使用我的自定义版本,例如com.acme.myapp.CustomDiskSpaceHealthIndicator
,而不是org.springframework.boot.actuate.system.DiskSpaceHealthIndicator
?
英文:
My spring application.yaml:
management:
...
endpoint:
health:
show-details: ALWAYS
info:
enabled: false
health:
diskspace:
path: "some-path"
threshold: 536870912
This will use the https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.html to perform the health check.
I would like to extend/wrap org.springframework.boot.actuate.system.DiskSpaceHealthIndicator
to add some application-specific behavior. Is there a way to configure my application to use my own custom version, e.g. com.acme.myapp.CustomDiskSpaceHealthIndicator
instead of org.springframework.boot.actuate.system.DiskSpaceHealthIndicator
?
答案1
得分: 1
是的,您可以简单地提供一个自定义的bean,名称为diskSpaceHealthIndicator
,它将替换默认的DiskSpaceHealthIndicator
:
@Configuration
public class DiskSpaceHealthIndicatorConfiguration {
@Bean
public DiskSpaceHealthIndicator diskSpaceHealthIndicator(DiskSpaceHealthIndicatorProperties properties) {
return new MyDiskSpaceHealthIndicator(properties.getPath(), properties.getThreshold());
}
private static class MyDiskSpaceHealthIndicator extends DiskSpaceHealthIndicator {
public MyDiskSpaceHealthIndicator(File path, DataSize threshold) {
super(path, threshold);
}
@Override
protected void doHealthCheck(Builder builder) throws Exception {
// 在这里执行您需要的操作
super.doHealthCheck(builder);
builder.withDetail("自定义详情", "任何内容");
}
}
}
希望这能帮助您。
英文:
Yes, you can simply provide a custom bean with the name diskSpaceHealthIndicator
and it will replace the default DiskSpaceHealthIndicator
:
@Configuration
public class DiskSpaceHealthIndicatorConfiguration {
@Bean
public DiskSpaceHealthIndicator diskSpaceHealthIndicator(DiskSpaceHealthIndicatorProperties properties) {
return new MyDiskSpaceHealthIndicator(properties.getPath(), properties.getThreshold());
}
private static class MyDiskSpaceHealthIndicator extends DiskSpaceHealthIndicator {
public MyDiskSpaceHealthIndicator(File path, DataSize threshold) {
super(path, threshold);
}
@Override
protected void doHealthCheck(Builder builder) throws Exception {
// Do whatever you need here
super.doHealthCheck(builder);
builder.withDetail("custom details", "whatever");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论