【问题标题】:Why does using ribbonReadTimeout don't break long request with Netflix Ribbon?为什么使用功能区 ReadTimeout 不会中断 Netflix 功能区的长请求?
【发布时间】:2019-04-22 14:14:39
【问题描述】:

我们将 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-boot spring-cloud spring-cloud-netflix netflix-ribbon


    【解决方案1】:

    在与 Spring RestTemplate 一起使用时,并非所有功能区属性都受 spring-cloud-netflix 支持。如docs 中所述,有一些功能区属性有效,但ReadTimeout 不是其中之一。所以它不起作用,但它是通过设计(根据对此issue的回复)。但是,如果你使用的是 Spring 的RestTemplate,你可以直接在那里设置,如下所示:

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

    【讨论】:

    • 非常感谢。我已经为 RestTemplate 设置了请求超时,现在它工作正常。
    • @AlexeySaltanov 我很高兴它有帮助。由于它已经解决了您的问题,您能否将此答案标记为已接受并给它+1?
    • 我尝试在 application.yml 中设置此超时属性,但收到消息“未知属性 'ribbon.ReadTimeout'”。这种方法效果很好!
    【解决方案2】:

    我认为您需要配置 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
    

    【讨论】:

      【解决方案3】:

      我们发现了这个:

      serviceA.ribbon.ReadTimeout=8000
      

      使用 spring 云 Finchley.SR2 和 spring boot 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 使用功能区:

      @FeignClient(value = "serviceA")
      public interface ServiceAClient {
      
          @GetMapping(value = "/test")
          String getTest();
      }
      

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

      【讨论】:

        猜你喜欢
        • 2018-11-02
        • 2017-01-02
        • 1970-01-01
        • 2020-08-04
        • 2013-05-04
        • 2016-04-18
        • 1970-01-01
        • 1970-01-01
        • 2014-10-06
        相关资源
        最近更新 更多