为什么使用 ribbon ReadTimeout 不会中断 Netflix Ribbon 的长请求?

Why does using ribbonReadTimeout don't break long request with Netflix Ribbon?

我们正在使用 Spring Boot 2.0.0.RELEASEspring-cloud-starter-netflix-ribbon 用于我们的微服务。我为慢速请求设置了 ribbon.readTimeout=1000 并在 @GetMapping 方法中使用我们的微服务设置断点检查它而不发送响应。在我的测试中,我已经等了 10 分钟,没有出现任何异常。好像根本就没有readTimeout。

服务配置

ribbon:
  ReadTimeout: 1000

my-service:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:8080
    ReadTimeout: 1000
    ConnectTimeout: 1000

使它起作用的唯一方法是 ribbon.restclient.enabled=true。但是此客户端已弃用,我不会使用它。

在与 Spring RestTemplate 一起使用时,spring-cloud-netflix 不支持所有功能区属性。如 docs, but ReadTimeout is not one of them. So it does not work, but it's by desing (as per the response to this issue) 中所述,有一些功能区属性有效。但是,如果您使用 Spring 的 RestTemplate,您可以直接在此处设置,如下所示:

@LoadBalanced
@Bean
RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder
        .setReadTimeout(2000)
        .build();
}

我们发现这个:

serviceA.ribbon.ReadTimeout=8000

使用 spring 云 Finchley.SR2

与 spring 启动 2.1.0.RELEASE 配合良好
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

但是,我们在客户端中通过feign使用ribbon:

@FeignClient(value = "serviceA")
public interface ServiceAClient {

    @GetMapping(value = "/test")
    String getTest();
}

然后我们使用 wiremock 测试在读取超时之上引入固定延迟以验证其是否正常工作。

我认为您需要配置 Hystrix 超时。查看文档的这一部分:http://cloud.spring.io/spring-cloud-static/Edgware.RELEASE/single/spring-cloud.html#_hystrix_timeouts_and_ribbon_clients 可能是这样的:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1100
ribbon:
  ConnectTimeout: 1000
  ReadTimeout: 1000