Spring Cloud Hystrix 在第一次命令调用时失败

Spring Cloud Hystrix fails at first command call

我注意到第一个 Hystrix 命令总是调用我的回退,之后的调用在 Spring Cloud Netflix 中运行良好。

有什么设置可以避免吗?为什么会这样?

看起来像是基础设施初始化的副作用和超时 https://groups.google.com/d/msg/hystrixoss/_jnxAyS20lA/fWo0ZAHoxt8J

我通过从 fallback 方法再次发送失败的请求来解决这个问题,这次是用 DicoveryClient 找到远程 pods 之一的 IP :

private String getNameFallback(int delay) {
    RestTemplate rt = new RestTemplate();
    return rt.getForObject(getUrl(delay), String.class);
}

private String getUrl(int delay) {
    String url = String.format("http://%s/name?delay=%d", SERVICE_ID, delay);
    if (discoveryClient != null) {
        Optional<ServiceInstance> svc = discoveryClient.getInstances(SERVICE_ID).stream().findFirst();
        if (svc.isPresent()) {
            String host = svc.get().getHost();
            int port = svc.get().getPort();
            url = "http://" + host + ":" + port + "?delay=" + delay;
        }
    }
    return url;
}

您可以找到更多详细信息here