【问题标题】:Using a custom DiskSpaceHealthIndicator (Spring Boot Actuator)?使用自定义 DiskSpaceHealthIndicator(Spring Boot Actuator)?
【发布时间】:2020-08-10 16:19:35
【问题描述】:

我的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

【问题讨论】:

    标签: java spring spring-boot spring-boot-actuator


    【解决方案1】:

    是的,您可以简单地提供一个名为 diskSpaceHealthIndicator 的自定义 bean,它将替换默认的 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");
            }
        }
    }
    

    【讨论】:

    • 谢谢@Mafor,MyDiskSpaceHealthIndicator 需要是静态内部类吗?
    • @Rory 绝对不,它可能放在任何地方。
    猜你喜欢
    • 2021-04-24
    • 2016-05-02
    • 2015-02-22
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 2020-06-01
    相关资源
    最近更新 更多