使用自定义的DiskSpaceHealthIndicator(Spring Boot Actuator)?

huangapple go评论67阅读模式
英文:

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");
        }
    }
}

huangapple
  • 本文由 发表于 2020年8月11日 00:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63344032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定