zoukankan      html  css  js  c++  java
  • Spring Cloud常用组件超时总结

     本文以Spring Cloud Finchley.RELEASE版本为例。

    RestTemplate超时时间

    RestTemplate可以通过RestTemplateBuilderl来设置超时时间:

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder
                .setConnectTimeout(...)
           .setReadTimeout(...)
           .build();
    }

    Ribbon超时时间

    #全局超时配置
    ribbon:
      ReadTimeout: 2000  #默认5s
      ConnectTimeout: 1000 #默认2s
    
    #针对具体的服务配置
    service-id:
      ribbon:
        ReadTimeout: 1000
        ConnectTimeout: 1000

    详情见默认配置列表

    Feign超时时间

    文档中没有详细介绍,但部分示例代码中包含了相关配置:

    feign:
      client:
        config:
          default:
            connectTimeout: 5000 #默认10s
            readTimeout: 5000 #默认60s
            loggerLevel: basic

    Feign的默认超时时间在Request.Options中设置的。

    注意:如果在应用里同时使用Feign和Ribbon,那么超时时间配置以Feign的为主。

    Hystrix超时时间

    hystrix:
      command:
        default:
          execution:
            timeout:
              enabled: true #默认true
            isolation:
              thread:
                timeoutInMilliseconds: 5000

    Hystrix作为熔断器,通常其他组件一起使用时(如Ribbon):

    //伪代码调用方式
    hystrix(retry(ribbon()))

    此时需要将超时时间设置成比其他组件的长,否则重试机制将失效。

    Zuul超时时间

    由于Zuul(Spring Cloud)使用了Hystrix和Ribbon,所以它的超时配置是这2个部分的组合。 同时,如果配置了URL路由,而非通过服务的方式(不经Ribbon),还需要配置 zuul.host.connect-timeout-millis。以下是完整配置:

    zuul:
      routes:
        service-id:
          path: /test/**
          serviceId: SERVICE-ID
        admin:
          path: /admin/**
          url: http://test.com/admin
      host: #针对直接发起URL请求的超时配置(不经Ribbon)
        max-total-connections: 5000
        max-per-route-connections: 500
    
    #针对Hystrix的超时配置
    hystrix:
      command:
        default:
          execution:
            timeout:
              enabled: true
            isolation:
              thread:
                timeoutInMilliseconds: 5000
    
    #针对Ribbon的超时配置
    ribbon:
      ReadTimeout: 1500
      ConnectTimeout: 1000

    如果错误地将Hystrix超时配置成2s,而Ribbon配置成5s,那么在每次请求都会看到类似于下面的警告:

    2019-01-19 17:39:20.981 WARN 36568 --- [io-60362-exec-9] o.s.c.n.z.f.r.s.AbstractRibbonCommand : The Hystrix timeout of 2000ms for the command SERVICE-ID is set lower than the combination of the Ribbon read and connect timeout, 12000ms.

    参考

    Feign Client 配置

    Spring Cloud各组件超时总结

  • 相关阅读:
    Openmp编程练习
    PAT-1107 Social Clusters (30 分) 并查集模板
    [无需建树]已知前序或后序和中序遍历结果,输出前序或后序或层次遍历的方法汇总
    微机原理与接口技术笔记(二)
    微机原理与接口技术笔记(一)
    win32API多线程编程
    PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度
    PAT-1015 Reversible Primes (20 分) 进制转换+质数
    PAT-1022 Digital Library (30 分) 字符串处理
    PAT-1013 Battle Over Cities (25 分) DFS求连通块
  • 原文地址:https://www.cnblogs.com/rgshare/p/spring_cloud_timeout_summary.html
Copyright © 2011-2022 走看看