【问题标题】:Jhipster Health Endpoint fails when mailserver not connected当邮件服务器未连接时,Jhipster Health Endpoint 失败
【发布时间】:2018-07-27 09:46:55
【问题描述】:

JHipster / Spring boot 提供了一个很好的端点/management/health,它在其中汇总了 db、disk 和 mail 等子系统的健康信息。 不幸的是,当与邮件服务器的连接失败时,整个端点都会失败。所以你不会得到失败的信息。

我得到这样的轨迹:

o.s.b.a.health.MailHealthIndicator       : Health check failed

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: XXXX, NNNN25025; timeout -1
...
   at org.springframework.boot.actuate.health.MailHealthIndicator.doHealthCheck(MailHealthIndicator.java:40)
    at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:43)
    at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68)
    at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:85)
    at org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.getCurrentHealth(HealthMvcEndpoint.java:177)
    at org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.getHealth(HealthMvcEndpoint.java:166)
    at org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(HealthMvcEndpoint.java:143)

这是弹簧靴 1.5.9 我应该在哪里解决这个问题,以便捕获异常并返回错误状态?

【问题讨论】:

    标签: spring-boot jhipster spring-boot-actuator


    【解决方案1】:

    您必须打开您的 SMTP 服务器或禁用对它的检查。

    禁用对 SMTP 服务器的检查:

    management.health.mail.enabled=false
    

    禁用对所有服务器的检查:

    management.health.defaults.enabled=false
    

    更多信息,请参阅http://www.briansjavablog.com/2017/09/health-checks-metric-s-more-with-spring.html

    我相信你可以用 ExceptionHandler for 1.5.9 版本的 Spring Boot 处理这个错误,因为较新的版本没有这样的问题:

         @ControllerAdvice
         public class ExceptionController {
    
            @ExceptionHandler(MailConnectException.class)
            public ResponseEntity<?> mailExceptionHandler(MailConnectException e) {
                  // Do your treatment ...
                } ... 
        }
    

    【讨论】:

    • 我的问题是:为什么健康端点没有捕捉到异常并显示邮件不起作用?
    • 我正在使用 Spring Boot 的 2.1.3,这正如我们预期的那样工作。但在 1.5.9 中,我相信您可以使用 Exception Handler 来解决问题。我将更新我的答案作为示例。
    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多