简体   繁体   English

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

[英]Spring Cloud Hystrix fails at first command call

I noticed the first Hystrix command always calls my fallback, and after that the following calls works fine in Spring Cloud Netflix.我注意到第一个 Hystrix 命令总是调用我的回退,之后以下调用在 Spring Cloud Netflix 中工作正常。

Is there any setting should I set to avoid it?我应该设置任何设置来避免它吗? Why does it happen?为什么会发生?

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

I addressed that by sending the failed request again from the fallback method, this time finding the IP of one of the remote pods with DicoveryClient :我通过从回退方法再次发送失败的请求来解决这个问题,这次使用DicoveryClient查找远程 Pod 之一的 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;
}

You can find more details here您可以在此处找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM