【问题标题】:How to change Spring Boot health actuator result for testing?如何更改 Spring Boot 健康执行器结果以进行测试?
【发布时间】:2017-08-08 17:18:24
【问题描述】:

使用 Spring Boot 时 health 执行器
http://localhost:8080/health

{"status":"UP","diskSpace":{"status":"UP","total":122588196864,"free":59227926528,"threshold":10485760},"mongo":{"status":"UP","version":"3.2.6"}}


现在我想检查其他条件,以便检查状态关闭时触发的相关操作。我想要类似的东西

{"status":"Down"}

【问题讨论】:

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


    【解决方案1】:

    首先禁用默认的 /health 端点或将其自定义为一些不同的端点。您可以禁用它关注

    endpoints.health.disabled=true 
    

    禁用此功能后,在 /health 实现您自己的自定义端点,并使用您喜欢检查的任何内容定义您的自定义条件。

    您可以在这里查看creating custom endpoints 在创建自定义端点时不要忘记使用测试配置文件

    【讨论】:

      【解决方案2】:

      您可以编写自定义的“Health Indicator”,它将覆盖默认的运行状况指示器并编写您的实现(例如,始终将状态返回为down)。

      现在,由于这只需要测试应用程序,我建议使用 @Profile 注释它,这样它只会在应用程序启动时激活,比如说 test 配置文件,例如:

      @Component
      @Profile("test")
      public class MyHealthIndicator implements HealthIndicator {
      

      通过这种方式,如果您使用除test 以外的任何配置文件启动应用程序,将使用默认的HealthIndicator

      【讨论】:

      • 感谢 Darshan 的领导,您能否说出所有情况下的健康状况如何,以便可以在开发机器上重新创建场景。
      猜你喜欢
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2018-03-03
      • 1970-01-01
      • 2019-03-10
      • 2017-01-30
      • 1970-01-01
      相关资源
      最近更新 更多