Spring Cloud 线上超时问题总结

1,977 阅读1分钟

前言

​ 因为公司最近将老项目及新项目进行了重构,使用了Spring Cloud架构,虽然解决了些许问题,但也伴随着一些新的问题,本次就来分享一下关于Spring Cloud超时问题的解决方案。

Feign/Ribbon 首次请求超时

  1. 增长 hystrix 超时时间

    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 10000
    

    默认情况 hystrix 超时时间为 1000 .

    default 默认是全局配置,可以修改 defaultHystrixCommandKey,为局部设置。

    详情配置参考:Hystrix Configuration

  2. 禁用 hystrix

    hystrix.command.default.execution.timeout.enabled = false
    
  3. feign 禁用 hystrix (不推荐)

    feign.hystrix.enabled = false
    
  4. Ribbon配置饥饿加载(推荐)

ribbon.eager-load.enabled=true
ribbon.eager-load.clients=service-id,service-id,service-id,...

目前线上项目中使用的就是这种方法.

Zuul

  1. zuul 本身的超时配置

    zuul.host.socket-timeout-millis=10000
    zuul.host.connect-timeout-millis=2000
    
  2. hystrix 超时配置

    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
    
  3. ribbon 超时

    ribbon.ReadTimeout=10000
    ribbon.ConnectTimeout=10000
    
  4. 指定服务(service-id)配置超时时间

    service-id.ribbon.ReadTimeout = 100000
    service-id.ribbon.ConnectTimeout = 100000
    

Feign

  1. 服务调用超时

    feign.client.config.feignName.connectTimeout=5000
    feign.client.config.feignName.readTimeout=5000
    

Ribbon

  1. 全局设定
ribbon.ReadTimeout = 10000
ribbon.ConnectTimeout = 10000
  1. 指定服务设定

    service-id.ribbon.ReadTimeout = 10000
    service-id.ribbon.ConnectTimeout = 10000
    

RestTemplate

@Bean
@LoadBalanced
public RestTemplaterestTemplate(){
    SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    simpleClientHttpRequestFactory.setConnectTimeout(1000);
    simpleClientHttpRequestFactory.setReadTimeout(1000);
    return new RestTemplate(simpleClientHttpRequestFactory);
}