我有这个HSMHealthIndicator.java类,它不实现HealthIndicator:

@Component
public class HSMHealthIndicator {

    /* constructor */
    public HSMHealthIndicator() {

        setServiceFactory(Application.getServiceFactory());
        errorMessage = "";
    }

    public Health health() {

        if (checkHSMStatus() != 0) {
           return Health.down().withDetail("Error Code",  checkHSMStatus()).build();
        }
        return Health.up().build();
    }

    private int checkHSMStatus() {
        try {
           serviceFactory.getService().ping();
        }
        catch (Exception e){
           logger.error("Exception", e);
           setErrorMessage(e.getMessage());
           return 1;
        }
        return 0;
    }

    ....
}


HSMandDbEndpoint类使用此HealthIndicator来实现Endpoint接口:

public class HSMandDbEndpoint implements Endpoint {

    private String host;
    private int port;

    public HSMandDbEndpoint(String host, int port) {
        this.host = host;
        this.port = port;
    }

    ...

    @Override
    public String invoke() {

        StringBuilder sb = new StringBuilder();
        HSMHealthIndicator h = new HSMHealthIndicator();
        h.setHost(getHost());
        h.setPort(getPort());
        Status s = h.health().getStatus();
        sb.append("Status of the HSM : " + s.getCode());
        if (Status.DOWN.getCode().equalsIgnoreCase(s.getCode()))
            sb.append(" - " + h.getErrorMessage());
        return sb.toString();
        }
 }


当我使用不同于HSM的IP地址进行测试时,我的预期状态为“ DOWN”。但是我在HTTP响应中有代码200。我希望它发送回HTTP错误500。该怎么办?

最佳答案

可能应该是503而不是500。您的invoke方法应返回一个ResponseEntity。您可以查看the source code of HealthMvcEndpoint以获得更多详细信息。

关于java - 当我的HealthIndicator返回DOWN时如何强制服务器发回HTTP错误500,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40807015/

10-09 00:16